]> git.proxmox.com Git - mirror_frr.git/blob - ripd/ripd.c
9e8c21da31ca57f92704c373d66d0fb574bc6d61
[mirror_frr.git] / ripd / ripd.c
1 /* RIP version 1 and 2.
2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "vrf.h"
26 #include "if.h"
27 #include "command.h"
28 #include "prefix.h"
29 #include "table.h"
30 #include "thread.h"
31 #include "memory.h"
32 #include "log.h"
33 #include "stream.h"
34 #include "filter.h"
35 #include "sockunion.h"
36 #include "sockopt.h"
37 #include "routemap.h"
38 #include "if_rmap.h"
39 #include "plist.h"
40 #include "distribute.h"
41 #include "md5.h"
42 #include "keychain.h"
43 #include "privs.h"
44
45 #include "ripd/ripd.h"
46 #include "ripd/rip_debug.h"
47
48 DEFINE_QOBJ_TYPE(rip)
49
50 /* UDP receive buffer size */
51 #define RIP_UDP_RCV_BUF 41600
52
53 /* privileges global */
54 extern struct zebra_privs_t ripd_privs;
55
56 /* RIP Structure. */
57 struct rip *rip = NULL;
58
59 /* RIP neighbor address table. */
60 struct route_table *rip_neighbor_table;
61
62 /* RIP route changes. */
63 long rip_global_route_changes = 0;
64
65 /* RIP queries. */
66 long rip_global_queries = 0;
67
68 /* Prototypes. */
69 static void rip_event (enum rip_event, int);
70 static void rip_output_process (struct connected *, struct sockaddr_in *, int, u_char);
71 static int rip_triggered_update (struct thread *);
72 static int rip_update_jitter (unsigned long);
73
74 /* RIP output routes type. */
75 enum
76 {
77 rip_all_route,
78 rip_changed_route
79 };
80
81 /* RIP command strings. */
82 static const struct message rip_msg[] =
83 {
84 {RIP_REQUEST, "REQUEST"},
85 {RIP_RESPONSE, "RESPONSE"},
86 {RIP_TRACEON, "TRACEON"},
87 {RIP_TRACEOFF, "TRACEOFF"},
88 {RIP_POLL, "POLL"},
89 {RIP_POLL_ENTRY, "POLL ENTRY"},
90 {0, NULL},
91 };
92
93 /* Utility function to set boradcast option to the socket. */
94 static int
95 sockopt_broadcast (int sock)
96 {
97 int ret;
98 int on = 1;
99
100 ret = setsockopt (sock, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof on);
101 if (ret < 0)
102 {
103 zlog_warn ("can't set sockopt SO_BROADCAST to socket %d", sock);
104 return -1;
105 }
106 return 0;
107 }
108
109 static int
110 rip_route_rte (struct rip_info *rinfo)
111 {
112 return (rinfo->type == ZEBRA_ROUTE_RIP && rinfo->sub_type == RIP_ROUTE_RTE);
113 }
114
115 static struct rip_info *
116 rip_info_new (void)
117 {
118 return XCALLOC (MTYPE_RIP_INFO, sizeof (struct rip_info));
119 }
120
121 void
122 rip_info_free (struct rip_info *rinfo)
123 {
124 XFREE (MTYPE_RIP_INFO, rinfo);
125 }
126
127 /* RIP route garbage collect timer. */
128 static int
129 rip_garbage_collect (struct thread *t)
130 {
131 struct rip_info *rinfo;
132 struct route_node *rp;
133
134 rinfo = THREAD_ARG (t);
135 rinfo->t_garbage_collect = NULL;
136
137 /* Off timeout timer. */
138 RIP_TIMER_OFF (rinfo->t_timeout);
139
140 /* Get route_node pointer. */
141 rp = rinfo->rp;
142
143 /* Unlock route_node. */
144 listnode_delete (rp->info, rinfo);
145 if (list_isempty ((struct list *)rp->info))
146 {
147 list_free (rp->info);
148 rp->info = NULL;
149 route_unlock_node (rp);
150 }
151
152 /* Free RIP routing information. */
153 rip_info_free (rinfo);
154
155 return 0;
156 }
157
158 static void rip_timeout_update (struct rip_info *rinfo);
159
160 /* Add new route to the ECMP list.
161 * RETURN: the new entry added in the list, or NULL if it is not the first
162 * entry and ECMP is not allowed.
163 */
164 struct rip_info *
165 rip_ecmp_add (struct rip_info *rinfo_new)
166 {
167 struct route_node *rp = rinfo_new->rp;
168 struct rip_info *rinfo = NULL;
169 struct list *list = NULL;
170
171 if (rp->info == NULL)
172 rp->info = list_new ();
173 list = (struct list *)rp->info;
174
175 /* If ECMP is not allowed and some entry already exists in the list,
176 * do nothing. */
177 if (listcount (list) && !rip->ecmp)
178 return NULL;
179
180 rinfo = rip_info_new ();
181 memcpy (rinfo, rinfo_new, sizeof (struct rip_info));
182 listnode_add (list, rinfo);
183
184 if (rip_route_rte (rinfo))
185 {
186 rip_timeout_update (rinfo);
187 rip_zebra_ipv4_add (rp);
188 }
189
190 /* Set the route change flag on the first entry. */
191 rinfo = listgetdata (listhead (list));
192 SET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
193
194 /* Signal the output process to trigger an update (see section 2.5). */
195 rip_event (RIP_TRIGGERED_UPDATE, 0);
196
197 return rinfo;
198 }
199
200 /* Replace the ECMP list with the new route.
201 * RETURN: the new entry added in the list
202 */
203 struct rip_info *
204 rip_ecmp_replace (struct rip_info *rinfo_new)
205 {
206 struct route_node *rp = rinfo_new->rp;
207 struct list *list = (struct list *)rp->info;
208 struct rip_info *rinfo = NULL, *tmp_rinfo = NULL;
209 struct listnode *node = NULL, *nextnode = NULL;
210
211 if (list == NULL || listcount (list) == 0)
212 return rip_ecmp_add (rinfo_new);
213
214 /* Get the first entry */
215 rinfo = listgetdata (listhead (list));
216
217 /* Learnt route replaced by a local one. Delete it from zebra. */
218 if (rip_route_rte (rinfo) && !rip_route_rte (rinfo_new))
219 if (CHECK_FLAG (rinfo->flags, RIP_RTF_FIB))
220 rip_zebra_ipv4_delete (rp);
221
222 /* Re-use the first entry, and delete the others. */
223 for (ALL_LIST_ELEMENTS (list, node, nextnode, tmp_rinfo))
224 if (tmp_rinfo != rinfo)
225 {
226 RIP_TIMER_OFF (tmp_rinfo->t_timeout);
227 RIP_TIMER_OFF (tmp_rinfo->t_garbage_collect);
228 list_delete_node (list, node);
229 rip_info_free (tmp_rinfo);
230 }
231
232 RIP_TIMER_OFF (rinfo->t_timeout);
233 RIP_TIMER_OFF (rinfo->t_garbage_collect);
234 memcpy (rinfo, rinfo_new, sizeof (struct rip_info));
235
236 if (rip_route_rte (rinfo))
237 {
238 rip_timeout_update (rinfo);
239 /* The ADD message implies an update. */
240 rip_zebra_ipv4_add (rp);
241 }
242
243 /* Set the route change flag. */
244 SET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
245
246 /* Signal the output process to trigger an update (see section 2.5). */
247 rip_event (RIP_TRIGGERED_UPDATE, 0);
248
249 return rinfo;
250 }
251
252 /* Delete one route from the ECMP list.
253 * RETURN:
254 * null - the entry is freed, and other entries exist in the list
255 * the entry - the entry is the last one in the list; its metric is set
256 * to INFINITY, and the garbage collector is started for it
257 */
258 struct rip_info *
259 rip_ecmp_delete (struct rip_info *rinfo)
260 {
261 struct route_node *rp = rinfo->rp;
262 struct list *list = (struct list *)rp->info;
263
264 RIP_TIMER_OFF (rinfo->t_timeout);
265
266 if (listcount (list) > 1)
267 {
268 /* Some other ECMP entries still exist. Just delete this entry. */
269 RIP_TIMER_OFF (rinfo->t_garbage_collect);
270 listnode_delete (list, rinfo);
271 if (rip_route_rte (rinfo) && CHECK_FLAG (rinfo->flags, RIP_RTF_FIB))
272 /* The ADD message implies the update. */
273 rip_zebra_ipv4_add (rp);
274 rip_info_free (rinfo);
275 rinfo = NULL;
276 }
277 else
278 {
279 assert (rinfo == listgetdata (listhead (list)));
280
281 /* This is the only entry left in the list. We must keep it in
282 * the list for garbage collection time, with INFINITY metric. */
283
284 rinfo->metric = RIP_METRIC_INFINITY;
285 RIP_TIMER_ON (rinfo->t_garbage_collect,
286 rip_garbage_collect, rip->garbage_time);
287
288 if (rip_route_rte (rinfo) && CHECK_FLAG (rinfo->flags, RIP_RTF_FIB))
289 rip_zebra_ipv4_delete (rp);
290 }
291
292 /* Set the route change flag on the first entry. */
293 rinfo = listgetdata (listhead (list));
294 SET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
295
296 /* Signal the output process to trigger an update (see section 2.5). */
297 rip_event (RIP_TRIGGERED_UPDATE, 0);
298
299 return rinfo;
300 }
301
302 /* Timeout RIP routes. */
303 static int
304 rip_timeout (struct thread *t)
305 {
306 rip_ecmp_delete ((struct rip_info *)THREAD_ARG (t));
307 return 0;
308 }
309
310 static void
311 rip_timeout_update (struct rip_info *rinfo)
312 {
313 if (rinfo->metric != RIP_METRIC_INFINITY)
314 {
315 RIP_TIMER_OFF (rinfo->t_timeout);
316 RIP_TIMER_ON (rinfo->t_timeout, rip_timeout, rip->timeout_time);
317 }
318 }
319
320 static int
321 rip_filter (int rip_distribute, struct prefix_ipv4 *p, struct rip_interface *ri)
322 {
323 struct distribute *dist;
324 struct access_list *alist;
325 struct prefix_list *plist;
326 int distribute = rip_distribute == RIP_FILTER_OUT ?
327 DISTRIBUTE_V4_OUT : DISTRIBUTE_V4_IN;
328 const char *inout = rip_distribute == RIP_FILTER_OUT ? "out" : "in";
329
330 /* Input distribute-list filtering. */
331 if (ri->list[rip_distribute])
332 {
333 if (access_list_apply (ri->list[rip_distribute],
334 (struct prefix *) p) == FILTER_DENY)
335 {
336 if (IS_RIP_DEBUG_PACKET)
337 zlog_debug ("%s/%d filtered by distribute %s",
338 inet_ntoa (p->prefix), p->prefixlen, inout);
339 return -1;
340 }
341 }
342 if (ri->prefix[rip_distribute])
343 {
344 if (prefix_list_apply (ri->prefix[rip_distribute],
345 (struct prefix *) p) == PREFIX_DENY)
346 {
347 if (IS_RIP_DEBUG_PACKET)
348 zlog_debug ("%s/%d filtered by prefix-list %s",
349 inet_ntoa (p->prefix), p->prefixlen, inout);
350 return -1;
351 }
352 }
353
354 /* All interface filter check. */
355 dist = distribute_lookup (NULL);
356 if (dist)
357 {
358 if (dist->list[distribute])
359 {
360 alist = access_list_lookup (AFI_IP, dist->list[distribute]);
361
362 if (alist)
363 {
364 if (access_list_apply (alist, (struct prefix *) p) == FILTER_DENY)
365 {
366 if (IS_RIP_DEBUG_PACKET)
367 zlog_debug ("%s/%d filtered by distribute %s",
368 inet_ntoa (p->prefix), p->prefixlen, inout);
369 return -1;
370 }
371 }
372 }
373 if (dist->prefix[distribute])
374 {
375 plist = prefix_list_lookup (AFI_IP, dist->prefix[distribute]);
376
377 if (plist)
378 {
379 if (prefix_list_apply (plist,
380 (struct prefix *) p) == PREFIX_DENY)
381 {
382 if (IS_RIP_DEBUG_PACKET)
383 zlog_debug ("%s/%d filtered by prefix-list %s",
384 inet_ntoa (p->prefix), p->prefixlen, inout);
385 return -1;
386 }
387 }
388 }
389 }
390 return 0;
391 }
392
393 /* Check nexthop address validity. */
394 static int
395 rip_nexthop_check (struct in_addr *addr)
396 {
397 struct listnode *node;
398 struct listnode *cnode;
399 struct interface *ifp;
400 struct connected *ifc;
401 struct prefix *p;
402
403 /* If nexthop address matches local configured address then it is
404 invalid nexthop. */
405
406 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
407 {
408 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, ifc))
409 {
410 p = ifc->address;
411
412 if (p->family == AF_INET
413 && IPV4_ADDR_SAME (&p->u.prefix4, addr))
414 return -1;
415 }
416 }
417 return 0;
418 }
419
420 /* RIP add route to routing table. */
421 static void
422 rip_rte_process (struct rte *rte, struct sockaddr_in *from,
423 struct interface *ifp)
424 {
425 int ret;
426 struct prefix_ipv4 p;
427 struct route_node *rp;
428 struct rip_info *rinfo = NULL, newinfo;
429 struct rip_interface *ri;
430 struct in_addr *nexthop;
431 int same = 0;
432 unsigned char old_dist, new_dist;
433 struct list *list = NULL;
434 struct listnode *node = NULL;
435
436 /* Make prefix structure. */
437 memset (&p, 0, sizeof (struct prefix_ipv4));
438 p.family = AF_INET;
439 p.prefix = rte->prefix;
440 p.prefixlen = ip_masklen (rte->mask);
441
442 /* Make sure mask is applied. */
443 apply_mask_ipv4 (&p);
444
445 /* Apply input filters. */
446 ri = ifp->info;
447
448 ret = rip_filter (RIP_FILTER_IN, &p, ri);
449 if (ret < 0)
450 return;
451
452 memset (&newinfo, 0, sizeof (newinfo));
453 newinfo.type = ZEBRA_ROUTE_RIP;
454 newinfo.sub_type = RIP_ROUTE_RTE;
455 newinfo.nexthop = rte->nexthop;
456 newinfo.from = from->sin_addr;
457 newinfo.ifindex = ifp->ifindex;
458 newinfo.metric = rte->metric;
459 newinfo.metric_out = rte->metric; /* XXX */
460 newinfo.tag = ntohs (rte->tag); /* XXX */
461
462 /* Modify entry according to the interface routemap. */
463 if (ri->routemap[RIP_FILTER_IN])
464 {
465 int ret;
466
467 /* The object should be of the type of rip_info */
468 ret = route_map_apply (ri->routemap[RIP_FILTER_IN],
469 (struct prefix *) &p, RMAP_RIP, &newinfo);
470
471 if (ret == RMAP_DENYMATCH)
472 {
473 if (IS_RIP_DEBUG_PACKET)
474 zlog_debug ("RIP %s/%d is filtered by route-map in",
475 inet_ntoa (p.prefix), p.prefixlen);
476 return;
477 }
478
479 /* Get back the object */
480 rte->nexthop = newinfo.nexthop_out;
481 rte->tag = htons (newinfo.tag_out); /* XXX */
482 rte->metric = newinfo.metric_out; /* XXX: the routemap uses the metric_out field */
483 }
484
485 /* Once the entry has been validated, update the metric by
486 adding the cost of the network on wich the message
487 arrived. If the result is greater than infinity, use infinity
488 (RFC2453 Sec. 3.9.2) */
489 /* Zebra ripd can handle offset-list in. */
490 ret = rip_offset_list_apply_in (&p, ifp, &rte->metric);
491
492 /* If offset-list does not modify the metric use interface's
493 metric. */
494 if (!ret)
495 rte->metric += ifp->metric ? ifp->metric : 1;
496
497 if (rte->metric > RIP_METRIC_INFINITY)
498 rte->metric = RIP_METRIC_INFINITY;
499
500 /* Set nexthop pointer. */
501 if (rte->nexthop.s_addr == 0)
502 nexthop = &from->sin_addr;
503 else
504 nexthop = &rte->nexthop;
505
506 /* Check if nexthop address is myself, then do nothing. */
507 if (rip_nexthop_check (nexthop) < 0)
508 {
509 if (IS_RIP_DEBUG_PACKET)
510 zlog_debug ("Nexthop address %s is myself", inet_ntoa (*nexthop));
511 return;
512 }
513
514 /* Get index for the prefix. */
515 rp = route_node_get (rip->table, (struct prefix *) &p);
516
517 newinfo.rp = rp;
518 newinfo.nexthop = *nexthop;
519 newinfo.metric = rte->metric;
520 newinfo.tag = ntohs (rte->tag);
521 newinfo.distance = rip_distance_apply (&newinfo);
522
523 new_dist = newinfo.distance ? newinfo.distance : ZEBRA_RIP_DISTANCE_DEFAULT;
524
525 /* Check to see whether there is already RIP route on the table. */
526 if ((list = rp->info) != NULL)
527 for (ALL_LIST_ELEMENTS_RO (list, node, rinfo))
528 {
529 /* Need to compare with redistributed entry or local entry */
530 if (!rip_route_rte (rinfo))
531 break;
532
533 if (IPV4_ADDR_SAME (&rinfo->from, &from->sin_addr) &&
534 IPV4_ADDR_SAME (&rinfo->nexthop, nexthop))
535 break;
536
537 if (!listnextnode (node))
538 {
539 /* Not found in the list */
540
541 if (rte->metric > rinfo->metric)
542 {
543 /* New route has a greater metric. Discard it. */
544 route_unlock_node (rp);
545 return;
546 }
547
548 if (rte->metric < rinfo->metric)
549 /* New route has a smaller metric. Replace the ECMP list
550 * with the new one in below. */
551 break;
552
553 /* Metrics are same. We compare the distances. */
554 old_dist = rinfo->distance ? \
555 rinfo->distance : ZEBRA_RIP_DISTANCE_DEFAULT;
556
557 if (new_dist > old_dist)
558 {
559 /* New route has a greater distance. Discard it. */
560 route_unlock_node (rp);
561 return;
562 }
563
564 if (new_dist < old_dist)
565 /* New route has a smaller distance. Replace the ECMP list
566 * with the new one in below. */
567 break;
568
569 /* Metrics and distances are both same. Keep "rinfo" null and
570 * the new route is added in the ECMP list in below. */
571 }
572 }
573
574 if (rinfo)
575 {
576 /* Local static route. */
577 if (rinfo->type == ZEBRA_ROUTE_RIP
578 && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
579 (rinfo->sub_type == RIP_ROUTE_DEFAULT))
580 && rinfo->metric != RIP_METRIC_INFINITY)
581 {
582 route_unlock_node (rp);
583 return;
584 }
585
586 /* Redistributed route check. */
587 if (rinfo->type != ZEBRA_ROUTE_RIP
588 && rinfo->metric != RIP_METRIC_INFINITY)
589 {
590 old_dist = rinfo->distance;
591 /* Only routes directly connected to an interface (nexthop == 0)
592 * may have a valid NULL distance */
593 if (rinfo->nexthop.s_addr != 0)
594 old_dist = old_dist ? old_dist : ZEBRA_RIP_DISTANCE_DEFAULT;
595 /* If imported route does not have STRICT precedence,
596 mark it as a ghost */
597 if (new_dist <= old_dist && rte->metric != RIP_METRIC_INFINITY)
598 rip_ecmp_replace (&newinfo);
599
600 route_unlock_node (rp);
601 return;
602 }
603 }
604
605 if (!rinfo)
606 {
607 if (rp->info)
608 route_unlock_node (rp);
609
610 /* Now, check to see whether there is already an explicit route
611 for the destination prefix. If there is no such route, add
612 this route to the routing table, unless the metric is
613 infinity (there is no point in adding a route which
614 unusable). */
615 if (rte->metric != RIP_METRIC_INFINITY)
616 rip_ecmp_add (&newinfo);
617 }
618 else
619 {
620 /* Route is there but we are not sure the route is RIP or not. */
621
622 /* If there is an existing route, compare the next hop address
623 to the address of the router from which the datagram came.
624 If this datagram is from the same router as the existing
625 route, reinitialize the timeout. */
626 same = (IPV4_ADDR_SAME (&rinfo->from, &from->sin_addr)
627 && (rinfo->ifindex == ifp->ifindex));
628
629 old_dist = rinfo->distance ? \
630 rinfo->distance : ZEBRA_RIP_DISTANCE_DEFAULT;
631
632 /* Next, compare the metrics. If the datagram is from the same
633 router as the existing route, and the new metric is different
634 than the old one; or, if the new metric is lower than the old
635 one, or if the tag has been changed; or if there is a route
636 with a lower administrave distance; or an update of the
637 distance on the actual route; do the following actions: */
638 if ((same && rinfo->metric != rte->metric)
639 || (rte->metric < rinfo->metric)
640 || ((same)
641 && (rinfo->metric == rte->metric)
642 && (newinfo.tag != rinfo->tag))
643 || (old_dist > new_dist)
644 || ((old_dist != new_dist) && same))
645 {
646 if (listcount (list) == 1)
647 {
648 if (newinfo.metric != RIP_METRIC_INFINITY)
649 rip_ecmp_replace (&newinfo);
650 else
651 rip_ecmp_delete (rinfo);
652 }
653 else
654 {
655 if (newinfo.metric < rinfo->metric)
656 rip_ecmp_replace (&newinfo);
657 else if (newinfo.metric > rinfo->metric)
658 rip_ecmp_delete (rinfo);
659 else if (new_dist < old_dist)
660 rip_ecmp_replace (&newinfo);
661 else if (new_dist > old_dist)
662 rip_ecmp_delete (rinfo);
663 else
664 {
665 int update = CHECK_FLAG (rinfo->flags, RIP_RTF_FIB) ? 1 : 0;
666
667 assert (newinfo.metric != RIP_METRIC_INFINITY);
668
669 RIP_TIMER_OFF (rinfo->t_timeout);
670 RIP_TIMER_OFF (rinfo->t_garbage_collect);
671 memcpy (rinfo, &newinfo, sizeof (struct rip_info));
672 rip_timeout_update (rinfo);
673
674 if (update)
675 rip_zebra_ipv4_add (rp);
676
677 /* - Set the route change flag on the first entry. */
678 rinfo = listgetdata (listhead (list));
679 SET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
680 rip_event (RIP_TRIGGERED_UPDATE, 0);
681 }
682 }
683 }
684 else /* same & no change */
685 rip_timeout_update (rinfo);
686
687 /* Unlock tempolary lock of the route. */
688 route_unlock_node (rp);
689 }
690 }
691
692 /* Dump RIP packet */
693 static void
694 rip_packet_dump (struct rip_packet *packet, int size, const char *sndrcv)
695 {
696 caddr_t lim;
697 struct rte *rte;
698 const char *command_str;
699 char pbuf[BUFSIZ], nbuf[BUFSIZ];
700 u_char netmask = 0;
701 u_char *p;
702
703 /* Set command string. */
704 if (packet->command > 0 && packet->command < RIP_COMMAND_MAX)
705 command_str = lookup (rip_msg, packet->command);
706 else
707 command_str = "unknown";
708
709 /* Dump packet header. */
710 zlog_debug ("%s %s version %d packet size %d",
711 sndrcv, command_str, packet->version, size);
712
713 /* Dump each routing table entry. */
714 rte = packet->rte;
715
716 for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
717 {
718 if (packet->version == RIPv2)
719 {
720 netmask = ip_masklen (rte->mask);
721
722 if (rte->family == htons (RIP_FAMILY_AUTH))
723 {
724 if (rte->tag == htons (RIP_AUTH_SIMPLE_PASSWORD))
725 {
726 p = (u_char *)&rte->prefix;
727
728 zlog_debug (" family 0x%X type %d auth string: %s",
729 ntohs (rte->family), ntohs (rte->tag), p);
730 }
731 else if (rte->tag == htons (RIP_AUTH_MD5))
732 {
733 struct rip_md5_info *md5;
734
735 md5 = (struct rip_md5_info *) &packet->rte;
736
737 zlog_debug (" family 0x%X type %d (MD5 authentication)",
738 ntohs (md5->family), ntohs (md5->type));
739 zlog_debug (" RIP-2 packet len %d Key ID %d"
740 " Auth Data len %d",
741 ntohs (md5->packet_len), md5->keyid,
742 md5->auth_len);
743 zlog_debug (" Sequence Number %ld",
744 (u_long) ntohl (md5->sequence));
745 }
746 else if (rte->tag == htons (RIP_AUTH_DATA))
747 {
748 p = (u_char *)&rte->prefix;
749
750 zlog_debug (" family 0x%X type %d (MD5 data)",
751 ntohs (rte->family), ntohs (rte->tag));
752 zlog_debug (" MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
753 "%02X%02X%02X%02X%02X%02X%02X%02X",
754 p[0], p[1], p[2], p[3], p[4], p[5], p[6],
755 p[7], p[8], p[9], p[10], p[11], p[12], p[13],
756 p[14], p[15]);
757 }
758 else
759 {
760 zlog_debug (" family 0x%X type %d (Unknown auth type)",
761 ntohs (rte->family), ntohs (rte->tag));
762 }
763 }
764 else
765 zlog_debug (" %s/%d -> %s family %d tag %"ROUTE_TAG_PRI" metric %ld",
766 inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
767 netmask, inet_ntop (AF_INET, &rte->nexthop, nbuf,
768 BUFSIZ), ntohs (rte->family),
769 (route_tag_t)ntohs (rte->tag),
770 (u_long) ntohl (rte->metric));
771 }
772 else
773 {
774 zlog_debug (" %s family %d tag %"ROUTE_TAG_PRI" metric %ld",
775 inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
776 ntohs (rte->family), (route_tag_t)ntohs (rte->tag),
777 (u_long)ntohl (rte->metric));
778 }
779 }
780 }
781
782 /* Check if the destination address is valid (unicast; not net 0
783 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
784 check net 0 because we accept default route. */
785 static int
786 rip_destination_check (struct in_addr addr)
787 {
788 u_int32_t destination;
789
790 /* Convert to host byte order. */
791 destination = ntohl (addr.s_addr);
792
793 if (IPV4_NET127 (destination))
794 return 0;
795
796 /* Net 0 may match to the default route. */
797 if (IPV4_NET0 (destination) && destination != 0)
798 return 0;
799
800 /* Unicast address must belong to class A, B, C. */
801 if (IN_CLASSA (destination))
802 return 1;
803 if (IN_CLASSB (destination))
804 return 1;
805 if (IN_CLASSC (destination))
806 return 1;
807
808 return 0;
809 }
810
811 /* RIP version 2 authentication. */
812 static int
813 rip_auth_simple_password (struct rte *rte, struct sockaddr_in *from,
814 struct interface *ifp)
815 {
816 struct rip_interface *ri;
817 char *auth_str = (char *) &rte->prefix;
818 int i;
819
820 /* reject passwords with zeros in the middle of the string */
821 for (i = strlen (auth_str); i < 16; i++)
822 {
823 if (auth_str[i] != '\0')
824 return 0;
825 }
826
827 if (IS_RIP_DEBUG_EVENT)
828 zlog_debug ("RIPv2 simple password authentication from %s",
829 inet_ntoa (from->sin_addr));
830
831 ri = ifp->info;
832
833 if (ri->auth_type != RIP_AUTH_SIMPLE_PASSWORD
834 || rte->tag != htons(RIP_AUTH_SIMPLE_PASSWORD))
835 return 0;
836
837 /* Simple password authentication. */
838 if (ri->auth_str)
839 {
840 if (strncmp (auth_str, ri->auth_str, 16) == 0)
841 return 1;
842 }
843 if (ri->key_chain)
844 {
845 struct keychain *keychain;
846 struct key *key;
847
848 keychain = keychain_lookup (ri->key_chain);
849 if (keychain == NULL)
850 return 0;
851
852 key = key_match_for_accept (keychain, auth_str);
853 if (key)
854 return 1;
855 }
856 return 0;
857 }
858
859 /* RIP version 2 authentication with MD5. */
860 static int
861 rip_auth_md5 (struct rip_packet *packet, struct sockaddr_in *from,
862 int length, struct interface *ifp)
863 {
864 struct rip_interface *ri;
865 struct rip_md5_info *md5;
866 struct rip_md5_data *md5data;
867 struct keychain *keychain;
868 struct key *key;
869 MD5_CTX ctx;
870 u_char digest[RIP_AUTH_MD5_SIZE];
871 u_int16_t packet_len;
872 char auth_str[RIP_AUTH_MD5_SIZE];
873
874 if (IS_RIP_DEBUG_EVENT)
875 zlog_debug ("RIPv2 MD5 authentication from %s",
876 inet_ntoa (from->sin_addr));
877
878 ri = ifp->info;
879 md5 = (struct rip_md5_info *) &packet->rte;
880
881 /* Check auth type. */
882 if (ri->auth_type != RIP_AUTH_MD5 || md5->type != htons(RIP_AUTH_MD5))
883 return 0;
884
885 /* If the authentication length is less than 16, then it must be wrong for
886 * any interpretation of rfc2082. Some implementations also interpret
887 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka RIP_AUTH_MD5_COMPAT_SIZE.
888 */
889 if ( !((md5->auth_len == RIP_AUTH_MD5_SIZE)
890 || (md5->auth_len == RIP_AUTH_MD5_COMPAT_SIZE)))
891 {
892 if (IS_RIP_DEBUG_EVENT)
893 zlog_debug ("RIPv2 MD5 authentication, strange authentication "
894 "length field %d", md5->auth_len);
895 return 0;
896 }
897
898 /* grab and verify check packet length */
899 packet_len = ntohs (md5->packet_len);
900
901 if (packet_len > (length - RIP_HEADER_SIZE - RIP_AUTH_MD5_SIZE))
902 {
903 if (IS_RIP_DEBUG_EVENT)
904 zlog_debug ("RIPv2 MD5 authentication, packet length field %d "
905 "greater than received length %d!",
906 md5->packet_len, length);
907 return 0;
908 }
909
910 /* retrieve authentication data */
911 md5data = (struct rip_md5_data *) (((u_char *) packet) + packet_len);
912
913 memset (auth_str, 0, RIP_AUTH_MD5_SIZE);
914
915 if (ri->key_chain)
916 {
917 keychain = keychain_lookup (ri->key_chain);
918 if (keychain == NULL)
919 return 0;
920
921 key = key_lookup_for_accept (keychain, md5->keyid);
922 if (key == NULL)
923 return 0;
924
925 strncpy (auth_str, key->string, RIP_AUTH_MD5_SIZE);
926 }
927 else if (ri->auth_str)
928 strncpy (auth_str, ri->auth_str, RIP_AUTH_MD5_SIZE);
929
930 if (auth_str[0] == 0)
931 return 0;
932
933 /* MD5 digest authentication. */
934 memset (&ctx, 0, sizeof(ctx));
935 MD5Init(&ctx);
936 MD5Update(&ctx, packet, packet_len + RIP_HEADER_SIZE);
937 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
938 MD5Final(digest, &ctx);
939
940 if (memcmp (md5data->digest, digest, RIP_AUTH_MD5_SIZE) == 0)
941 return packet_len;
942 else
943 return 0;
944 }
945
946 /* Pick correct auth string for sends, prepare auth_str buffer for use.
947 * (left justified and padded).
948 *
949 * presumes one of ri or key is valid, and that the auth strings they point
950 * to are nul terminated. If neither are present, auth_str will be fully
951 * zero padded.
952 *
953 */
954 static void
955 rip_auth_prepare_str_send (struct rip_interface *ri, struct key *key,
956 char *auth_str, int len)
957 {
958 assert (ri || key);
959
960 memset (auth_str, 0, len);
961 if (key && key->string)
962 strncpy (auth_str, key->string, len);
963 else if (ri->auth_str)
964 strncpy (auth_str, ri->auth_str, len);
965
966 return;
967 }
968
969 /* Write RIPv2 simple password authentication information
970 *
971 * auth_str is presumed to be 2 bytes and correctly prepared
972 * (left justified and zero padded).
973 */
974 static void
975 rip_auth_simple_write (struct stream *s, char *auth_str, int len)
976 {
977 assert (s && len == RIP_AUTH_SIMPLE_SIZE);
978
979 stream_putw (s, RIP_FAMILY_AUTH);
980 stream_putw (s, RIP_AUTH_SIMPLE_PASSWORD);
981 stream_put (s, auth_str, RIP_AUTH_SIMPLE_SIZE);
982
983 return;
984 }
985
986 /* write RIPv2 MD5 "authentication header"
987 * (uses the auth key data field)
988 *
989 * Digest offset field is set to 0.
990 *
991 * returns: offset of the digest offset field, which must be set when
992 * length to the auth-data MD5 digest is known.
993 */
994 static size_t
995 rip_auth_md5_ah_write (struct stream *s, struct rip_interface *ri,
996 struct key *key)
997 {
998 size_t doff = 0;
999
1000 assert (s && ri && ri->auth_type == RIP_AUTH_MD5);
1001
1002 /* MD5 authentication. */
1003 stream_putw (s, RIP_FAMILY_AUTH);
1004 stream_putw (s, RIP_AUTH_MD5);
1005
1006 /* MD5 AH digest offset field.
1007 *
1008 * Set to placeholder value here, to true value when RIP-2 Packet length
1009 * is known. Actual value is set in .....().
1010 */
1011 doff = stream_get_endp(s);
1012 stream_putw (s, 0);
1013
1014 /* Key ID. */
1015 if (key)
1016 stream_putc (s, key->index % 256);
1017 else
1018 stream_putc (s, 1);
1019
1020 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1021 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for this
1022 * to be configurable.
1023 */
1024 stream_putc (s, ri->md5_auth_len);
1025
1026 /* Sequence Number (non-decreasing). */
1027 /* RFC2080: The value used in the sequence number is
1028 arbitrary, but two suggestions are the time of the
1029 message's creation or a simple message counter. */
1030 stream_putl (s, time (NULL));
1031
1032 /* Reserved field must be zero. */
1033 stream_putl (s, 0);
1034 stream_putl (s, 0);
1035
1036 return doff;
1037 }
1038
1039 /* If authentication is in used, write the appropriate header
1040 * returns stream offset to which length must later be written
1041 * or 0 if this is not required
1042 */
1043 static size_t
1044 rip_auth_header_write (struct stream *s, struct rip_interface *ri,
1045 struct key *key, char *auth_str, int len)
1046 {
1047 assert (ri->auth_type != RIP_NO_AUTH);
1048
1049 switch (ri->auth_type)
1050 {
1051 case RIP_AUTH_SIMPLE_PASSWORD:
1052 rip_auth_prepare_str_send (ri, key, auth_str, len);
1053 rip_auth_simple_write (s, auth_str, len);
1054 return 0;
1055 case RIP_AUTH_MD5:
1056 return rip_auth_md5_ah_write (s, ri, key);
1057 }
1058 assert (1);
1059 return 0;
1060 }
1061
1062 /* Write RIPv2 MD5 authentication data trailer */
1063 static void
1064 rip_auth_md5_set (struct stream *s, struct rip_interface *ri, size_t doff,
1065 char *auth_str, int authlen)
1066 {
1067 unsigned long len;
1068 MD5_CTX ctx;
1069 unsigned char digest[RIP_AUTH_MD5_SIZE];
1070
1071 /* Make it sure this interface is configured as MD5
1072 authentication. */
1073 assert ((ri->auth_type == RIP_AUTH_MD5) && (authlen == RIP_AUTH_MD5_SIZE));
1074 assert (doff > 0);
1075
1076 /* Get packet length. */
1077 len = stream_get_endp(s);
1078
1079 /* Check packet length. */
1080 if (len < (RIP_HEADER_SIZE + RIP_RTE_SIZE))
1081 {
1082 zlog_err ("rip_auth_md5_set(): packet length %ld is less than minimum length.", len);
1083 return;
1084 }
1085
1086 /* Set the digest offset length in the header */
1087 stream_putw_at (s, doff, len);
1088
1089 /* Set authentication data. */
1090 stream_putw (s, RIP_FAMILY_AUTH);
1091 stream_putw (s, RIP_AUTH_DATA);
1092
1093 /* Generate a digest for the RIP packet. */
1094 memset(&ctx, 0, sizeof(ctx));
1095 MD5Init(&ctx);
1096 MD5Update(&ctx, STREAM_DATA (s), stream_get_endp (s));
1097 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
1098 MD5Final(digest, &ctx);
1099
1100 /* Copy the digest to the packet. */
1101 stream_write (s, digest, RIP_AUTH_MD5_SIZE);
1102 }
1103
1104 /* RIP routing information. */
1105 static void
1106 rip_response_process (struct rip_packet *packet, int size,
1107 struct sockaddr_in *from, struct connected *ifc)
1108 {
1109 caddr_t lim;
1110 struct rte *rte;
1111 struct prefix_ipv4 ifaddr;
1112 struct prefix_ipv4 ifaddrclass;
1113 int subnetted;
1114
1115 memset(&ifaddr, 0, sizeof(ifaddr));
1116 /* We don't know yet. */
1117 subnetted = -1;
1118
1119 /* The Response must be ignored if it is not from the RIP
1120 port. (RFC2453 - Sec. 3.9.2)*/
1121 if (from->sin_port != htons(RIP_PORT_DEFAULT))
1122 {
1123 zlog_info ("response doesn't come from RIP port: %d",
1124 from->sin_port);
1125 rip_peer_bad_packet (from);
1126 return;
1127 }
1128
1129 /* The datagram's IPv4 source address should be checked to see
1130 whether the datagram is from a valid neighbor; the source of the
1131 datagram must be on a directly connected network (RFC2453 - Sec. 3.9.2) */
1132 if (if_lookup_address((void *)&from->sin_addr, AF_INET, VRF_DEFAULT) == NULL)
1133 {
1134 zlog_info ("This datagram doesn't came from a valid neighbor: %s",
1135 inet_ntoa (from->sin_addr));
1136 rip_peer_bad_packet (from);
1137 return;
1138 }
1139
1140 /* It is also worth checking to see whether the response is from one
1141 of the router's own addresses. */
1142
1143 ; /* Alredy done in rip_read () */
1144
1145 /* Update RIP peer. */
1146 rip_peer_update (from, packet->version);
1147
1148 /* Set RTE pointer. */
1149 rte = packet->rte;
1150
1151 for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
1152 {
1153 /* RIPv2 authentication check. */
1154 /* If the Address Family Identifier of the first (and only the
1155 first) entry in the message is 0xFFFF, then the remainder of
1156 the entry contains the authentication. */
1157 /* If the packet gets here it means authentication enabled */
1158 /* Check is done in rip_read(). So, just skipping it */
1159 if (packet->version == RIPv2 &&
1160 rte == packet->rte &&
1161 rte->family == htons(RIP_FAMILY_AUTH))
1162 continue;
1163
1164 if (rte->family != htons(AF_INET))
1165 {
1166 /* Address family check. RIP only supports AF_INET. */
1167 zlog_info ("Unsupported family %d from %s.",
1168 ntohs (rte->family), inet_ntoa (from->sin_addr));
1169 continue;
1170 }
1171
1172 /* - is the destination address valid (e.g., unicast; not net 0
1173 or 127) */
1174 if (! rip_destination_check (rte->prefix))
1175 {
1176 zlog_info ("Network is net 0 or net 127 or it is not unicast network");
1177 rip_peer_bad_route (from);
1178 continue;
1179 }
1180
1181 /* Convert metric value to host byte order. */
1182 rte->metric = ntohl (rte->metric);
1183
1184 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1185 if (! (rte->metric >= 1 && rte->metric <= 16))
1186 {
1187 zlog_info ("Route's metric is not in the 1-16 range.");
1188 rip_peer_bad_route (from);
1189 continue;
1190 }
1191
1192 /* RIPv1 does not have nexthop value. */
1193 if (packet->version == RIPv1 && rte->nexthop.s_addr != 0)
1194 {
1195 zlog_info ("RIPv1 packet with nexthop value %s",
1196 inet_ntoa (rte->nexthop));
1197 rip_peer_bad_route (from);
1198 continue;
1199 }
1200
1201 /* That is, if the provided information is ignored, a possibly
1202 sub-optimal, but absolutely valid, route may be taken. If
1203 the received Next Hop is not directly reachable, it should be
1204 treated as 0.0.0.0. */
1205 if (packet->version == RIPv2 && rte->nexthop.s_addr != 0)
1206 {
1207 u_int32_t addrval;
1208
1209 /* Multicast address check. */
1210 addrval = ntohl (rte->nexthop.s_addr);
1211 if (IN_CLASSD (addrval))
1212 {
1213 zlog_info ("Nexthop %s is multicast address, skip this rte",
1214 inet_ntoa (rte->nexthop));
1215 continue;
1216 }
1217
1218 if (! if_lookup_address ((void *)&rte->nexthop, AF_INET, VRF_DEFAULT))
1219 {
1220 struct route_node *rn;
1221 struct rip_info *rinfo;
1222
1223 rn = route_node_match_ipv4 (rip->table, &rte->nexthop);
1224
1225 if (rn)
1226 {
1227 rinfo = rn->info;
1228
1229 if (rinfo->type == ZEBRA_ROUTE_RIP
1230 && rinfo->sub_type == RIP_ROUTE_RTE)
1231 {
1232 if (IS_RIP_DEBUG_EVENT)
1233 zlog_debug ("Next hop %s is on RIP network. Set nexthop to the packet's originator", inet_ntoa (rte->nexthop));
1234 rte->nexthop = rinfo->from;
1235 }
1236 else
1237 {
1238 if (IS_RIP_DEBUG_EVENT)
1239 zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
1240 rte->nexthop.s_addr = 0;
1241 }
1242
1243 route_unlock_node (rn);
1244 }
1245 else
1246 {
1247 if (IS_RIP_DEBUG_EVENT)
1248 zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
1249 rte->nexthop.s_addr = 0;
1250 }
1251
1252 }
1253 }
1254
1255 /* For RIPv1, there won't be a valid netmask.
1256
1257 This is a best guess at the masks. If everyone was using old
1258 Ciscos before the 'ip subnet zero' option, it would be almost
1259 right too :-)
1260
1261 Cisco summarize ripv1 advertisments to the classful boundary
1262 (/16 for class B's) except when the RIP packet does to inside
1263 the classful network in question. */
1264
1265 if ((packet->version == RIPv1 && rte->prefix.s_addr != 0)
1266 || (packet->version == RIPv2
1267 && (rte->prefix.s_addr != 0 && rte->mask.s_addr == 0)))
1268 {
1269 u_int32_t destination;
1270
1271 if (subnetted == -1)
1272 {
1273 memcpy (&ifaddr, ifc->address, sizeof (struct prefix_ipv4));
1274 memcpy (&ifaddrclass, &ifaddr, sizeof (struct prefix_ipv4));
1275 apply_classful_mask_ipv4 (&ifaddrclass);
1276 subnetted = 0;
1277 if (ifaddr.prefixlen > ifaddrclass.prefixlen)
1278 subnetted = 1;
1279 }
1280
1281 destination = ntohl (rte->prefix.s_addr);
1282
1283 if (IN_CLASSA (destination))
1284 masklen2ip (8, &rte->mask);
1285 else if (IN_CLASSB (destination))
1286 masklen2ip (16, &rte->mask);
1287 else if (IN_CLASSC (destination))
1288 masklen2ip (24, &rte->mask);
1289
1290 if (subnetted == 1)
1291 masklen2ip (ifaddrclass.prefixlen,
1292 (struct in_addr *) &destination);
1293 if ((subnetted == 1) && ((rte->prefix.s_addr & destination) ==
1294 ifaddrclass.prefix.s_addr))
1295 {
1296 masklen2ip (ifaddr.prefixlen, &rte->mask);
1297 if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
1298 masklen2ip (32, &rte->mask);
1299 if (IS_RIP_DEBUG_EVENT)
1300 zlog_debug ("Subnetted route %s", inet_ntoa (rte->prefix));
1301 }
1302 else
1303 {
1304 if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
1305 continue;
1306 }
1307
1308 if (IS_RIP_DEBUG_EVENT)
1309 {
1310 zlog_debug ("Resultant route %s", inet_ntoa (rte->prefix));
1311 zlog_debug ("Resultant mask %s", inet_ntoa (rte->mask));
1312 }
1313 }
1314
1315 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1316 ignore the entry. */
1317 if ((packet->version == RIPv2)
1318 && (rte->mask.s_addr != 0)
1319 && ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr))
1320 {
1321 zlog_warn ("RIPv2 address %s is not mask /%d applied one",
1322 inet_ntoa (rte->prefix), ip_masklen (rte->mask));
1323 rip_peer_bad_route (from);
1324 continue;
1325 }
1326
1327 /* Default route's netmask is ignored. */
1328 if (packet->version == RIPv2
1329 && (rte->prefix.s_addr == 0)
1330 && (rte->mask.s_addr != 0))
1331 {
1332 if (IS_RIP_DEBUG_EVENT)
1333 zlog_debug ("Default route with non-zero netmask. Set zero to netmask");
1334 rte->mask.s_addr = 0;
1335 }
1336
1337 /* Routing table updates. */
1338 rip_rte_process (rte, from, ifc->ifp);
1339 }
1340 }
1341
1342 /* Make socket for RIP protocol. */
1343 static int
1344 rip_create_socket (void)
1345 {
1346 int ret;
1347 int sock;
1348 struct sockaddr_in addr;
1349
1350 memset (&addr, 0, sizeof (struct sockaddr_in));
1351 addr.sin_family = AF_INET;
1352 addr.sin_addr.s_addr = INADDR_ANY;
1353 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1354 addr.sin_len = sizeof (struct sockaddr_in);
1355 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1356 /* sending port must always be the RIP port */
1357 addr.sin_port = htons (RIP_PORT_DEFAULT);
1358
1359 /* Make datagram socket. */
1360 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1361 if (sock < 0)
1362 {
1363 zlog_err("Cannot create UDP socket: %s", safe_strerror(errno));
1364 exit (1);
1365 }
1366
1367 sockopt_broadcast (sock);
1368 sockopt_reuseaddr (sock);
1369 sockopt_reuseport (sock);
1370 setsockopt_ipv4_multicast_loop (sock, 0);
1371 #ifdef RIP_RECVMSG
1372 setsockopt_pktinfo (sock);
1373 #endif /* RIP_RECVMSG */
1374 #ifdef IPTOS_PREC_INTERNETCONTROL
1375 setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
1376 #endif
1377
1378 if (ripd_privs.change (ZPRIVS_RAISE))
1379 zlog_err ("rip_create_socket: could not raise privs");
1380 setsockopt_so_recvbuf (sock, RIP_UDP_RCV_BUF);
1381 if ( (ret = bind (sock, (struct sockaddr *) & addr, sizeof (addr))) < 0)
1382
1383 {
1384 int save_errno = errno;
1385 if (ripd_privs.change (ZPRIVS_LOWER))
1386 zlog_err ("rip_create_socket: could not lower privs");
1387
1388 zlog_err("%s: Can't bind socket %d to %s port %d: %s", __func__,
1389 sock, inet_ntoa(addr.sin_addr),
1390 (int) ntohs(addr.sin_port),
1391 safe_strerror(save_errno));
1392
1393 close (sock);
1394 return ret;
1395 }
1396
1397 if (ripd_privs.change (ZPRIVS_LOWER))
1398 zlog_err ("rip_create_socket: could not lower privs");
1399
1400 return sock;
1401 }
1402
1403 /* RIP packet send to destination address, on interface denoted by
1404 * by connected argument. NULL to argument denotes destination should be
1405 * should be RIP multicast group
1406 */
1407 static int
1408 rip_send_packet (u_char * buf, int size, struct sockaddr_in *to,
1409 struct connected *ifc)
1410 {
1411 int ret;
1412 struct sockaddr_in sin;
1413
1414 assert (ifc != NULL);
1415
1416 if (IS_RIP_DEBUG_PACKET)
1417 {
1418 #define ADDRESS_SIZE 20
1419 char dst[ADDRESS_SIZE];
1420 dst[ADDRESS_SIZE - 1] = '\0';
1421
1422 if (to)
1423 {
1424 strncpy (dst, inet_ntoa(to->sin_addr), ADDRESS_SIZE - 1);
1425 }
1426 else
1427 {
1428 sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
1429 strncpy (dst, inet_ntoa(sin.sin_addr), ADDRESS_SIZE - 1);
1430 }
1431 #undef ADDRESS_SIZE
1432 zlog_debug("rip_send_packet %s > %s (%s)",
1433 inet_ntoa(ifc->address->u.prefix4),
1434 dst, ifc->ifp->name);
1435 }
1436
1437 if ( CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY) )
1438 {
1439 /*
1440 * ZEBRA_IFA_SECONDARY is set on linux when an interface is configured
1441 * with multiple addresses on the same subnet: the first address
1442 * on the subnet is configured "primary", and all subsequent addresses
1443 * on that subnet are treated as "secondary" addresses.
1444 * In order to avoid routing-table bloat on other rip listeners,
1445 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1446 * XXX Since Linux is the only system for which the ZEBRA_IFA_SECONDARY
1447 * flag is set, we would end up sending a packet for a "secondary"
1448 * source address on non-linux systems.
1449 */
1450 if (IS_RIP_DEBUG_PACKET)
1451 zlog_debug("duplicate dropped");
1452 return 0;
1453 }
1454
1455 /* Make destination address. */
1456 memset (&sin, 0, sizeof (struct sockaddr_in));
1457 sin.sin_family = AF_INET;
1458 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1459 sin.sin_len = sizeof (struct sockaddr_in);
1460 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1461
1462 /* When destination is specified, use it's port and address. */
1463 if (to)
1464 {
1465 sin.sin_port = to->sin_port;
1466 sin.sin_addr = to->sin_addr;
1467 }
1468 else
1469 {
1470 sin.sin_port = htons (RIP_PORT_DEFAULT);
1471 sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
1472
1473 rip_interface_multicast_set (rip->sock, ifc);
1474 }
1475
1476 ret = sendto (rip->sock, buf, size, 0, (struct sockaddr *)&sin,
1477 sizeof (struct sockaddr_in));
1478
1479 if (IS_RIP_DEBUG_EVENT)
1480 zlog_debug ("SEND to %s.%d", inet_ntoa(sin.sin_addr),
1481 ntohs (sin.sin_port));
1482
1483 if (ret < 0)
1484 zlog_warn ("can't send packet : %s", safe_strerror (errno));
1485
1486 return ret;
1487 }
1488
1489 /* Add redistributed route to RIP table. */
1490 void
1491 rip_redistribute_add (int type, int sub_type, struct prefix_ipv4 *p,
1492 ifindex_t ifindex, struct in_addr *nexthop,
1493 unsigned int metric, unsigned char distance,
1494 route_tag_t tag)
1495 {
1496 int ret;
1497 struct route_node *rp = NULL;
1498 struct rip_info *rinfo = NULL, newinfo;
1499 struct list *list = NULL;
1500
1501 /* Redistribute route */
1502 ret = rip_destination_check (p->prefix);
1503 if (! ret)
1504 return;
1505
1506 rp = route_node_get (rip->table, (struct prefix *) p);
1507
1508 memset (&newinfo, 0, sizeof (struct rip_info));
1509 newinfo.type = type;
1510 newinfo.sub_type = sub_type;
1511 newinfo.ifindex = ifindex;
1512 newinfo.metric = 1;
1513 newinfo.external_metric = metric;
1514 newinfo.distance = distance;
1515 if (tag <= UINT16_MAX) /* RIP only supports 16 bit tags */
1516 newinfo.tag = tag;
1517 newinfo.rp = rp;
1518 if (nexthop)
1519 newinfo.nexthop = *nexthop;
1520
1521 if ((list = rp->info) != NULL && listcount (list) != 0)
1522 {
1523 rinfo = listgetdata (listhead (list));
1524
1525 if (rinfo->type == ZEBRA_ROUTE_CONNECT
1526 && rinfo->sub_type == RIP_ROUTE_INTERFACE
1527 && rinfo->metric != RIP_METRIC_INFINITY)
1528 {
1529 route_unlock_node (rp);
1530 return;
1531 }
1532
1533 /* Manually configured RIP route check. */
1534 if (rinfo->type == ZEBRA_ROUTE_RIP
1535 && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
1536 (rinfo->sub_type == RIP_ROUTE_DEFAULT)) )
1537 {
1538 if (type != ZEBRA_ROUTE_RIP || ((sub_type != RIP_ROUTE_STATIC) &&
1539 (sub_type != RIP_ROUTE_DEFAULT)))
1540 {
1541 route_unlock_node (rp);
1542 return;
1543 }
1544 }
1545
1546 rinfo = rip_ecmp_replace (&newinfo);
1547 route_unlock_node (rp);
1548 }
1549 else
1550 rinfo = rip_ecmp_add (&newinfo);
1551
1552 if (IS_RIP_DEBUG_EVENT) {
1553 if (!nexthop)
1554 zlog_debug ("Redistribute new prefix %s/%d on the interface %s",
1555 inet_ntoa(p->prefix), p->prefixlen,
1556 ifindex2ifname(ifindex, VRF_DEFAULT));
1557 else
1558 zlog_debug ("Redistribute new prefix %s/%d with nexthop %s on the interface %s",
1559 inet_ntoa(p->prefix), p->prefixlen, inet_ntoa(rinfo->nexthop),
1560 ifindex2ifname(ifindex, VRF_DEFAULT));
1561 }
1562
1563 rip_event (RIP_TRIGGERED_UPDATE, 0);
1564 }
1565
1566 /* Delete redistributed route from RIP table. */
1567 void
1568 rip_redistribute_delete (int type, int sub_type, struct prefix_ipv4 *p,
1569 ifindex_t ifindex)
1570 {
1571 int ret;
1572 struct route_node *rp;
1573 struct rip_info *rinfo;
1574
1575 ret = rip_destination_check (p->prefix);
1576 if (! ret)
1577 return;
1578
1579 rp = route_node_lookup (rip->table, (struct prefix *) p);
1580 if (rp)
1581 {
1582 struct list *list = rp->info;
1583
1584 if (list != NULL && listcount (list) != 0)
1585 {
1586 rinfo = listgetdata (listhead (list));
1587 if (rinfo != NULL
1588 && rinfo->type == type
1589 && rinfo->sub_type == sub_type
1590 && rinfo->ifindex == ifindex)
1591 {
1592 /* Perform poisoned reverse. */
1593 rinfo->metric = RIP_METRIC_INFINITY;
1594 RIP_TIMER_ON (rinfo->t_garbage_collect,
1595 rip_garbage_collect, rip->garbage_time);
1596 RIP_TIMER_OFF (rinfo->t_timeout);
1597 rinfo->flags |= RIP_RTF_CHANGED;
1598
1599 if (IS_RIP_DEBUG_EVENT)
1600 zlog_debug ("Poisone %s/%d on the interface %s with an "
1601 "infinity metric [delete]",
1602 inet_ntoa(p->prefix), p->prefixlen,
1603 ifindex2ifname(ifindex, VRF_DEFAULT));
1604
1605 rip_event (RIP_TRIGGERED_UPDATE, 0);
1606 }
1607 }
1608 route_unlock_node (rp);
1609 }
1610 }
1611
1612 /* Response to request called from rip_read ().*/
1613 static void
1614 rip_request_process (struct rip_packet *packet, int size,
1615 struct sockaddr_in *from, struct connected *ifc)
1616 {
1617 caddr_t lim;
1618 struct rte *rte;
1619 struct prefix_ipv4 p;
1620 struct route_node *rp;
1621 struct rip_info *rinfo;
1622 struct rip_interface *ri;
1623
1624 /* Does not reponse to the requests on the loopback interfaces */
1625 if (if_is_loopback (ifc->ifp))
1626 return;
1627
1628 /* Check RIP process is enabled on this interface. */
1629 ri = ifc->ifp->info;
1630 if (! ri->running)
1631 return;
1632
1633 /* When passive interface is specified, suppress responses */
1634 if (ri->passive)
1635 return;
1636
1637 /* RIP peer update. */
1638 rip_peer_update (from, packet->version);
1639
1640 lim = ((caddr_t) packet) + size;
1641 rte = packet->rte;
1642
1643 /* The Request is processed entry by entry. If there are no
1644 entries, no response is given. */
1645 if (lim == (caddr_t) rte)
1646 return;
1647
1648 /* There is one special case. If there is exactly one entry in the
1649 request, and it has an address family identifier of zero and a
1650 metric of infinity (i.e., 16), then this is a request to send the
1651 entire routing table. */
1652 if (lim == ((caddr_t) (rte + 1)) &&
1653 ntohs (rte->family) == 0 &&
1654 ntohl (rte->metric) == RIP_METRIC_INFINITY)
1655 {
1656 /* All route with split horizon */
1657 rip_output_process (ifc, from, rip_all_route, packet->version);
1658 }
1659 else
1660 {
1661 if (ntohs (rte->family) != AF_INET)
1662 return;
1663
1664 /* Examine the list of RTEs in the Request one by one. For each
1665 entry, look up the destination in the router's routing
1666 database and, if there is a route, put that route's metric in
1667 the metric field of the RTE. If there is no explicit route
1668 to the specified destination, put infinity in the metric
1669 field. Once all the entries have been filled in, change the
1670 command from Request to Response and send the datagram back
1671 to the requestor. */
1672 p.family = AF_INET;
1673
1674 for (; ((caddr_t) rte) < lim; rte++)
1675 {
1676 p.prefix = rte->prefix;
1677 p.prefixlen = ip_masklen (rte->mask);
1678 apply_mask_ipv4 (&p);
1679
1680 rp = route_node_lookup (rip->table, (struct prefix *) &p);
1681 if (rp)
1682 {
1683 rinfo = listgetdata (listhead ((struct list *)rp->info));
1684 rte->metric = htonl (rinfo->metric);
1685 route_unlock_node (rp);
1686 }
1687 else
1688 rte->metric = htonl (RIP_METRIC_INFINITY);
1689 }
1690 packet->command = RIP_RESPONSE;
1691
1692 rip_send_packet ((u_char *)packet, size, from, ifc);
1693 }
1694 rip_global_queries++;
1695 }
1696
1697 #if RIP_RECVMSG
1698 /* Set IPv6 packet info to the socket. */
1699 static int
1700 setsockopt_pktinfo (int sock)
1701 {
1702 int ret;
1703 int val = 1;
1704
1705 ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
1706 if (ret < 0)
1707 zlog_warn ("Can't setsockopt IP_PKTINFO : %s", safe_strerror (errno));
1708 return ret;
1709 }
1710
1711 /* Read RIP packet by recvmsg function. */
1712 int
1713 rip_recvmsg (int sock, u_char *buf, int size, struct sockaddr_in *from,
1714 ifindex_t *ifindex)
1715 {
1716 int ret;
1717 struct msghdr msg;
1718 struct iovec iov;
1719 struct cmsghdr *ptr;
1720 char adata[1024];
1721
1722 msg.msg_name = (void *) from;
1723 msg.msg_namelen = sizeof (struct sockaddr_in);
1724 msg.msg_iov = &iov;
1725 msg.msg_iovlen = 1;
1726 msg.msg_control = (void *) adata;
1727 msg.msg_controllen = sizeof adata;
1728 iov.iov_base = buf;
1729 iov.iov_len = size;
1730
1731 ret = recvmsg (sock, &msg, 0);
1732 if (ret < 0)
1733 return ret;
1734
1735 for (ptr = ZCMSG_FIRSTHDR(&msg); ptr != NULL; ptr = CMSG_NXTHDR(&msg, ptr))
1736 if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO)
1737 {
1738 struct in_pktinfo *pktinfo;
1739 int i;
1740
1741 pktinfo = (struct in_pktinfo *) CMSG_DATA (ptr);
1742 i = pktinfo->ipi_ifindex;
1743 }
1744 return ret;
1745 }
1746
1747 /* RIP packet read function. */
1748 int
1749 rip_read_new (struct thread *t)
1750 {
1751 int ret;
1752 int sock;
1753 char buf[RIP_PACKET_MAXSIZ];
1754 struct sockaddr_in from;
1755 ifindex_t ifindex;
1756
1757 /* Fetch socket then register myself. */
1758 sock = THREAD_FD (t);
1759 rip_event (RIP_READ, sock);
1760
1761 /* Read RIP packet. */
1762 ret = rip_recvmsg (sock, buf, RIP_PACKET_MAXSIZ, &from, (int *)&ifindex);
1763 if (ret < 0)
1764 {
1765 zlog_warn ("Can't read RIP packet: %s", safe_strerror (errno));
1766 return ret;
1767 }
1768
1769 return ret;
1770 }
1771 #endif /* RIP_RECVMSG */
1772
1773 /* First entry point of RIP packet. */
1774 static int
1775 rip_read (struct thread *t)
1776 {
1777 int sock;
1778 int ret;
1779 int rtenum;
1780 union rip_buf rip_buf;
1781 struct rip_packet *packet;
1782 struct sockaddr_in from;
1783 int len;
1784 int vrecv;
1785 socklen_t fromlen;
1786 struct interface *ifp = NULL;
1787 struct connected *ifc;
1788 struct rip_interface *ri;
1789 struct prefix p;
1790
1791 /* Fetch socket then register myself. */
1792 sock = THREAD_FD (t);
1793 rip->t_read = NULL;
1794
1795 /* Add myself to tne next event */
1796 rip_event (RIP_READ, sock);
1797
1798 /* RIPd manages only IPv4. */
1799 memset (&from, 0, sizeof (struct sockaddr_in));
1800 fromlen = sizeof (struct sockaddr_in);
1801
1802 len = recvfrom (sock, (char *)&rip_buf.buf, sizeof (rip_buf.buf), 0,
1803 (struct sockaddr *) &from, &fromlen);
1804 if (len < 0)
1805 {
1806 zlog_info ("recvfrom failed: %s", safe_strerror (errno));
1807 return len;
1808 }
1809
1810 /* Check is this packet comming from myself? */
1811 if (if_check_address (from.sin_addr))
1812 {
1813 if (IS_RIP_DEBUG_PACKET)
1814 zlog_debug ("ignore packet comes from myself");
1815 return -1;
1816 }
1817
1818 /* Which interface is this packet comes from. */
1819 ifc = if_lookup_address ((void *)&from.sin_addr, AF_INET, VRF_DEFAULT);
1820 if (ifc)
1821 ifp = ifc->ifp;
1822
1823 /* RIP packet received */
1824 if (IS_RIP_DEBUG_EVENT)
1825 zlog_debug ("RECV packet from %s port %d on %s",
1826 inet_ntoa (from.sin_addr), ntohs (from.sin_port),
1827 ifp ? ifp->name : "unknown");
1828
1829 /* If this packet come from unknown interface, ignore it. */
1830 if (ifp == NULL)
1831 {
1832 zlog_info ("rip_read: cannot find interface for packet from %s port %d",
1833 inet_ntoa(from.sin_addr), ntohs (from.sin_port));
1834 return -1;
1835 }
1836
1837 p.family = AF_INET;
1838 p.u.prefix4 = from.sin_addr;
1839 p.prefixlen = IPV4_MAX_BITLEN;
1840
1841 ifc = connected_lookup_prefix (ifp, &p);
1842
1843 if (ifc == NULL)
1844 {
1845 zlog_info ("rip_read: cannot find connected address for packet from %s "
1846 "port %d on interface %s",
1847 inet_ntoa(from.sin_addr), ntohs (from.sin_port), ifp->name);
1848 return -1;
1849 }
1850
1851 /* Packet length check. */
1852 if (len < RIP_PACKET_MINSIZ)
1853 {
1854 zlog_warn ("packet size %d is smaller than minimum size %d",
1855 len, RIP_PACKET_MINSIZ);
1856 rip_peer_bad_packet (&from);
1857 return len;
1858 }
1859 if (len > RIP_PACKET_MAXSIZ)
1860 {
1861 zlog_warn ("packet size %d is larger than max size %d",
1862 len, RIP_PACKET_MAXSIZ);
1863 rip_peer_bad_packet (&from);
1864 return len;
1865 }
1866
1867 /* Packet alignment check. */
1868 if ((len - RIP_PACKET_MINSIZ) % 20)
1869 {
1870 zlog_warn ("packet size %d is wrong for RIP packet alignment", len);
1871 rip_peer_bad_packet (&from);
1872 return len;
1873 }
1874
1875 /* Set RTE number. */
1876 rtenum = ((len - RIP_PACKET_MINSIZ) / 20);
1877
1878 /* For easy to handle. */
1879 packet = &rip_buf.rip_packet;
1880
1881 /* RIP version check. */
1882 if (packet->version == 0)
1883 {
1884 zlog_info ("version 0 with command %d received.", packet->command);
1885 rip_peer_bad_packet (&from);
1886 return -1;
1887 }
1888
1889 /* Dump RIP packet. */
1890 if (IS_RIP_DEBUG_RECV)
1891 rip_packet_dump (packet, len, "RECV");
1892
1893 /* RIP version adjust. This code should rethink now. RFC1058 says
1894 that "Version 1 implementations are to ignore this extra data and
1895 process only the fields specified in this document.". So RIPv3
1896 packet should be treated as RIPv1 ignoring must be zero field. */
1897 if (packet->version > RIPv2)
1898 packet->version = RIPv2;
1899
1900 /* Is RIP running or is this RIP neighbor ?*/
1901 ri = ifp->info;
1902 if (! ri->running && ! rip_neighbor_lookup (&from))
1903 {
1904 if (IS_RIP_DEBUG_EVENT)
1905 zlog_debug ("RIP is not enabled on interface %s.", ifp->name);
1906 rip_peer_bad_packet (&from);
1907 return -1;
1908 }
1909
1910 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1911 vrecv = ((ri->ri_receive == RI_RIP_UNSPEC) ?
1912 rip->version_recv : ri->ri_receive);
1913 if (vrecv == RI_RIP_VERSION_NONE ||
1914 ((packet->version == RIPv1) && !(vrecv & RIPv1)) ||
1915 ((packet->version == RIPv2) && !(vrecv & RIPv2)))
1916 {
1917 if (IS_RIP_DEBUG_PACKET)
1918 zlog_debug (" packet's v%d doesn't fit to if version spec",
1919 packet->version);
1920 rip_peer_bad_packet (&from);
1921 return -1;
1922 }
1923
1924 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1925 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1926 accepted; authenticated RIP-2 messages shall be discarded. */
1927 if ((ri->auth_type == RIP_NO_AUTH)
1928 && rtenum
1929 && (packet->version == RIPv2)
1930 && (packet->rte->family == htons(RIP_FAMILY_AUTH)))
1931 {
1932 if (IS_RIP_DEBUG_EVENT)
1933 zlog_debug ("packet RIPv%d is dropped because authentication disabled",
1934 packet->version);
1935 rip_peer_bad_packet (&from);
1936 return -1;
1937 }
1938
1939 /* RFC:
1940 If the router is configured to authenticate RIP-2 messages, then
1941 RIP-1 messages and RIP-2 messages which pass authentication
1942 testing shall be accepted; unauthenticated and failed
1943 authentication RIP-2 messages shall be discarded. For maximum
1944 security, RIP-1 messages should be ignored when authentication is
1945 in use (see section 4.1); otherwise, the routing information from
1946 authenticated messages will be propagated by RIP-1 routers in an
1947 unauthenticated manner.
1948 */
1949 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1950 * always reply regardless of authentication settings, because:
1951 *
1952 * - if there other authorised routers on-link, the REQUESTor can
1953 * passively obtain the routing updates anyway
1954 * - if there are no other authorised routers on-link, RIP can
1955 * easily be disabled for the link to prevent giving out information
1956 * on state of this routers RIP routing table..
1957 *
1958 * I.e. if RIPv1 has any place anymore these days, it's as a very
1959 * simple way to distribute routing information (e.g. to embedded
1960 * hosts / appliances) and the ability to give out RIPv1
1961 * routing-information freely, while still requiring RIPv2
1962 * authentication for any RESPONSEs might be vaguely useful.
1963 */
1964 if (ri->auth_type != RIP_NO_AUTH
1965 && packet->version == RIPv1)
1966 {
1967 /* Discard RIPv1 messages other than REQUESTs */
1968 if (packet->command != RIP_REQUEST)
1969 {
1970 if (IS_RIP_DEBUG_PACKET)
1971 zlog_debug ("RIPv1" " dropped because authentication enabled");
1972 rip_peer_bad_packet (&from);
1973 return -1;
1974 }
1975 }
1976 else if (ri->auth_type != RIP_NO_AUTH)
1977 {
1978 const char *auth_desc;
1979
1980 if (rtenum == 0)
1981 {
1982 /* There definitely is no authentication in the packet. */
1983 if (IS_RIP_DEBUG_PACKET)
1984 zlog_debug ("RIPv2 authentication failed: no auth RTE in packet");
1985 rip_peer_bad_packet (&from);
1986 return -1;
1987 }
1988
1989 /* First RTE must be an Authentication Family RTE */
1990 if (packet->rte->family != htons(RIP_FAMILY_AUTH))
1991 {
1992 if (IS_RIP_DEBUG_PACKET)
1993 zlog_debug ("RIPv2" " dropped because authentication enabled");
1994 rip_peer_bad_packet (&from);
1995 return -1;
1996 }
1997
1998 /* Check RIPv2 authentication. */
1999 switch (ntohs(packet->rte->tag))
2000 {
2001 case RIP_AUTH_SIMPLE_PASSWORD:
2002 auth_desc = "simple";
2003 ret = rip_auth_simple_password (packet->rte, &from, ifp);
2004 break;
2005
2006 case RIP_AUTH_MD5:
2007 auth_desc = "MD5";
2008 ret = rip_auth_md5 (packet, &from, len, ifp);
2009 /* Reset RIP packet length to trim MD5 data. */
2010 len = ret;
2011 break;
2012
2013 default:
2014 ret = 0;
2015 auth_desc = "unknown type";
2016 if (IS_RIP_DEBUG_PACKET)
2017 zlog_debug ("RIPv2 Unknown authentication type %d",
2018 ntohs (packet->rte->tag));
2019 }
2020
2021 if (ret)
2022 {
2023 if (IS_RIP_DEBUG_PACKET)
2024 zlog_debug ("RIPv2 %s authentication success", auth_desc);
2025 }
2026 else
2027 {
2028 if (IS_RIP_DEBUG_PACKET)
2029 zlog_debug ("RIPv2 %s authentication failure", auth_desc);
2030 rip_peer_bad_packet (&from);
2031 return -1;
2032 }
2033 }
2034
2035 /* Process each command. */
2036 switch (packet->command)
2037 {
2038 case RIP_RESPONSE:
2039 rip_response_process (packet, len, &from, ifc);
2040 break;
2041 case RIP_REQUEST:
2042 case RIP_POLL:
2043 rip_request_process (packet, len, &from, ifc);
2044 break;
2045 case RIP_TRACEON:
2046 case RIP_TRACEOFF:
2047 zlog_info ("Obsolete command %s received, please sent it to routed",
2048 lookup (rip_msg, packet->command));
2049 rip_peer_bad_packet (&from);
2050 break;
2051 case RIP_POLL_ENTRY:
2052 zlog_info ("Obsolete command %s received",
2053 lookup (rip_msg, packet->command));
2054 rip_peer_bad_packet (&from);
2055 break;
2056 default:
2057 zlog_info ("Unknown RIP command %d received", packet->command);
2058 rip_peer_bad_packet (&from);
2059 break;
2060 }
2061
2062 return len;
2063 }
2064
2065 /* Write routing table entry to the stream and return next index of
2066 the routing table entry in the stream. */
2067 static int
2068 rip_write_rte (int num, struct stream *s, struct prefix_ipv4 *p,
2069 u_char version, struct rip_info *rinfo)
2070 {
2071 struct in_addr mask;
2072
2073 /* Write routing table entry. */
2074 if (version == RIPv1)
2075 {
2076 stream_putw (s, AF_INET);
2077 stream_putw (s, 0);
2078 stream_put_ipv4 (s, p->prefix.s_addr);
2079 stream_put_ipv4 (s, 0);
2080 stream_put_ipv4 (s, 0);
2081 stream_putl (s, rinfo->metric_out);
2082 }
2083 else
2084 {
2085 masklen2ip (p->prefixlen, &mask);
2086
2087 stream_putw (s, AF_INET);
2088 stream_putw (s, rinfo->tag_out);
2089 stream_put_ipv4 (s, p->prefix.s_addr);
2090 stream_put_ipv4 (s, mask.s_addr);
2091 stream_put_ipv4 (s, rinfo->nexthop_out.s_addr);
2092 stream_putl (s, rinfo->metric_out);
2093 }
2094
2095 return ++num;
2096 }
2097
2098 /* Send update to the ifp or spcified neighbor. */
2099 void
2100 rip_output_process (struct connected *ifc, struct sockaddr_in *to,
2101 int route_type, u_char version)
2102 {
2103 int ret;
2104 struct stream *s;
2105 struct route_node *rp;
2106 struct rip_info *rinfo;
2107 struct rip_interface *ri;
2108 struct prefix_ipv4 *p;
2109 struct prefix_ipv4 classfull;
2110 struct prefix_ipv4 ifaddrclass;
2111 struct key *key = NULL;
2112 /* this might need to made dynamic if RIP ever supported auth methods
2113 with larger key string sizes */
2114 char auth_str[RIP_AUTH_SIMPLE_SIZE];
2115 size_t doff = 0; /* offset of digest offset field */
2116 int num = 0;
2117 int rtemax;
2118 int subnetted = 0;
2119 struct list *list = NULL;
2120 struct listnode *listnode = NULL;
2121
2122 /* Logging output event. */
2123 if (IS_RIP_DEBUG_EVENT)
2124 {
2125 if (to)
2126 zlog_debug ("update routes to neighbor %s", inet_ntoa (to->sin_addr));
2127 else
2128 zlog_debug ("update routes on interface %s ifindex %d",
2129 ifc->ifp->name, ifc->ifp->ifindex);
2130 }
2131
2132 /* Set output stream. */
2133 s = rip->obuf;
2134
2135 /* Reset stream and RTE counter. */
2136 stream_reset (s);
2137 rtemax = RIP_MAX_RTE;
2138
2139 /* Get RIP interface. */
2140 ri = ifc->ifp->info;
2141
2142 /* If output interface is in simple password authentication mode, we
2143 need space for authentication data. */
2144 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
2145 rtemax -= 1;
2146
2147 /* If output interface is in MD5 authentication mode, we need space
2148 for authentication header and data. */
2149 if (ri->auth_type == RIP_AUTH_MD5)
2150 rtemax -= 2;
2151
2152 /* If output interface is in simple password authentication mode
2153 and string or keychain is specified we need space for auth. data */
2154 if (ri->auth_type != RIP_NO_AUTH)
2155 {
2156 if (ri->key_chain)
2157 {
2158 struct keychain *keychain;
2159
2160 keychain = keychain_lookup (ri->key_chain);
2161 if (keychain)
2162 key = key_lookup_for_send (keychain);
2163 }
2164 /* to be passed to auth functions later */
2165 rip_auth_prepare_str_send (ri, key, auth_str, RIP_AUTH_SIMPLE_SIZE);
2166 }
2167
2168 if (version == RIPv1)
2169 {
2170 memcpy (&ifaddrclass, ifc->address, sizeof (struct prefix_ipv4));
2171 apply_classful_mask_ipv4 (&ifaddrclass);
2172 subnetted = 0;
2173 if (ifc->address->prefixlen > ifaddrclass.prefixlen)
2174 subnetted = 1;
2175 }
2176
2177 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2178 if ((list = rp->info) != NULL && listcount (list) != 0)
2179 {
2180 rinfo = listgetdata (listhead (list));
2181 /* For RIPv1, if we are subnetted, output subnets in our network */
2182 /* that have the same mask as the output "interface". For other */
2183 /* networks, only the classfull version is output. */
2184
2185 if (version == RIPv1)
2186 {
2187 p = (struct prefix_ipv4 *) &rp->p;
2188
2189 if (IS_RIP_DEBUG_PACKET)
2190 zlog_debug("RIPv1 mask check, %s/%d considered for output",
2191 inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
2192
2193 if (subnetted &&
2194 prefix_match ((struct prefix *) &ifaddrclass, &rp->p))
2195 {
2196 if ((ifc->address->prefixlen != rp->p.prefixlen) &&
2197 (rp->p.prefixlen != 32))
2198 continue;
2199 }
2200 else
2201 {
2202 memcpy (&classfull, &rp->p, sizeof(struct prefix_ipv4));
2203 apply_classful_mask_ipv4(&classfull);
2204 if (rp->p.u.prefix4.s_addr != 0 &&
2205 classfull.prefixlen != rp->p.prefixlen)
2206 continue;
2207 }
2208 if (IS_RIP_DEBUG_PACKET)
2209 zlog_debug("RIPv1 mask check, %s/%d made it through",
2210 inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
2211 }
2212 else
2213 p = (struct prefix_ipv4 *) &rp->p;
2214
2215 /* Apply output filters. */
2216 ret = rip_filter (RIP_FILTER_OUT, p, ri);
2217 if (ret < 0)
2218 continue;
2219
2220 /* Changed route only output. */
2221 if (route_type == rip_changed_route &&
2222 (! (rinfo->flags & RIP_RTF_CHANGED)))
2223 continue;
2224
2225 /* Split horizon. */
2226 /* if (split_horizon == rip_split_horizon) */
2227 if (ri->split_horizon == RIP_SPLIT_HORIZON)
2228 {
2229 /*
2230 * We perform split horizon for RIP and connected route.
2231 * For rip routes, we want to suppress the route if we would
2232 * end up sending the route back on the interface that we
2233 * learned it from, with a higher metric. For connected routes,
2234 * we suppress the route if the prefix is a subset of the
2235 * source address that we are going to use for the packet
2236 * (in order to handle the case when multiple subnets are
2237 * configured on the same interface).
2238 */
2239 int suppress = 0;
2240 struct rip_info *tmp_rinfo = NULL;
2241
2242 for (ALL_LIST_ELEMENTS_RO (list, listnode, tmp_rinfo))
2243 if (tmp_rinfo->type == ZEBRA_ROUTE_RIP &&
2244 tmp_rinfo->ifindex == ifc->ifp->ifindex)
2245 {
2246 suppress = 1;
2247 break;
2248 }
2249
2250 if (!suppress && rinfo->type == ZEBRA_ROUTE_CONNECT &&
2251 prefix_match((struct prefix *)p, ifc->address))
2252 suppress = 1;
2253
2254 if (suppress)
2255 continue;
2256 }
2257
2258 /* Preparation for route-map. */
2259 rinfo->metric_set = 0;
2260 rinfo->nexthop_out.s_addr = 0;
2261 rinfo->metric_out = rinfo->metric;
2262 rinfo->tag_out = rinfo->tag;
2263 rinfo->ifindex_out = ifc->ifp->ifindex;
2264
2265 /* In order to avoid some local loops,
2266 * if the RIP route has a nexthop via this interface, keep the nexthop,
2267 * otherwise set it to 0. The nexthop should not be propagated
2268 * beyond the local broadcast/multicast area in order
2269 * to avoid an IGP multi-level recursive look-up.
2270 * see (4.4)
2271 */
2272 if (rinfo->ifindex == ifc->ifp->ifindex)
2273 rinfo->nexthop_out = rinfo->nexthop;
2274
2275 /* Interface route-map */
2276 if (ri->routemap[RIP_FILTER_OUT])
2277 {
2278 ret = route_map_apply (ri->routemap[RIP_FILTER_OUT],
2279 (struct prefix *) p, RMAP_RIP,
2280 rinfo);
2281
2282 if (ret == RMAP_DENYMATCH)
2283 {
2284 if (IS_RIP_DEBUG_PACKET)
2285 zlog_debug ("RIP %s/%d is filtered by route-map out",
2286 inet_ntoa (p->prefix), p->prefixlen);
2287 continue;
2288 }
2289 }
2290
2291 /* Apply redistribute route map - continue, if deny */
2292 if (rip->route_map[rinfo->type].name
2293 && rinfo->sub_type != RIP_ROUTE_INTERFACE)
2294 {
2295 ret = route_map_apply (rip->route_map[rinfo->type].map,
2296 (struct prefix *)p, RMAP_RIP, rinfo);
2297
2298 if (ret == RMAP_DENYMATCH)
2299 {
2300 if (IS_RIP_DEBUG_PACKET)
2301 zlog_debug ("%s/%d is filtered by route-map",
2302 inet_ntoa (p->prefix), p->prefixlen);
2303 continue;
2304 }
2305 }
2306
2307 /* When route-map does not set metric. */
2308 if (! rinfo->metric_set)
2309 {
2310 /* If redistribute metric is set. */
2311 if (rip->route_map[rinfo->type].metric_config
2312 && rinfo->metric != RIP_METRIC_INFINITY)
2313 {
2314 rinfo->metric_out = rip->route_map[rinfo->type].metric;
2315 }
2316 else
2317 {
2318 /* If the route is not connected or localy generated
2319 one, use default-metric value*/
2320 if (rinfo->type != ZEBRA_ROUTE_RIP
2321 && rinfo->type != ZEBRA_ROUTE_CONNECT
2322 && rinfo->metric != RIP_METRIC_INFINITY)
2323 rinfo->metric_out = rip->default_metric;
2324 }
2325 }
2326
2327 /* Apply offset-list */
2328 if (rinfo->metric != RIP_METRIC_INFINITY)
2329 rip_offset_list_apply_out (p, ifc->ifp, &rinfo->metric_out);
2330
2331 if (rinfo->metric_out > RIP_METRIC_INFINITY)
2332 rinfo->metric_out = RIP_METRIC_INFINITY;
2333
2334 /* Perform split-horizon with poisoned reverse
2335 * for RIP and connected routes.
2336 **/
2337 if (ri->split_horizon == RIP_SPLIT_HORIZON_POISONED_REVERSE) {
2338 /*
2339 * We perform split horizon for RIP and connected route.
2340 * For rip routes, we want to suppress the route if we would
2341 * end up sending the route back on the interface that we
2342 * learned it from, with a higher metric. For connected routes,
2343 * we suppress the route if the prefix is a subset of the
2344 * source address that we are going to use for the packet
2345 * (in order to handle the case when multiple subnets are
2346 * configured on the same interface).
2347 */
2348 struct rip_info *tmp_rinfo = NULL;
2349
2350 for (ALL_LIST_ELEMENTS_RO (list, listnode, tmp_rinfo))
2351 if (tmp_rinfo->type == ZEBRA_ROUTE_RIP &&
2352 tmp_rinfo->ifindex == ifc->ifp->ifindex)
2353 rinfo->metric_out = RIP_METRIC_INFINITY;
2354 if (tmp_rinfo->type == ZEBRA_ROUTE_CONNECT &&
2355 prefix_match((struct prefix *)p, ifc->address))
2356 rinfo->metric_out = RIP_METRIC_INFINITY;
2357 }
2358
2359 /* Prepare preamble, auth headers, if needs be */
2360 if (num == 0)
2361 {
2362 stream_putc (s, RIP_RESPONSE);
2363 stream_putc (s, version);
2364 stream_putw (s, 0);
2365
2366 /* auth header for !v1 && !no_auth */
2367 if ( (ri->auth_type != RIP_NO_AUTH) && (version != RIPv1) )
2368 doff = rip_auth_header_write (s, ri, key, auth_str,
2369 RIP_AUTH_SIMPLE_SIZE);
2370 }
2371
2372 /* Write RTE to the stream. */
2373 num = rip_write_rte (num, s, p, version, rinfo);
2374 if (num == rtemax)
2375 {
2376 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
2377 rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
2378
2379 ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s),
2380 to, ifc);
2381
2382 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2383 rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
2384 stream_get_endp(s), "SEND");
2385 num = 0;
2386 stream_reset (s);
2387 }
2388 }
2389
2390 /* Flush unwritten RTE. */
2391 if (num != 0)
2392 {
2393 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
2394 rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
2395
2396 ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s), to, ifc);
2397
2398 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2399 rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
2400 stream_get_endp (s), "SEND");
2401 stream_reset (s);
2402 }
2403
2404 /* Statistics updates. */
2405 ri->sent_updates++;
2406 }
2407
2408 /* Send RIP packet to the interface. */
2409 static void
2410 rip_update_interface (struct connected *ifc, u_char version, int route_type)
2411 {
2412 struct interface *ifp = ifc->ifp;
2413 struct rip_interface *ri = ifp->info;
2414 struct sockaddr_in to;
2415
2416 /* When RIP version is 2 and multicast enable interface. */
2417 if (version == RIPv2 && !ri->v2_broadcast && if_is_multicast (ifp))
2418 {
2419 if (IS_RIP_DEBUG_EVENT)
2420 zlog_debug ("multicast announce on %s ", ifp->name);
2421
2422 rip_output_process (ifc, NULL, route_type, version);
2423 return;
2424 }
2425
2426 /* If we can't send multicast packet, send it with unicast. */
2427 if (if_is_broadcast (ifp) || if_is_pointopoint (ifp))
2428 {
2429 if (ifc->address->family == AF_INET)
2430 {
2431 /* Destination address and port setting. */
2432 memset (&to, 0, sizeof (struct sockaddr_in));
2433 if (ifc->destination)
2434 /* use specified broadcast or peer destination addr */
2435 to.sin_addr = ifc->destination->u.prefix4;
2436 else if (ifc->address->prefixlen < IPV4_MAX_PREFIXLEN)
2437 /* calculate the appropriate broadcast address */
2438 to.sin_addr.s_addr =
2439 ipv4_broadcast_addr(ifc->address->u.prefix4.s_addr,
2440 ifc->address->prefixlen);
2441 else
2442 /* do not know where to send the packet */
2443 return;
2444 to.sin_port = htons (RIP_PORT_DEFAULT);
2445
2446 if (IS_RIP_DEBUG_EVENT)
2447 zlog_debug("%s announce to %s on %s",
2448 CONNECTED_PEER(ifc) ? "unicast" : "broadcast",
2449 inet_ntoa (to.sin_addr), ifp->name);
2450
2451 rip_output_process (ifc, &to, route_type, version);
2452 }
2453 }
2454 }
2455
2456 /* Update send to all interface and neighbor. */
2457 static void
2458 rip_update_process (int route_type)
2459 {
2460 struct listnode *node;
2461 struct listnode *ifnode, *ifnnode;
2462 struct connected *connected;
2463 struct interface *ifp;
2464 struct rip_interface *ri;
2465 struct route_node *rp;
2466 struct sockaddr_in to;
2467 struct prefix *p;
2468
2469 /* Send RIP update to each interface. */
2470 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
2471 {
2472 if (if_is_loopback (ifp))
2473 continue;
2474
2475 if (! if_is_operative (ifp))
2476 continue;
2477
2478 /* Fetch RIP interface information. */
2479 ri = ifp->info;
2480
2481 /* When passive interface is specified, suppress announce to the
2482 interface. */
2483 if (ri->passive)
2484 continue;
2485
2486 if (ri->running)
2487 {
2488 /*
2489 * If there is no version configuration in the interface,
2490 * use rip's version setting.
2491 */
2492 int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ?
2493 rip->version_send : ri->ri_send);
2494
2495 if (IS_RIP_DEBUG_EVENT)
2496 zlog_debug("SEND UPDATE to %s ifindex %d",
2497 ifp->name, ifp->ifindex);
2498
2499 /* send update on each connected network */
2500 for (ALL_LIST_ELEMENTS (ifp->connected, ifnode, ifnnode, connected))
2501 {
2502 if (connected->address->family == AF_INET)
2503 {
2504 if (vsend & RIPv1)
2505 rip_update_interface (connected, RIPv1, route_type);
2506 if ((vsend & RIPv2) && if_is_multicast(ifp))
2507 rip_update_interface (connected, RIPv2, route_type);
2508 }
2509 }
2510 }
2511 }
2512
2513 /* RIP send updates to each neighbor. */
2514 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
2515 if (rp->info != NULL)
2516 {
2517 p = &rp->p;
2518
2519 connected = if_lookup_address (&p->u.prefix4, AF_INET, VRF_DEFAULT);
2520 if (! connected)
2521 {
2522 zlog_warn ("Neighbor %s doesnt have connected interface!",
2523 inet_ntoa (p->u.prefix4));
2524 continue;
2525 }
2526
2527 /* Set destination address and port */
2528 memset (&to, 0, sizeof (struct sockaddr_in));
2529 to.sin_addr = p->u.prefix4;
2530 to.sin_port = htons (RIP_PORT_DEFAULT);
2531
2532 /* RIP version is rip's configuration. */
2533 rip_output_process (connected, &to, route_type, rip->version_send);
2534 }
2535 }
2536
2537 /* RIP's periodical timer. */
2538 static int
2539 rip_update (struct thread *t)
2540 {
2541 /* Clear timer pointer. */
2542 rip->t_update = NULL;
2543
2544 if (IS_RIP_DEBUG_EVENT)
2545 zlog_debug ("update timer fire!");
2546
2547 /* Process update output. */
2548 rip_update_process (rip_all_route);
2549
2550 /* Triggered updates may be suppressed if a regular update is due by
2551 the time the triggered update would be sent. */
2552 RIP_TIMER_OFF (rip->t_triggered_interval);
2553 rip->trigger = 0;
2554
2555 /* Register myself. */
2556 rip_event (RIP_UPDATE_EVENT, 0);
2557
2558 return 0;
2559 }
2560
2561 /* Walk down the RIP routing table then clear changed flag. */
2562 static void
2563 rip_clear_changed_flag (void)
2564 {
2565 struct route_node *rp;
2566 struct rip_info *rinfo = NULL;
2567 struct list *list = NULL;
2568 struct listnode *listnode = NULL;
2569
2570 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2571 if ((list = rp->info) != NULL)
2572 for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
2573 {
2574 UNSET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
2575 /* This flag can be set only on the first entry. */
2576 break;
2577 }
2578 }
2579
2580 /* Triggered update interval timer. */
2581 static int
2582 rip_triggered_interval (struct thread *t)
2583 {
2584 int rip_triggered_update (struct thread *);
2585
2586 rip->t_triggered_interval = NULL;
2587
2588 if (rip->trigger)
2589 {
2590 rip->trigger = 0;
2591 rip_triggered_update (t);
2592 }
2593 return 0;
2594 }
2595
2596 /* Execute triggered update. */
2597 static int
2598 rip_triggered_update (struct thread *t)
2599 {
2600 int interval;
2601
2602 /* Clear thred pointer. */
2603 rip->t_triggered_update = NULL;
2604
2605 /* Cancel interval timer. */
2606 RIP_TIMER_OFF (rip->t_triggered_interval);
2607 rip->trigger = 0;
2608
2609 /* Logging triggered update. */
2610 if (IS_RIP_DEBUG_EVENT)
2611 zlog_debug ("triggered update!");
2612
2613 /* Split Horizon processing is done when generating triggered
2614 updates as well as normal updates (see section 2.6). */
2615 rip_update_process (rip_changed_route);
2616
2617 /* Once all of the triggered updates have been generated, the route
2618 change flags should be cleared. */
2619 rip_clear_changed_flag ();
2620
2621 /* After a triggered update is sent, a timer should be set for a
2622 random interval between 1 and 5 seconds. If other changes that
2623 would trigger updates occur before the timer expires, a single
2624 update is triggered when the timer expires. */
2625 interval = (random () % 5) + 1;
2626
2627 rip->t_triggered_interval =
2628 thread_add_timer (master, rip_triggered_interval, NULL, interval);
2629
2630 return 0;
2631 }
2632
2633 /* Withdraw redistributed route. */
2634 void
2635 rip_redistribute_withdraw (int type)
2636 {
2637 struct route_node *rp;
2638 struct rip_info *rinfo = NULL;
2639 struct list *list = NULL;
2640
2641 if (!rip)
2642 return;
2643
2644 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2645 if ((list = rp->info) != NULL)
2646 {
2647 rinfo = listgetdata (listhead (list));
2648 if (rinfo->type == type
2649 && rinfo->sub_type != RIP_ROUTE_INTERFACE)
2650 {
2651 /* Perform poisoned reverse. */
2652 rinfo->metric = RIP_METRIC_INFINITY;
2653 RIP_TIMER_ON (rinfo->t_garbage_collect,
2654 rip_garbage_collect, rip->garbage_time);
2655 RIP_TIMER_OFF (rinfo->t_timeout);
2656 rinfo->flags |= RIP_RTF_CHANGED;
2657
2658 if (IS_RIP_DEBUG_EVENT) {
2659 struct prefix_ipv4 *p = (struct prefix_ipv4 *) &rp->p;
2660
2661 zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2662 inet_ntoa(p->prefix), p->prefixlen,
2663 ifindex2ifname(rinfo->ifindex, VRF_DEFAULT));
2664 }
2665
2666 rip_event (RIP_TRIGGERED_UPDATE, 0);
2667 }
2668 }
2669 }
2670
2671 /* Create new RIP instance and set it to global variable. */
2672 static int
2673 rip_create (void)
2674 {
2675 rip = XCALLOC (MTYPE_RIP, sizeof (struct rip));
2676
2677 /* Set initial value. */
2678 rip->version_send = RI_RIP_VERSION_2;
2679 rip->version_recv = RI_RIP_VERSION_1_AND_2;
2680 rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
2681 rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
2682 rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
2683 rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
2684
2685 /* Initialize RIP routig table. */
2686 rip->table = route_table_init ();
2687 rip->route = route_table_init ();
2688 rip->neighbor = route_table_init ();
2689
2690 /* Make output stream. */
2691 rip->obuf = stream_new (1500);
2692
2693 /* Make socket. */
2694 rip->sock = rip_create_socket ();
2695 if (rip->sock < 0)
2696 return rip->sock;
2697
2698 /* Create read and timer thread. */
2699 rip_event (RIP_READ, rip->sock);
2700 rip_event (RIP_UPDATE_EVENT, 1);
2701
2702 QOBJ_REG (rip, rip);
2703
2704 return 0;
2705 }
2706
2707 /* Sned RIP request to the destination. */
2708 int
2709 rip_request_send (struct sockaddr_in *to, struct interface *ifp,
2710 u_char version, struct connected *connected)
2711 {
2712 struct rte *rte;
2713 struct rip_packet rip_packet;
2714 struct listnode *node, *nnode;
2715
2716 memset (&rip_packet, 0, sizeof (rip_packet));
2717
2718 rip_packet.command = RIP_REQUEST;
2719 rip_packet.version = version;
2720 rte = rip_packet.rte;
2721 rte->metric = htonl (RIP_METRIC_INFINITY);
2722
2723 if (connected)
2724 {
2725 /*
2726 * connected is only sent for ripv1 case, or when
2727 * interface does not support multicast. Caller loops
2728 * over each connected address for this case.
2729 */
2730 if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
2731 to, connected) != sizeof (rip_packet))
2732 return -1;
2733 else
2734 return sizeof (rip_packet);
2735 }
2736
2737 /* send request on each connected network */
2738 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
2739 {
2740 struct prefix_ipv4 *p;
2741
2742 p = (struct prefix_ipv4 *) connected->address;
2743
2744 if (p->family != AF_INET)
2745 continue;
2746
2747 if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
2748 to, connected) != sizeof (rip_packet))
2749 return -1;
2750 }
2751 return sizeof (rip_packet);
2752 }
2753
2754 static int
2755 rip_update_jitter (unsigned long time)
2756 {
2757 #define JITTER_BOUND 4
2758 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2759 Given that, we cannot let time be less than JITTER_BOUND seconds.
2760 The RIPv2 RFC says jitter should be small compared to
2761 update_time. We consider 1/JITTER_BOUND to be small.
2762 */
2763
2764 int jitter_input = time;
2765 int jitter;
2766
2767 if (jitter_input < JITTER_BOUND)
2768 jitter_input = JITTER_BOUND;
2769
2770 jitter = (((random () % ((jitter_input * 2) + 1)) - jitter_input));
2771
2772 return jitter/JITTER_BOUND;
2773 }
2774
2775 void
2776 rip_event (enum rip_event event, int sock)
2777 {
2778 int jitter = 0;
2779
2780 switch (event)
2781 {
2782 case RIP_READ:
2783 rip->t_read = thread_add_read (master, rip_read, NULL, sock);
2784 break;
2785 case RIP_UPDATE_EVENT:
2786 RIP_TIMER_OFF (rip->t_update);
2787 jitter = rip_update_jitter (rip->update_time);
2788 rip->t_update =
2789 thread_add_timer (master, rip_update, NULL,
2790 sock ? 2 : rip->update_time + jitter);
2791 break;
2792 case RIP_TRIGGERED_UPDATE:
2793 if (rip->t_triggered_interval)
2794 rip->trigger = 1;
2795 else if (! rip->t_triggered_update)
2796 rip->t_triggered_update =
2797 thread_add_event (master, rip_triggered_update, NULL, 0);
2798 break;
2799 default:
2800 break;
2801 }
2802 }
2803
2804 DEFUN_NOSH (router_rip,
2805 router_rip_cmd,
2806 "router rip",
2807 "Enable a routing process\n"
2808 "Routing Information Protocol (RIP)\n")
2809 {
2810 int ret;
2811
2812 /* If rip is not enabled before. */
2813 if (! rip)
2814 {
2815 ret = rip_create ();
2816 if (ret < 0)
2817 {
2818 zlog_info ("Can't create RIP");
2819 return CMD_WARNING;
2820 }
2821 }
2822 VTY_PUSH_CONTEXT(RIP_NODE, rip);
2823
2824 return CMD_SUCCESS;
2825 }
2826
2827 DEFUN (no_router_rip,
2828 no_router_rip_cmd,
2829 "no router rip",
2830 NO_STR
2831 "Enable a routing process\n"
2832 "Routing Information Protocol (RIP)\n")
2833 {
2834 if (rip)
2835 rip_clean ();
2836 return CMD_SUCCESS;
2837 }
2838
2839 DEFUN (rip_version,
2840 rip_version_cmd,
2841 "version (1-2)",
2842 "Set routing protocol version\n"
2843 "version\n")
2844 {
2845 int idx_number = 1;
2846 int version;
2847
2848 version = atoi (argv[idx_number]->arg);
2849 if (version != RIPv1 && version != RIPv2)
2850 {
2851 vty_out (vty, "invalid rip version %d%s", version,
2852 VTY_NEWLINE);
2853 return CMD_WARNING;
2854 }
2855 rip->version_send = version;
2856 rip->version_recv = version;
2857
2858 return CMD_SUCCESS;
2859 }
2860
2861 DEFUN (no_rip_version,
2862 no_rip_version_cmd,
2863 "no version [(1-2)]",
2864 NO_STR
2865 "Set routing protocol version\n"
2866 "Version\n")
2867 {
2868 /* Set RIP version to the default. */
2869 rip->version_send = RI_RIP_VERSION_2;
2870 rip->version_recv = RI_RIP_VERSION_1_AND_2;
2871
2872 return CMD_SUCCESS;
2873 }
2874
2875
2876 DEFUN (rip_route,
2877 rip_route_cmd,
2878 "route A.B.C.D/M",
2879 "RIP static route configuration\n"
2880 "IP prefix <network>/<length>\n")
2881 {
2882 int idx_ipv4_prefixlen = 1;
2883 int ret;
2884 struct prefix_ipv4 p;
2885 struct route_node *node;
2886
2887 ret = str2prefix_ipv4 (argv[idx_ipv4_prefixlen]->arg, &p);
2888 if (ret < 0)
2889 {
2890 vty_out (vty, "Malformed address%s", VTY_NEWLINE);
2891 return CMD_WARNING;
2892 }
2893 apply_mask_ipv4 (&p);
2894
2895 /* For router rip configuration. */
2896 node = route_node_get (rip->route, (struct prefix *) &p);
2897
2898 if (node->info)
2899 {
2900 vty_out (vty, "There is already same static route.%s", VTY_NEWLINE);
2901 route_unlock_node (node);
2902 return CMD_WARNING;
2903 }
2904
2905 node->info = (void *)1;
2906
2907 rip_redistribute_add (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0, NULL, 0, 0, 0);
2908
2909 return CMD_SUCCESS;
2910 }
2911
2912 DEFUN (no_rip_route,
2913 no_rip_route_cmd,
2914 "no route A.B.C.D/M",
2915 NO_STR
2916 "RIP static route configuration\n"
2917 "IP prefix <network>/<length>\n")
2918 {
2919 int idx_ipv4_prefixlen = 2;
2920 int ret;
2921 struct prefix_ipv4 p;
2922 struct route_node *node;
2923
2924 ret = str2prefix_ipv4 (argv[idx_ipv4_prefixlen]->arg, &p);
2925 if (ret < 0)
2926 {
2927 vty_out (vty, "Malformed address%s", VTY_NEWLINE);
2928 return CMD_WARNING;
2929 }
2930 apply_mask_ipv4 (&p);
2931
2932 /* For router rip configuration. */
2933 node = route_node_lookup (rip->route, (struct prefix *) &p);
2934 if (! node)
2935 {
2936 vty_out (vty, "Can't find route %s.%s", argv[idx_ipv4_prefixlen]->arg,
2937 VTY_NEWLINE);
2938 return CMD_WARNING;
2939 }
2940
2941 rip_redistribute_delete (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0);
2942 route_unlock_node (node);
2943
2944 node->info = NULL;
2945 route_unlock_node (node);
2946
2947 return CMD_SUCCESS;
2948 }
2949
2950 #if 0
2951 static void
2952 rip_update_default_metric (void)
2953 {
2954 struct route_node *np;
2955 struct rip_info *rinfo = NULL;
2956 struct list *list = NULL;
2957 struct listnode *listnode = NULL;
2958
2959 for (np = route_top (rip->table); np; np = route_next (np))
2960 if ((list = np->info) != NULL)
2961 for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
2962 if (rinfo->type != ZEBRA_ROUTE_RIP && rinfo->type != ZEBRA_ROUTE_CONNECT)
2963 rinfo->metric = rip->default_metric;
2964 }
2965 #endif
2966
2967 DEFUN (rip_default_metric,
2968 rip_default_metric_cmd,
2969 "default-metric (1-16)",
2970 "Set a metric of redistribute routes\n"
2971 "Default metric\n")
2972 {
2973 int idx_number = 1;
2974 if (rip)
2975 {
2976 rip->default_metric = atoi (argv[idx_number]->arg);
2977 /* rip_update_default_metric (); */
2978 }
2979 return CMD_SUCCESS;
2980 }
2981
2982 DEFUN (no_rip_default_metric,
2983 no_rip_default_metric_cmd,
2984 "no default-metric [(1-16)]",
2985 NO_STR
2986 "Set a metric of redistribute routes\n"
2987 "Default metric\n")
2988 {
2989 if (rip)
2990 {
2991 rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
2992 /* rip_update_default_metric (); */
2993 }
2994 return CMD_SUCCESS;
2995 }
2996
2997
2998 DEFUN (rip_timers,
2999 rip_timers_cmd,
3000 "timers basic (5-2147483647) (5-2147483647) (5-2147483647)",
3001 "Adjust routing timers\n"
3002 "Basic routing protocol update timers\n"
3003 "Routing table update timer value in second. Default is 30.\n"
3004 "Routing information timeout timer. Default is 180.\n"
3005 "Garbage collection timer. Default is 120.\n")
3006 {
3007 int idx_number = 2;
3008 int idx_number_2 = 3;
3009 int idx_number_3 = 4;
3010 unsigned long update;
3011 unsigned long timeout;
3012 unsigned long garbage;
3013 char *endptr = NULL;
3014 unsigned long RIP_TIMER_MAX = 2147483647;
3015 unsigned long RIP_TIMER_MIN = 5;
3016
3017 update = strtoul (argv[idx_number]->arg, &endptr, 10);
3018 if (update > RIP_TIMER_MAX || update < RIP_TIMER_MIN || *endptr != '\0')
3019 {
3020 vty_out (vty, "update timer value error%s", VTY_NEWLINE);
3021 return CMD_WARNING;
3022 }
3023
3024 timeout = strtoul (argv[idx_number_2]->arg, &endptr, 10);
3025 if (timeout > RIP_TIMER_MAX || timeout < RIP_TIMER_MIN || *endptr != '\0')
3026 {
3027 vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
3028 return CMD_WARNING;
3029 }
3030
3031 garbage = strtoul (argv[idx_number_3]->arg, &endptr, 10);
3032 if (garbage > RIP_TIMER_MAX || garbage < RIP_TIMER_MIN || *endptr != '\0')
3033 {
3034 vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
3035 return CMD_WARNING;
3036 }
3037
3038 /* Set each timer value. */
3039 rip->update_time = update;
3040 rip->timeout_time = timeout;
3041 rip->garbage_time = garbage;
3042
3043 /* Reset update timer thread. */
3044 rip_event (RIP_UPDATE_EVENT, 0);
3045
3046 return CMD_SUCCESS;
3047 }
3048
3049 DEFUN (no_rip_timers,
3050 no_rip_timers_cmd,
3051 "no timers basic [(0-65535) (0-65535) (0-65535)]",
3052 NO_STR
3053 "Adjust routing timers\n"
3054 "Basic routing protocol update timers\n"
3055 "Routing table update timer value in second. Default is 30.\n"
3056 "Routing information timeout timer. Default is 180.\n"
3057 "Garbage collection timer. Default is 120.\n")
3058 {
3059 /* Set each timer value to the default. */
3060 rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
3061 rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
3062 rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
3063
3064 /* Reset update timer thread. */
3065 rip_event (RIP_UPDATE_EVENT, 0);
3066
3067 return CMD_SUCCESS;
3068 }
3069
3070
3071
3072 struct route_table *rip_distance_table;
3073
3074 struct rip_distance
3075 {
3076 /* Distance value for the IP source prefix. */
3077 u_char distance;
3078
3079 /* Name of the access-list to be matched. */
3080 char *access_list;
3081 };
3082
3083 static struct rip_distance *
3084 rip_distance_new (void)
3085 {
3086 return XCALLOC (MTYPE_RIP_DISTANCE, sizeof (struct rip_distance));
3087 }
3088
3089 static void
3090 rip_distance_free (struct rip_distance *rdistance)
3091 {
3092 XFREE (MTYPE_RIP_DISTANCE, rdistance);
3093 }
3094
3095 static int
3096 rip_distance_set (struct vty *vty, const char *distance_str, const char *ip_str,
3097 const char *access_list_str)
3098 {
3099 int ret;
3100 struct prefix_ipv4 p;
3101 u_char distance;
3102 struct route_node *rn;
3103 struct rip_distance *rdistance;
3104
3105 ret = str2prefix_ipv4 (ip_str, &p);
3106 if (ret == 0)
3107 {
3108 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
3109 return CMD_WARNING;
3110 }
3111
3112 distance = atoi (distance_str);
3113
3114 /* Get RIP distance node. */
3115 rn = route_node_get (rip_distance_table, (struct prefix *) &p);
3116 if (rn->info)
3117 {
3118 rdistance = rn->info;
3119 route_unlock_node (rn);
3120 }
3121 else
3122 {
3123 rdistance = rip_distance_new ();
3124 rn->info = rdistance;
3125 }
3126
3127 /* Set distance value. */
3128 rdistance->distance = distance;
3129
3130 /* Reset access-list configuration. */
3131 if (rdistance->access_list)
3132 {
3133 free (rdistance->access_list);
3134 rdistance->access_list = NULL;
3135 }
3136 if (access_list_str)
3137 rdistance->access_list = strdup (access_list_str);
3138
3139 return CMD_SUCCESS;
3140 }
3141
3142 static int
3143 rip_distance_unset (struct vty *vty, const char *distance_str,
3144 const char *ip_str, const char *access_list_str)
3145 {
3146 int ret;
3147 struct prefix_ipv4 p;
3148 struct route_node *rn;
3149 struct rip_distance *rdistance;
3150
3151 ret = str2prefix_ipv4 (ip_str, &p);
3152 if (ret == 0)
3153 {
3154 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
3155 return CMD_WARNING;
3156 }
3157
3158 rn = route_node_lookup (rip_distance_table, (struct prefix *)&p);
3159 if (! rn)
3160 {
3161 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
3162 return CMD_WARNING;
3163 }
3164
3165 rdistance = rn->info;
3166
3167 if (rdistance->access_list)
3168 free (rdistance->access_list);
3169 rip_distance_free (rdistance);
3170
3171 rn->info = NULL;
3172 route_unlock_node (rn);
3173 route_unlock_node (rn);
3174
3175 return CMD_SUCCESS;
3176 }
3177
3178 static void
3179 rip_distance_reset (void)
3180 {
3181 struct route_node *rn;
3182 struct rip_distance *rdistance;
3183
3184 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3185 if ((rdistance = rn->info) != NULL)
3186 {
3187 if (rdistance->access_list)
3188 free (rdistance->access_list);
3189 rip_distance_free (rdistance);
3190 rn->info = NULL;
3191 route_unlock_node (rn);
3192 }
3193 }
3194
3195 /* Apply RIP information to distance method. */
3196 u_char
3197 rip_distance_apply (struct rip_info *rinfo)
3198 {
3199 struct route_node *rn;
3200 struct prefix_ipv4 p;
3201 struct rip_distance *rdistance;
3202 struct access_list *alist;
3203
3204 if (! rip)
3205 return 0;
3206
3207 memset (&p, 0, sizeof (struct prefix_ipv4));
3208 p.family = AF_INET;
3209 p.prefix = rinfo->from;
3210 p.prefixlen = IPV4_MAX_BITLEN;
3211
3212 /* Check source address. */
3213 rn = route_node_match (rip_distance_table, (struct prefix *) &p);
3214 if (rn)
3215 {
3216 rdistance = rn->info;
3217 route_unlock_node (rn);
3218
3219 if (rdistance->access_list)
3220 {
3221 alist = access_list_lookup (AFI_IP, rdistance->access_list);
3222 if (alist == NULL)
3223 return 0;
3224 if (access_list_apply (alist, &rinfo->rp->p) == FILTER_DENY)
3225 return 0;
3226
3227 return rdistance->distance;
3228 }
3229 else
3230 return rdistance->distance;
3231 }
3232
3233 if (rip->distance)
3234 return rip->distance;
3235
3236 return 0;
3237 }
3238
3239 static void
3240 rip_distance_show (struct vty *vty)
3241 {
3242 struct route_node *rn;
3243 struct rip_distance *rdistance;
3244 int header = 1;
3245 char buf[BUFSIZ];
3246
3247 vty_out (vty, " Distance: (default is %d)%s",
3248 rip->distance ? rip->distance :ZEBRA_RIP_DISTANCE_DEFAULT,
3249 VTY_NEWLINE);
3250
3251 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3252 if ((rdistance = rn->info) != NULL)
3253 {
3254 if (header)
3255 {
3256 vty_out (vty, " Address Distance List%s",
3257 VTY_NEWLINE);
3258 header = 0;
3259 }
3260 sprintf (buf, "%s/%d", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen);
3261 vty_out (vty, " %-20s %4d %s%s",
3262 buf, rdistance->distance,
3263 rdistance->access_list ? rdistance->access_list : "",
3264 VTY_NEWLINE);
3265 }
3266 }
3267
3268 DEFUN (rip_distance,
3269 rip_distance_cmd,
3270 "distance (1-255)",
3271 "Administrative distance\n"
3272 "Distance value\n")
3273 {
3274 int idx_number = 1;
3275 rip->distance = atoi (argv[idx_number]->arg);
3276 return CMD_SUCCESS;
3277 }
3278
3279 DEFUN (no_rip_distance,
3280 no_rip_distance_cmd,
3281 "no distance (1-255)",
3282 NO_STR
3283 "Administrative distance\n"
3284 "Distance value\n")
3285 {
3286 rip->distance = 0;
3287 return CMD_SUCCESS;
3288 }
3289
3290 DEFUN (rip_distance_source,
3291 rip_distance_source_cmd,
3292 "distance (1-255) A.B.C.D/M",
3293 "Administrative distance\n"
3294 "Distance value\n"
3295 "IP source prefix\n")
3296 {
3297 int idx_number = 1;
3298 int idx_ipv4_prefixlen = 2;
3299 rip_distance_set (vty, argv[idx_number]->arg, argv[idx_ipv4_prefixlen]->arg, NULL);
3300 return CMD_SUCCESS;
3301 }
3302
3303 DEFUN (no_rip_distance_source,
3304 no_rip_distance_source_cmd,
3305 "no distance (1-255) A.B.C.D/M",
3306 NO_STR
3307 "Administrative distance\n"
3308 "Distance value\n"
3309 "IP source prefix\n")
3310 {
3311 int idx_number = 2;
3312 int idx_ipv4_prefixlen = 3;
3313 rip_distance_unset (vty, argv[idx_number]->arg, argv[idx_ipv4_prefixlen]->arg, NULL);
3314 return CMD_SUCCESS;
3315 }
3316
3317 DEFUN (rip_distance_source_access_list,
3318 rip_distance_source_access_list_cmd,
3319 "distance (1-255) A.B.C.D/M WORD",
3320 "Administrative distance\n"
3321 "Distance value\n"
3322 "IP source prefix\n"
3323 "Access list name\n")
3324 {
3325 int idx_number = 1;
3326 int idx_ipv4_prefixlen = 2;
3327 int idx_word = 3;
3328 rip_distance_set (vty, argv[idx_number]->arg, argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
3329 return CMD_SUCCESS;
3330 }
3331
3332 DEFUN (no_rip_distance_source_access_list,
3333 no_rip_distance_source_access_list_cmd,
3334 "no distance (1-255) A.B.C.D/M WORD",
3335 NO_STR
3336 "Administrative distance\n"
3337 "Distance value\n"
3338 "IP source prefix\n"
3339 "Access list name\n")
3340 {
3341 int idx_number = 2;
3342 int idx_ipv4_prefixlen = 3;
3343 int idx_word = 4;
3344 rip_distance_unset (vty, argv[idx_number]->arg, argv[idx_ipv4_prefixlen]->arg, argv[idx_word]->arg);
3345 return CMD_SUCCESS;
3346 }
3347
3348 /* Update ECMP routes to zebra when ECMP is disabled. */
3349 static void
3350 rip_ecmp_disable (void)
3351 {
3352 struct route_node *rp;
3353 struct rip_info *rinfo, *tmp_rinfo;
3354 struct list *list;
3355 struct listnode *node, *nextnode;
3356
3357 if (!rip)
3358 return;
3359
3360 for (rp = route_top (rip->table); rp; rp = route_next (rp))
3361 if ((list = rp->info) != NULL && listcount (list) > 1)
3362 {
3363 rinfo = listgetdata (listhead (list));
3364 if (!rip_route_rte (rinfo))
3365 continue;
3366
3367 /* Drop all other entries, except the first one. */
3368 for (ALL_LIST_ELEMENTS (list, node, nextnode, tmp_rinfo))
3369 if (tmp_rinfo != rinfo)
3370 {
3371 RIP_TIMER_OFF (tmp_rinfo->t_timeout);
3372 RIP_TIMER_OFF (tmp_rinfo->t_garbage_collect);
3373 list_delete_node (list, node);
3374 rip_info_free (tmp_rinfo);
3375 }
3376
3377 /* Update zebra. */
3378 rip_zebra_ipv4_add (rp);
3379
3380 /* Set the route change flag. */
3381 SET_FLAG (rinfo->flags, RIP_RTF_CHANGED);
3382
3383 /* Signal the output process to trigger an update. */
3384 rip_event (RIP_TRIGGERED_UPDATE, 0);
3385 }
3386 }
3387
3388 DEFUN (rip_allow_ecmp,
3389 rip_allow_ecmp_cmd,
3390 "allow-ecmp",
3391 "Allow Equal Cost MultiPath\n")
3392 {
3393 if (rip->ecmp)
3394 {
3395 vty_out (vty, "ECMP is already enabled.%s", VTY_NEWLINE);
3396 return CMD_WARNING;
3397 }
3398
3399 rip->ecmp = 1;
3400 zlog_info ("ECMP is enabled.");
3401 return CMD_SUCCESS;
3402 }
3403
3404 DEFUN (no_rip_allow_ecmp,
3405 no_rip_allow_ecmp_cmd,
3406 "no allow-ecmp",
3407 NO_STR
3408 "Allow Equal Cost MultiPath\n")
3409 {
3410 if (!rip->ecmp)
3411 {
3412 vty_out (vty, "ECMP is already disabled.%s", VTY_NEWLINE);
3413 return CMD_WARNING;
3414 }
3415
3416 rip->ecmp = 0;
3417 zlog_info ("ECMP is disabled.");
3418 rip_ecmp_disable ();
3419 return CMD_SUCCESS;
3420 }
3421
3422 /* Print out routes update time. */
3423 static void
3424 rip_vty_out_uptime (struct vty *vty, struct rip_info *rinfo)
3425 {
3426 time_t clock;
3427 struct tm *tm;
3428 #define TIME_BUF 25
3429 char timebuf [TIME_BUF];
3430 struct thread *thread;
3431
3432 if ((thread = rinfo->t_timeout) != NULL)
3433 {
3434 clock = thread_timer_remain_second (thread);
3435 tm = gmtime (&clock);
3436 strftime (timebuf, TIME_BUF, "%M:%S", tm);
3437 vty_out (vty, "%5s", timebuf);
3438 }
3439 else if ((thread = rinfo->t_garbage_collect) != NULL)
3440 {
3441 clock = thread_timer_remain_second (thread);
3442 tm = gmtime (&clock);
3443 strftime (timebuf, TIME_BUF, "%M:%S", tm);
3444 vty_out (vty, "%5s", timebuf);
3445 }
3446 }
3447
3448 static const char *
3449 rip_route_type_print (int sub_type)
3450 {
3451 switch (sub_type)
3452 {
3453 case RIP_ROUTE_RTE:
3454 return "n";
3455 case RIP_ROUTE_STATIC:
3456 return "s";
3457 case RIP_ROUTE_DEFAULT:
3458 return "d";
3459 case RIP_ROUTE_REDISTRIBUTE:
3460 return "r";
3461 case RIP_ROUTE_INTERFACE:
3462 return "i";
3463 default:
3464 return "?";
3465 }
3466 }
3467
3468 DEFUN (show_ip_rip,
3469 show_ip_rip_cmd,
3470 "show ip rip",
3471 SHOW_STR
3472 IP_STR
3473 "Show RIP routes\n")
3474 {
3475 struct route_node *np;
3476 struct rip_info *rinfo = NULL;
3477 struct list *list = NULL;
3478 struct listnode *listnode = NULL;
3479
3480 if (! rip)
3481 return CMD_SUCCESS;
3482
3483 vty_out (vty, "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP%s"
3484 "Sub-codes:%s"
3485 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,%s"
3486 " (i) - interface%s%s"
3487 " Network Next Hop Metric From Tag Time%s",
3488 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
3489
3490 for (np = route_top (rip->table); np; np = route_next (np))
3491 if ((list = np->info) != NULL)
3492 for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
3493 {
3494 int len;
3495
3496 len = vty_out (vty, "%c(%s) %s/%d",
3497 /* np->lock, For debugging. */
3498 zebra_route_char(rinfo->type),
3499 rip_route_type_print (rinfo->sub_type),
3500 inet_ntoa (np->p.u.prefix4), np->p.prefixlen);
3501
3502 len = 24 - len;
3503
3504 if (len > 0)
3505 vty_out (vty, "%*s", len, " ");
3506
3507 if (rinfo->nexthop.s_addr)
3508 vty_out (vty, "%-20s %2d ", inet_ntoa (rinfo->nexthop),
3509 rinfo->metric);
3510 else
3511 vty_out (vty, "0.0.0.0 %2d ", rinfo->metric);
3512
3513 /* Route which exist in kernel routing table. */
3514 if ((rinfo->type == ZEBRA_ROUTE_RIP) &&
3515 (rinfo->sub_type == RIP_ROUTE_RTE))
3516 {
3517 vty_out (vty, "%-15s ", inet_ntoa (rinfo->from));
3518 vty_out (vty, "%3"ROUTE_TAG_PRI" ", (route_tag_t)rinfo->tag);
3519 rip_vty_out_uptime (vty, rinfo);
3520 }
3521 else if (rinfo->metric == RIP_METRIC_INFINITY)
3522 {
3523 vty_out (vty, "self ");
3524 vty_out (vty, "%3"ROUTE_TAG_PRI" ", (route_tag_t)rinfo->tag);
3525 rip_vty_out_uptime (vty, rinfo);
3526 }
3527 else
3528 {
3529 if (rinfo->external_metric)
3530 {
3531 len = vty_out (vty, "self (%s:%d)",
3532 zebra_route_string(rinfo->type),
3533 rinfo->external_metric);
3534 len = 16 - len;
3535 if (len > 0)
3536 vty_out (vty, "%*s", len, " ");
3537 }
3538 else
3539 vty_out (vty, "self ");
3540 vty_out (vty, "%3"ROUTE_TAG_PRI, (route_tag_t)rinfo->tag);
3541 }
3542
3543 vty_out (vty, "%s", VTY_NEWLINE);
3544 }
3545 return CMD_SUCCESS;
3546 }
3547
3548 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3549 DEFUN (show_ip_rip_status,
3550 show_ip_rip_status_cmd,
3551 "show ip rip status",
3552 SHOW_STR
3553 IP_STR
3554 "Show RIP routes\n"
3555 "IP routing protocol process parameters and statistics\n")
3556 {
3557 struct listnode *node;
3558 struct interface *ifp;
3559 struct rip_interface *ri;
3560 extern const struct message ri_version_msg[];
3561 const char *send_version;
3562 const char *receive_version;
3563
3564 if (! rip)
3565 return CMD_SUCCESS;
3566
3567 vty_out (vty, "Routing Protocol is \"rip\"%s", VTY_NEWLINE);
3568 vty_out (vty, " Sending updates every %ld seconds with +/-50%%,",
3569 rip->update_time);
3570 vty_out (vty, " next due in %lu seconds%s",
3571 thread_timer_remain_second(rip->t_update),
3572 VTY_NEWLINE);
3573 vty_out (vty, " Timeout after %ld seconds,", rip->timeout_time);
3574 vty_out (vty, " garbage collect after %ld seconds%s", rip->garbage_time,
3575 VTY_NEWLINE);
3576
3577 /* Filtering status show. */
3578 config_show_distribute (vty);
3579
3580 /* Default metric information. */
3581 vty_out (vty, " Default redistribution metric is %d%s",
3582 rip->default_metric, VTY_NEWLINE);
3583
3584 /* Redistribute information. */
3585 vty_out (vty, " Redistributing:");
3586 config_write_rip_redistribute (vty, 0);
3587 vty_out (vty, "%s", VTY_NEWLINE);
3588
3589 vty_out (vty, " Default version control: send version %s,",
3590 lookup(ri_version_msg,rip->version_send));
3591 if (rip->version_recv == RI_RIP_VERSION_1_AND_2)
3592 vty_out (vty, " receive any version %s", VTY_NEWLINE);
3593 else
3594 vty_out (vty, " receive version %s %s",
3595 lookup(ri_version_msg,rip->version_recv), VTY_NEWLINE);
3596
3597 vty_out (vty, " Interface Send Recv Key-chain%s", VTY_NEWLINE);
3598
3599 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
3600 {
3601 ri = ifp->info;
3602
3603 if (!ri->running)
3604 continue;
3605
3606 if (ri->enable_network || ri->enable_interface)
3607 {
3608 if (ri->ri_send == RI_RIP_UNSPEC)
3609 send_version = lookup (ri_version_msg, rip->version_send);
3610 else
3611 send_version = lookup (ri_version_msg, ri->ri_send);
3612
3613 if (ri->ri_receive == RI_RIP_UNSPEC)
3614 receive_version = lookup (ri_version_msg, rip->version_recv);
3615 else
3616 receive_version = lookup (ri_version_msg, ri->ri_receive);
3617
3618 vty_out (vty, " %-17s%-3s %-3s %s%s", ifp->name,
3619 send_version,
3620 receive_version,
3621 ri->key_chain ? ri->key_chain : "",
3622 VTY_NEWLINE);
3623 }
3624 }
3625
3626 vty_out (vty, " Routing for Networks:%s", VTY_NEWLINE);
3627 config_write_rip_network (vty, 0);
3628
3629 {
3630 int found_passive = 0;
3631 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
3632 {
3633 ri = ifp->info;
3634
3635 if ((ri->enable_network || ri->enable_interface) && ri->passive)
3636 {
3637 if (!found_passive)
3638 {
3639 vty_out (vty, " Passive Interface(s):%s", VTY_NEWLINE);
3640 found_passive = 1;
3641 }
3642 vty_out (vty, " %s%s", ifp->name, VTY_NEWLINE);
3643 }
3644 }
3645 }
3646
3647 vty_out (vty, " Routing Information Sources:%s", VTY_NEWLINE);
3648 vty_out (vty, " Gateway BadPackets BadRoutes Distance Last Update%s", VTY_NEWLINE);
3649 rip_peer_display (vty);
3650
3651 rip_distance_show (vty);
3652
3653 return CMD_SUCCESS;
3654 }
3655
3656 /* RIP configuration write function. */
3657 static int
3658 config_write_rip (struct vty *vty)
3659 {
3660 int write = 0;
3661 struct route_node *rn;
3662 struct rip_distance *rdistance;
3663
3664 if (rip)
3665 {
3666 /* Router RIP statement. */
3667 vty_out (vty, "router rip%s", VTY_NEWLINE);
3668 write++;
3669
3670 /* RIP version statement. Default is RIP version 2. */
3671 if (rip->version_send != RI_RIP_VERSION_2
3672 || rip->version_recv != RI_RIP_VERSION_1_AND_2)
3673 vty_out (vty, " version %d%s", rip->version_send,
3674 VTY_NEWLINE);
3675
3676 /* RIP timer configuration. */
3677 if (rip->update_time != RIP_UPDATE_TIMER_DEFAULT
3678 || rip->timeout_time != RIP_TIMEOUT_TIMER_DEFAULT
3679 || rip->garbage_time != RIP_GARBAGE_TIMER_DEFAULT)
3680 vty_out (vty, " timers basic %lu %lu %lu%s",
3681 rip->update_time,
3682 rip->timeout_time,
3683 rip->garbage_time,
3684 VTY_NEWLINE);
3685
3686 /* Default information configuration. */
3687 if (rip->default_information)
3688 {
3689 if (rip->default_information_route_map)
3690 vty_out (vty, " default-information originate route-map %s%s",
3691 rip->default_information_route_map, VTY_NEWLINE);
3692 else
3693 vty_out (vty, " default-information originate%s",
3694 VTY_NEWLINE);
3695 }
3696
3697 /* Redistribute configuration. */
3698 config_write_rip_redistribute (vty, 1);
3699
3700 /* RIP offset-list configuration. */
3701 config_write_rip_offset_list (vty);
3702
3703 /* RIP enabled network and interface configuration. */
3704 config_write_rip_network (vty, 1);
3705
3706 /* RIP default metric configuration */
3707 if (rip->default_metric != RIP_DEFAULT_METRIC_DEFAULT)
3708 vty_out (vty, " default-metric %d%s",
3709 rip->default_metric, VTY_NEWLINE);
3710
3711 /* Distribute configuration. */
3712 write += config_write_distribute (vty);
3713
3714 /* Interface routemap configuration */
3715 write += config_write_if_rmap (vty);
3716
3717 /* Distance configuration. */
3718 if (rip->distance)
3719 vty_out (vty, " distance %d%s", rip->distance, VTY_NEWLINE);
3720
3721 /* RIP source IP prefix distance configuration. */
3722 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3723 if ((rdistance = rn->info) != NULL)
3724 vty_out (vty, " distance %d %s/%d %s%s", rdistance->distance,
3725 inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
3726 rdistance->access_list ? rdistance->access_list : "",
3727 VTY_NEWLINE);
3728
3729 /* ECMP configuration. */
3730 if (rip->ecmp)
3731 vty_out (vty, " allow-ecmp%s", VTY_NEWLINE);
3732
3733 /* RIP static route configuration. */
3734 for (rn = route_top (rip->route); rn; rn = route_next (rn))
3735 if (rn->info)
3736 vty_out (vty, " route %s/%d%s",
3737 inet_ntoa (rn->p.u.prefix4),
3738 rn->p.prefixlen,
3739 VTY_NEWLINE);
3740
3741 }
3742 return write;
3743 }
3744
3745 /* RIP node structure. */
3746 static struct cmd_node rip_node =
3747 {
3748 RIP_NODE,
3749 "%s(config-router)# ",
3750 1
3751 };
3752
3753 /* Distribute-list update functions. */
3754 static void
3755 rip_distribute_update (struct distribute *dist)
3756 {
3757 struct interface *ifp;
3758 struct rip_interface *ri;
3759 struct access_list *alist;
3760 struct prefix_list *plist;
3761
3762 if (! dist->ifname)
3763 return;
3764
3765 ifp = if_lookup_by_name (dist->ifname, VRF_DEFAULT);
3766 if (ifp == NULL)
3767 return;
3768
3769 ri = ifp->info;
3770
3771 if (dist->list[DISTRIBUTE_V4_IN])
3772 {
3773 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_V4_IN]);
3774 if (alist)
3775 ri->list[RIP_FILTER_IN] = alist;
3776 else
3777 ri->list[RIP_FILTER_IN] = NULL;
3778 }
3779 else
3780 ri->list[RIP_FILTER_IN] = NULL;
3781
3782 if (dist->list[DISTRIBUTE_V4_OUT])
3783 {
3784 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_V4_OUT]);
3785 if (alist)
3786 ri->list[RIP_FILTER_OUT] = alist;
3787 else
3788 ri->list[RIP_FILTER_OUT] = NULL;
3789 }
3790 else
3791 ri->list[RIP_FILTER_OUT] = NULL;
3792
3793 if (dist->prefix[DISTRIBUTE_V4_IN])
3794 {
3795 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_V4_IN]);
3796 if (plist)
3797 ri->prefix[RIP_FILTER_IN] = plist;
3798 else
3799 ri->prefix[RIP_FILTER_IN] = NULL;
3800 }
3801 else
3802 ri->prefix[RIP_FILTER_IN] = NULL;
3803
3804 if (dist->prefix[DISTRIBUTE_V4_OUT])
3805 {
3806 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_V4_OUT]);
3807 if (plist)
3808 ri->prefix[RIP_FILTER_OUT] = plist;
3809 else
3810 ri->prefix[RIP_FILTER_OUT] = NULL;
3811 }
3812 else
3813 ri->prefix[RIP_FILTER_OUT] = NULL;
3814 }
3815
3816 void
3817 rip_distribute_update_interface (struct interface *ifp)
3818 {
3819 struct distribute *dist;
3820
3821 dist = distribute_lookup (ifp->name);
3822 if (dist)
3823 rip_distribute_update (dist);
3824 }
3825
3826 /* Update all interface's distribute list. */
3827 /* ARGSUSED */
3828 static void
3829 rip_distribute_update_all (struct prefix_list *notused)
3830 {
3831 struct interface *ifp;
3832 struct listnode *node, *nnode;
3833
3834 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
3835 rip_distribute_update_interface (ifp);
3836 }
3837 /* ARGSUSED */
3838 static void
3839 rip_distribute_update_all_wrapper(struct access_list *notused)
3840 {
3841 rip_distribute_update_all(NULL);
3842 }
3843
3844 /* Delete all added rip route. */
3845 void
3846 rip_clean (void)
3847 {
3848 int i;
3849 struct route_node *rp;
3850 struct rip_info *rinfo = NULL;
3851 struct list *list = NULL;
3852 struct listnode *listnode = NULL;
3853
3854 if (rip)
3855 {
3856 QOBJ_UNREG (rip);
3857
3858 /* Clear RIP routes */
3859 for (rp = route_top (rip->table); rp; rp = route_next (rp))
3860 if ((list = rp->info) != NULL)
3861 {
3862 rinfo = listgetdata (listhead (list));
3863 if (rip_route_rte (rinfo))
3864 rip_zebra_ipv4_delete (rp);
3865
3866 for (ALL_LIST_ELEMENTS_RO (list, listnode, rinfo))
3867 {
3868 RIP_TIMER_OFF (rinfo->t_timeout);
3869 RIP_TIMER_OFF (rinfo->t_garbage_collect);
3870 rip_info_free (rinfo);
3871 }
3872 list_delete (list);
3873 rp->info = NULL;
3874 route_unlock_node (rp);
3875 }
3876
3877 /* Cancel RIP related timers. */
3878 RIP_TIMER_OFF (rip->t_update);
3879 RIP_TIMER_OFF (rip->t_triggered_update);
3880 RIP_TIMER_OFF (rip->t_triggered_interval);
3881
3882 /* Cancel read thread. */
3883 THREAD_READ_OFF (rip->t_read);
3884
3885 /* Close RIP socket. */
3886 if (rip->sock >= 0)
3887 {
3888 close (rip->sock);
3889 rip->sock = -1;
3890 }
3891
3892 /* Static RIP route configuration. */
3893 for (rp = route_top (rip->route); rp; rp = route_next (rp))
3894 if (rp->info)
3895 {
3896 rp->info = NULL;
3897 route_unlock_node (rp);
3898 }
3899
3900 /* RIP neighbor configuration. */
3901 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
3902 if (rp->info)
3903 {
3904 rp->info = NULL;
3905 route_unlock_node (rp);
3906 }
3907
3908 /* Redistribute related clear. */
3909 if (rip->default_information_route_map)
3910 free (rip->default_information_route_map);
3911
3912 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3913 if (rip->route_map[i].name)
3914 free (rip->route_map[i].name);
3915
3916 XFREE (MTYPE_ROUTE_TABLE, rip->table);
3917 XFREE (MTYPE_ROUTE_TABLE, rip->route);
3918 XFREE (MTYPE_ROUTE_TABLE, rip->neighbor);
3919
3920 XFREE (MTYPE_RIP, rip);
3921 rip = NULL;
3922 }
3923
3924 rip_clean_network ();
3925 rip_passive_nondefault_clean ();
3926 rip_offset_clean ();
3927 rip_interfaces_clean ();
3928 rip_distance_reset ();
3929 rip_redistribute_clean ();
3930 }
3931
3932 /* Reset all values to the default settings. */
3933 void
3934 rip_reset (void)
3935 {
3936 /* Reset global counters. */
3937 rip_global_route_changes = 0;
3938 rip_global_queries = 0;
3939
3940 /* Call ripd related reset functions. */
3941 rip_debug_reset ();
3942 rip_route_map_reset ();
3943
3944 /* Call library reset functions. */
3945 vty_reset ();
3946 access_list_reset ();
3947 prefix_list_reset ();
3948
3949 distribute_list_reset ();
3950
3951 rip_interfaces_reset ();
3952 rip_distance_reset ();
3953
3954 rip_zclient_reset ();
3955 }
3956
3957 static void
3958 rip_if_rmap_update (struct if_rmap *if_rmap)
3959 {
3960 struct interface *ifp;
3961 struct rip_interface *ri;
3962 struct route_map *rmap;
3963
3964 ifp = if_lookup_by_name (if_rmap->ifname, VRF_DEFAULT);
3965 if (ifp == NULL)
3966 return;
3967
3968 ri = ifp->info;
3969
3970 if (if_rmap->routemap[IF_RMAP_IN])
3971 {
3972 rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_IN]);
3973 if (rmap)
3974 ri->routemap[IF_RMAP_IN] = rmap;
3975 else
3976 ri->routemap[IF_RMAP_IN] = NULL;
3977 }
3978 else
3979 ri->routemap[RIP_FILTER_IN] = NULL;
3980
3981 if (if_rmap->routemap[IF_RMAP_OUT])
3982 {
3983 rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_OUT]);
3984 if (rmap)
3985 ri->routemap[IF_RMAP_OUT] = rmap;
3986 else
3987 ri->routemap[IF_RMAP_OUT] = NULL;
3988 }
3989 else
3990 ri->routemap[RIP_FILTER_OUT] = NULL;
3991 }
3992
3993 void
3994 rip_if_rmap_update_interface (struct interface *ifp)
3995 {
3996 struct if_rmap *if_rmap;
3997
3998 if_rmap = if_rmap_lookup (ifp->name);
3999 if (if_rmap)
4000 rip_if_rmap_update (if_rmap);
4001 }
4002
4003 static void
4004 rip_routemap_update_redistribute (void)
4005 {
4006 int i;
4007
4008 if (rip)
4009 {
4010 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
4011 {
4012 if (rip->route_map[i].name)
4013 rip->route_map[i].map =
4014 route_map_lookup_by_name (rip->route_map[i].name);
4015 }
4016 }
4017 }
4018
4019 /* ARGSUSED */
4020 static void
4021 rip_routemap_update (const char *notused)
4022 {
4023 struct interface *ifp;
4024 struct listnode *node, *nnode;
4025
4026 for (ALL_LIST_ELEMENTS (vrf_iflist (VRF_DEFAULT), node, nnode, ifp))
4027 rip_if_rmap_update_interface (ifp);
4028
4029 rip_routemap_update_redistribute ();
4030 }
4031
4032 /* Allocate new rip structure and set default value. */
4033 void
4034 rip_init (void)
4035 {
4036 /* Install top nodes. */
4037 install_node (&rip_node, config_write_rip);
4038
4039 /* Install rip commands. */
4040 install_element (VIEW_NODE, &show_ip_rip_cmd);
4041 install_element (VIEW_NODE, &show_ip_rip_status_cmd);
4042 install_element (CONFIG_NODE, &router_rip_cmd);
4043 install_element (CONFIG_NODE, &no_router_rip_cmd);
4044
4045 install_default (RIP_NODE);
4046 install_element (RIP_NODE, &rip_version_cmd);
4047 install_element (RIP_NODE, &no_rip_version_cmd);
4048 install_element (RIP_NODE, &rip_default_metric_cmd);
4049 install_element (RIP_NODE, &no_rip_default_metric_cmd);
4050 install_element (RIP_NODE, &rip_timers_cmd);
4051 install_element (RIP_NODE, &no_rip_timers_cmd);
4052 install_element (RIP_NODE, &rip_route_cmd);
4053 install_element (RIP_NODE, &no_rip_route_cmd);
4054 install_element (RIP_NODE, &rip_distance_cmd);
4055 install_element (RIP_NODE, &no_rip_distance_cmd);
4056 install_element (RIP_NODE, &rip_distance_source_cmd);
4057 install_element (RIP_NODE, &no_rip_distance_source_cmd);
4058 install_element (RIP_NODE, &rip_distance_source_access_list_cmd);
4059 install_element (RIP_NODE, &no_rip_distance_source_access_list_cmd);
4060 install_element (RIP_NODE, &rip_allow_ecmp_cmd);
4061 install_element (RIP_NODE, &no_rip_allow_ecmp_cmd);
4062
4063 /* Debug related init. */
4064 rip_debug_init ();
4065
4066 /* Access list install. */
4067 access_list_init ();
4068 access_list_add_hook (rip_distribute_update_all_wrapper);
4069 access_list_delete_hook (rip_distribute_update_all_wrapper);
4070
4071 /* Prefix list initialize.*/
4072 prefix_list_init ();
4073 prefix_list_add_hook (rip_distribute_update_all);
4074 prefix_list_delete_hook (rip_distribute_update_all);
4075
4076 /* Distribute list install. */
4077 distribute_list_init (RIP_NODE);
4078 distribute_list_add_hook (rip_distribute_update);
4079 distribute_list_delete_hook (rip_distribute_update);
4080
4081 /* Route-map */
4082 rip_route_map_init ();
4083 rip_offset_init ();
4084
4085 route_map_add_hook (rip_routemap_update);
4086 route_map_delete_hook (rip_routemap_update);
4087
4088 if_rmap_init (RIP_NODE);
4089 if_rmap_hook_add (rip_if_rmap_update);
4090 if_rmap_hook_delete (rip_if_rmap_update);
4091
4092 /* Distance control. */
4093 rip_distance_table = route_table_init ();
4094 }