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