1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RIP version 1 and 2.
3 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
19 #include "sockunion.h"
24 #include "distribute.h"
25 #ifdef CRYPTO_INTERNAL
30 #include "lib_errors.h"
31 #include "northbound_cli.h"
33 #include "lib/printfrr.h"
35 #include "ripd/ripd.h"
36 #include "ripd/rip_nb.h"
37 #include "ripd/rip_bfd.h"
38 #include "ripd/rip_debug.h"
39 #include "ripd/rip_errors.h"
40 #include "ripd/rip_interface.h"
42 /* UDP receive buffer size */
43 #define RIP_UDP_RCV_BUF 41600
45 DEFINE_MGROUP(RIPD
, "ripd");
46 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure");
47 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name");
48 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info");
49 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance");
52 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
54 static void rip_triggered_update(struct event
*);
55 static int rip_update_jitter(unsigned long);
56 static void rip_distance_table_node_cleanup(struct route_table
*table
,
57 struct route_node
*node
);
58 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
59 static void rip_instance_disable(struct rip
*rip
);
61 static void rip_distribute_update(struct distribute_ctx
*ctx
,
62 struct distribute
*dist
);
64 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
65 struct if_rmap
*if_rmap
);
67 /* RIP output routes type. */
68 enum { rip_all_route
, rip_changed_route
};
70 /* RIP command strings. */
71 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
72 {RIP_RESPONSE
, "RESPONSE"},
73 {RIP_TRACEON
, "TRACEON"},
74 {RIP_TRACEOFF
, "TRACEOFF"},
76 {RIP_POLL_ENTRY
, "POLL ENTRY"},
79 /* Generate rb-tree of RIP instances. */
80 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
82 return strcmp(a
->vrf_name
, b
->vrf_name
);
84 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
86 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
88 /* Utility function to set broadcast option to the socket. */
89 static int sockopt_broadcast(int sock
)
94 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
97 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
103 int rip_route_rte(struct rip_info
*rinfo
)
105 return (rinfo
->type
== ZEBRA_ROUTE_RIP
106 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
109 static struct rip_info
*rip_info_new(void)
111 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
114 void rip_info_free(struct rip_info
*rinfo
)
116 XFREE(MTYPE_RIP_INFO
, rinfo
);
119 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
121 return route_table_get_info(rinfo
->rp
->table
);
124 /* RIP route garbage collect timer. */
125 static void rip_garbage_collect(struct event
*t
)
127 struct rip_info
*rinfo
;
128 struct route_node
*rp
;
130 rinfo
= EVENT_ARG(t
);
132 /* Off timeout timer. */
133 EVENT_OFF(rinfo
->t_timeout
);
135 /* Get route_node pointer. */
138 /* Unlock route_node. */
139 listnode_delete(rp
->info
, rinfo
);
140 if (list_isempty((struct list
*)rp
->info
)) {
141 list_delete((struct list
**)&rp
->info
);
142 route_unlock_node(rp
);
145 /* Free RIP routing information. */
146 rip_info_free(rinfo
);
149 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
151 /* Add new route to the ECMP list.
152 * RETURN: the new entry added in the list, or NULL if it is not the first
153 * entry and ECMP is not allowed.
155 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
157 struct route_node
*rp
= rinfo_new
->rp
;
158 struct rip_info
*rinfo
= NULL
;
159 struct rip_info
*rinfo_exist
= NULL
;
160 struct list
*list
= NULL
;
161 struct listnode
*node
= NULL
;
162 struct listnode
*nnode
= NULL
;
164 if (rp
->info
== NULL
)
165 rp
->info
= list_new();
166 list
= (struct list
*)rp
->info
;
168 /* If ECMP is not allowed and some entry already exists in the list,
170 if (listcount(list
) && !rip
->ecmp
)
173 /* Add or replace an existing ECMP path with lower neighbor IP */
174 if (listcount(list
) && listcount(list
) >= rip
->ecmp
) {
175 struct rip_info
*from_highest
= NULL
;
177 /* Find the rip_info struct that has the highest nexthop IP */
178 for (ALL_LIST_ELEMENTS(list
, node
, nnode
, rinfo_exist
))
181 IPV4_ADDR_CMP(&rinfo_exist
->from
,
182 &from_highest
->from
) > 0)) {
183 from_highest
= rinfo_exist
;
186 /* If we have a route in ECMP group, delete the old
187 * one that has a higher next-hop address. Lower IP is
190 if (rip
->ecmp
> 1 && from_highest
&&
191 IPV4_ADDR_CMP(&from_highest
->from
, &rinfo_new
->from
) > 0) {
192 rip_ecmp_delete(rip
, from_highest
);
200 rinfo
= rip_info_new();
201 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
202 listnode_add(list
, rinfo
);
204 if (rip_route_rte(rinfo
)) {
205 rip_timeout_update(rip
, rinfo
);
206 rip_zebra_ipv4_add(rip
, rp
);
209 /* Set the route change flag on the first entry. */
210 rinfo
= listgetdata(listhead(list
));
211 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
213 /* Signal the output process to trigger an update (see section 2.5). */
214 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
219 /* Replace the ECMP list with the new route.
220 * RETURN: the new entry added in the list
222 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
224 struct route_node
*rp
= rinfo_new
->rp
;
225 struct list
*list
= (struct list
*)rp
->info
;
226 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
227 struct listnode
*node
= NULL
, *nextnode
= NULL
;
229 if (list
== NULL
|| listcount(list
) == 0)
230 return rip_ecmp_add(rip
, rinfo_new
);
232 /* Get the first entry */
233 rinfo
= listgetdata(listhead(list
));
235 /* Learnt route replaced by a local one. Delete it from zebra. */
236 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
237 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
238 rip_zebra_ipv4_delete(rip
, rp
);
240 /* Re-use the first entry, and delete the others. */
241 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
242 if (tmp_rinfo
== rinfo
)
245 EVENT_OFF(tmp_rinfo
->t_timeout
);
246 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
247 list_delete_node(list
, node
);
248 rip_info_free(tmp_rinfo
);
251 EVENT_OFF(rinfo
->t_timeout
);
252 EVENT_OFF(rinfo
->t_garbage_collect
);
253 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
255 if (rip_route_rte(rinfo
)) {
256 rip_timeout_update(rip
, rinfo
);
257 /* The ADD message implies an update. */
258 rip_zebra_ipv4_add(rip
, rp
);
261 /* Set the route change flag. */
262 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
264 /* Signal the output process to trigger an update (see section 2.5). */
265 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
270 /* Delete one route from the ECMP list.
272 * null - the entry is freed, and other entries exist in the list
273 * the entry - the entry is the last one in the list; its metric is set
274 * to INFINITY, and the garbage collector is started for it
276 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
278 struct route_node
*rp
= rinfo
->rp
;
279 struct list
*list
= (struct list
*)rp
->info
;
281 EVENT_OFF(rinfo
->t_timeout
);
283 if (listcount(list
) > 1) {
284 /* Some other ECMP entries still exist. Just delete this entry.
286 EVENT_OFF(rinfo
->t_garbage_collect
);
287 listnode_delete(list
, rinfo
);
288 if (rip_route_rte(rinfo
)
289 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
290 /* The ADD message implies the update. */
291 rip_zebra_ipv4_add(rip
, rp
);
292 rip_info_free(rinfo
);
295 assert(rinfo
== listgetdata(listhead(list
)));
297 /* This is the only entry left in the list. We must keep it in
298 * the list for garbage collection time, with INFINITY metric.
301 rinfo
->metric
= RIP_METRIC_INFINITY
;
302 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
305 if (rip_route_rte(rinfo
)
306 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
307 rip_zebra_ipv4_delete(rip
, rp
);
310 /* Set the route change flag on the first entry. */
311 rinfo
= listgetdata(listhead(list
));
312 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
314 /* Signal the output process to trigger an update (see section 2.5). */
315 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
320 /* Timeout RIP routes. */
321 static void rip_timeout(struct event
*t
)
323 struct rip_info
*rinfo
= EVENT_ARG(t
);
324 struct rip
*rip
= rip_info_get_instance(rinfo
);
326 rip_ecmp_delete(rip
, rinfo
);
329 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
331 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
332 EVENT_OFF(rinfo
->t_timeout
);
333 event_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
338 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
339 struct rip_interface
*ri
)
341 struct distribute
*dist
;
342 struct access_list
*alist
;
343 struct prefix_list
*plist
;
344 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
346 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
348 /* Input distribute-list filtering. */
349 if (ri
->list
[rip_distribute
] &&
350 access_list_apply(ri
->list
[rip_distribute
], (struct prefix
*)p
) ==
352 if (IS_RIP_DEBUG_PACKET
)
353 zlog_debug("%pFX filtered by distribute %s", p
, inout
);
357 if (ri
->prefix
[rip_distribute
] &&
358 prefix_list_apply(ri
->prefix
[rip_distribute
], (struct prefix
*)p
) ==
360 if (IS_RIP_DEBUG_PACKET
)
361 zlog_debug("%pFX filtered by prefix-list %s", p
, inout
);
365 /* All interface filter check. */
366 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
370 if (dist
->list
[distribute
]) {
371 alist
= access_list_lookup(AFI_IP
, dist
->list
[distribute
]);
374 if (access_list_apply(alist
, (struct prefix
*)p
) ==
376 if (IS_RIP_DEBUG_PACKET
)
378 "%pFX filtered by distribute %s",
384 if (dist
->prefix
[distribute
]) {
385 plist
= prefix_list_lookup(AFI_IP
, dist
->prefix
[distribute
]);
388 if (prefix_list_apply(plist
, (struct prefix
*)p
) ==
390 if (IS_RIP_DEBUG_PACKET
)
392 "%pFX filtered by prefix-list %s",
402 /* Check nexthop address validity. */
403 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
405 struct interface
*ifp
;
406 struct listnode
*cnode
;
407 struct connected
*ifc
;
410 /* If nexthop address matches local configured address then it is
413 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
414 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
417 if (p
->family
== AF_INET
418 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
425 /* RIP add route to routing table. */
426 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
427 struct interface
*ifp
)
431 struct prefix_ipv4 p
;
432 struct route_node
*rp
;
433 struct rip_info
*rinfo
= NULL
, newinfo
;
434 struct rip_interface
*ri
;
435 struct in_addr
*nexthop
;
437 unsigned char old_dist
, new_dist
;
438 struct list
*list
= NULL
;
439 struct listnode
*node
= NULL
;
441 /* Make prefix structure. */
442 memset(&p
, 0, sizeof(struct prefix_ipv4
));
444 p
.prefix
= rte
->prefix
;
445 p
.prefixlen
= ip_masklen(rte
->mask
);
447 /* Make sure mask is applied. */
453 /* Apply input filters. */
454 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
458 memset(&newinfo
, 0, sizeof(newinfo
));
459 newinfo
.type
= ZEBRA_ROUTE_RIP
;
460 newinfo
.sub_type
= RIP_ROUTE_RTE
;
461 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
462 newinfo
.from
= from
->sin_addr
;
463 newinfo
.nh
.ifindex
= ifp
->ifindex
;
464 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
465 newinfo
.metric
= rte
->metric
;
466 newinfo
.metric_out
= rte
->metric
; /* XXX */
467 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
469 /* Modify entry according to the interface routemap. */
470 if (ri
->routemap
[RIP_FILTER_IN
]) {
471 /* The object should be of the type of rip_info */
472 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
473 (struct prefix
*)&p
, &newinfo
);
475 if (ret
== RMAP_DENYMATCH
) {
476 if (IS_RIP_DEBUG_PACKET
)
478 "RIP %pFX is filtered by route-map in",
483 /* Get back the object */
484 rte
->nexthop
= newinfo
.nexthop_out
;
485 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
486 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
490 /* Once the entry has been validated, update the metric by
491 adding the cost of the network on which the message
492 arrived. If the result is greater than infinity, use infinity
493 (RFC2453 Sec. 3.9.2) */
494 /* Zebra ripd can handle offset-list in. */
495 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
497 /* If offset-list does not modify the metric use interface's
500 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
502 if (rte
->metric
> RIP_METRIC_INFINITY
)
503 rte
->metric
= RIP_METRIC_INFINITY
;
505 /* Set nexthop pointer. */
506 if (rte
->nexthop
.s_addr
== INADDR_ANY
)
507 nexthop
= &from
->sin_addr
;
509 nexthop
= &rte
->nexthop
;
511 /* Check if nexthop address is myself, then do nothing. */
512 if (rip_nexthop_check(rip
, nexthop
) < 0) {
513 if (IS_RIP_DEBUG_PACKET
)
514 zlog_debug("Nexthop address %pI4 is myself",
519 /* Get index for the prefix. */
520 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
523 newinfo
.nh
.gate
.ipv4
= *nexthop
;
524 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
525 newinfo
.metric
= rte
->metric
;
526 newinfo
.tag
= ntohs(rte
->tag
);
527 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
529 new_dist
= newinfo
.distance
? newinfo
.distance
530 : ZEBRA_RIP_DISTANCE_DEFAULT
;
532 /* Check to see whether there is already RIP route on the table. */
533 if ((list
= rp
->info
) != NULL
)
534 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
535 /* Need to compare with redistributed entry or local
537 if (!rip_route_rte(rinfo
))
540 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
541 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
544 if (listnextnode(node
))
547 /* Not found in the list */
549 if (rte
->metric
> rinfo
->metric
) {
550 /* New route has a greater metric.
552 route_unlock_node(rp
);
556 if (rte
->metric
< rinfo
->metric
)
557 /* New route has a smaller metric.
558 * Replace the ECMP list
559 * with the new one in below. */
562 /* Metrics are same. We compare the distances.
564 old_dist
= rinfo
->distance
? rinfo
->distance
565 : ZEBRA_RIP_DISTANCE_DEFAULT
;
567 if (new_dist
> old_dist
) {
568 /* New route has a greater distance.
570 route_unlock_node(rp
);
574 if (new_dist
< old_dist
)
575 /* New route has a smaller distance.
576 * Replace the ECMP list
577 * with the new one in below. */
580 /* Metrics and distances are both same. Keep
582 * the new route is added in the ECMP list in
587 /* Local static route. */
588 if (rinfo
->type
== ZEBRA_ROUTE_RIP
589 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
590 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
591 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
592 route_unlock_node(rp
);
596 /* Redistributed route check. */
597 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
598 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
599 old_dist
= rinfo
->distance
;
600 /* Only routes directly connected to an interface
602 * may have a valid NULL distance */
603 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
606 : ZEBRA_RIP_DISTANCE_DEFAULT
;
607 /* If imported route does not have STRICT precedence,
608 mark it as a ghost */
609 if (new_dist
<= old_dist
610 && rte
->metric
!= RIP_METRIC_INFINITY
)
611 rip_ecmp_replace(rip
, &newinfo
);
613 route_unlock_node(rp
);
620 route_unlock_node(rp
);
622 /* Now, check to see whether there is already an explicit route
623 for the destination prefix. If there is no such route, add
624 this route to the routing table, unless the metric is
625 infinity (there is no point in adding a route which
627 if (rte
->metric
!= RIP_METRIC_INFINITY
)
628 rip_ecmp_add(rip
, &newinfo
);
630 /* Route is there but we are not sure the route is RIP or not.
633 /* If there is an existing route, compare the next hop address
634 to the address of the router from which the datagram came.
635 If this datagram is from the same router as the existing
636 route, reinitialize the timeout. */
637 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
638 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
640 old_dist
= rinfo
->distance
? rinfo
->distance
641 : ZEBRA_RIP_DISTANCE_DEFAULT
;
643 /* Next, compare the metrics. If the datagram is from the same
644 router as the existing route, and the new metric is different
645 than the old one; or, if the new metric is lower than the old
646 one, or if the tag has been changed; or if there is a route
647 with a lower administrave distance; or an update of the
648 distance on the actual route; do the following actions: */
649 if ((same
&& rinfo
->metric
!= rte
->metric
)
650 || (rte
->metric
< rinfo
->metric
)
651 || ((same
) && (rinfo
->metric
== rte
->metric
)
652 && (newinfo
.tag
!= rinfo
->tag
))
653 || (old_dist
> new_dist
)
654 || ((old_dist
!= new_dist
) && same
)) {
655 if (listcount(list
) == 1) {
656 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
657 rip_ecmp_replace(rip
, &newinfo
);
659 rip_ecmp_delete(rip
, rinfo
);
661 if (newinfo
.metric
< rinfo
->metric
)
662 rip_ecmp_replace(rip
, &newinfo
);
663 else if (newinfo
.metric
> rinfo
->metric
)
664 rip_ecmp_delete(rip
, rinfo
);
665 else if (new_dist
< old_dist
)
666 rip_ecmp_replace(rip
, &newinfo
);
667 else if (new_dist
> old_dist
)
668 rip_ecmp_delete(rip
, rinfo
);
670 int update
= CHECK_FLAG(rinfo
->flags
,
675 assert(newinfo
.metric
676 != RIP_METRIC_INFINITY
);
678 EVENT_OFF(rinfo
->t_timeout
);
679 EVENT_OFF(rinfo
->t_garbage_collect
);
680 memcpy(rinfo
, &newinfo
,
681 sizeof(struct rip_info
));
682 rip_timeout_update(rip
, rinfo
);
685 rip_zebra_ipv4_add(rip
, rp
);
687 /* - Set the route change flag on the
689 rinfo
= listgetdata(listhead(list
));
690 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
691 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
694 } else /* same & no change */
695 rip_timeout_update(rip
, rinfo
);
697 /* Unlock tempolary lock of the route. */
698 route_unlock_node(rp
);
702 /* Dump RIP packet */
703 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
708 const char *command_str
;
712 /* Set command string. */
713 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
714 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
716 command_str
= "unknown";
718 /* Dump packet header. */
719 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
720 packet
->version
, size
);
722 /* Dump each routing table entry. */
725 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
726 if (packet
->version
== RIPv2
) {
727 netmask
= ip_masklen(rte
->mask
);
729 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
731 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
732 p
= (uint8_t *)&rte
->prefix
;
735 " family 0x%X type %d auth string: %s",
738 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
739 struct rip_md5_info
*md5
;
741 md5
= (struct rip_md5_info
*)&packet
745 " family 0x%X type %d (MD5 authentication)",
749 " RIP-2 packet len %d Key ID %d Auth Data len %d",
750 ntohs(md5
->packet_len
),
751 md5
->keyid
, md5
->auth_len
);
752 zlog_debug(" Sequence Number %ld",
753 (unsigned long)ntohl(
755 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
756 p
= (uint8_t *)&rte
->prefix
;
759 " family 0x%X type %d (MD5 data)",
763 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
764 p
[0], p
[1], p
[2], p
[3], p
[4],
765 p
[5], p
[6], p
[7], p
[8], p
[9],
766 p
[10], p
[11], p
[12], p
[13],
770 " family 0x%X type %d (Unknown auth type)",
776 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
778 &rte
->prefix
, netmask
, &rte
->nexthop
,
780 (route_tag_t
)ntohs(rte
->tag
),
781 (unsigned long)ntohl(rte
->metric
));
783 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
785 &rte
->prefix
, ntohs(rte
->family
),
786 (route_tag_t
)ntohs(rte
->tag
),
787 (unsigned long)ntohl(rte
->metric
));
792 /* Check if the destination address is valid (unicast; not net 0
793 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
794 check net 0 because we accept default route. */
795 static int rip_destination_check(struct in_addr addr
)
797 uint32_t destination
;
799 /* Convert to host byte order. */
800 destination
= ntohl(addr
.s_addr
);
802 if (IPV4_NET127(destination
))
805 /* Net 0 may match to the default route. */
806 if (IPV4_NET0(destination
) && destination
!= 0)
809 /* Unicast address must belong to class A, B, C. */
810 if (IN_CLASSA(destination
))
812 if (IN_CLASSB(destination
))
814 if (IN_CLASSC(destination
))
820 /* RIP version 2 authentication. */
821 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
822 struct interface
*ifp
)
824 struct rip_interface
*ri
;
825 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
828 /* reject passwords with zeros in the middle of the string */
829 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
830 if (auth_str
[i
] != '\0')
834 if (IS_RIP_DEBUG_EVENT
)
835 zlog_debug("RIPv2 simple password authentication from %pI4",
840 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
841 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
844 /* Simple password authentication. */
846 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
850 struct keychain
*keychain
;
853 keychain
= keychain_lookup(ri
->key_chain
);
854 if (keychain
== NULL
|| keychain
->key
== NULL
)
857 key
= key_match_for_accept(keychain
, auth_str
);
864 /* RIP version 2 authentication with MD5. */
865 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
866 int length
, struct interface
*ifp
)
868 struct rip_interface
*ri
;
869 struct rip_md5_info
*md5
;
870 struct rip_md5_data
*md5data
;
871 struct keychain
*keychain
;
873 #ifdef CRYPTO_OPENSSL
875 #elif CRYPTO_INTERNAL
878 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
880 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
882 if (IS_RIP_DEBUG_EVENT
)
883 zlog_debug("RIPv2 MD5 authentication from %pI4",
887 md5
= (struct rip_md5_info
*)&packet
->rte
;
889 /* Check auth type. */
890 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
893 /* If the authentication length is less than 16, then it must be wrong
895 * any interpretation of rfc2082. Some implementations also interpret
896 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
897 * RIP_AUTH_MD5_COMPAT_SIZE.
899 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
900 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
901 if (IS_RIP_DEBUG_EVENT
)
903 "RIPv2 MD5 authentication, strange authentication length field %d",
908 /* grab and verify check packet length */
909 packet_len
= ntohs(md5
->packet_len
);
911 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
912 if (IS_RIP_DEBUG_EVENT
)
914 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
915 md5
->packet_len
, length
);
919 /* retrieve authentication data */
920 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
923 keychain
= keychain_lookup(ri
->key_chain
);
924 if (keychain
== NULL
)
927 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
928 if (key
== NULL
|| key
->string
== NULL
)
931 memcpy(auth_str
, key
->string
,
932 MIN(sizeof(auth_str
), strlen(key
->string
)));
933 } else if (ri
->auth_str
)
934 memcpy(auth_str
, ri
->auth_str
,
935 MIN(sizeof(auth_str
), strlen(ri
->auth_str
)));
937 if (auth_str
[0] == 0)
940 /* MD5 digest authentication. */
941 #ifdef CRYPTO_OPENSSL
942 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
943 ctx
= EVP_MD_CTX_new();
944 EVP_DigestInit(ctx
, EVP_md5());
945 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
946 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
947 EVP_DigestFinal(ctx
, digest
, &md5_size
);
948 EVP_MD_CTX_free(ctx
);
949 #elif CRYPTO_INTERNAL
950 memset(&ctx
, 0, sizeof(ctx
));
952 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
953 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
954 MD5Final(digest
, &ctx
);
957 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
963 /* Pick correct auth string for sends, prepare auth_str buffer for use.
964 * (left justified and padded).
966 * presumes one of ri or key is valid, and that the auth strings they point
967 * to are nul terminated. If neither are present, auth_str will be fully
971 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
972 char *auth_str
, int len
)
976 memset(auth_str
, 0, len
);
977 if (key
&& key
->string
)
978 memcpy(auth_str
, key
->string
,
979 MIN((size_t)len
, strlen(key
->string
)));
980 else if (ri
->auth_str
)
981 memcpy(auth_str
, ri
->auth_str
,
982 MIN((size_t)len
, strlen(ri
->auth_str
)));
987 /* Write RIPv2 simple password authentication information
989 * auth_str is presumed to be 2 bytes and correctly prepared
990 * (left justified and zero padded).
992 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
994 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
996 stream_putw(s
, RIP_FAMILY_AUTH
);
997 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
998 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
1003 /* write RIPv2 MD5 "authentication header"
1004 * (uses the auth key data field)
1006 * Digest offset field is set to 0.
1008 * returns: offset of the digest offset field, which must be set when
1009 * length to the auth-data MD5 digest is known.
1011 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1015 static uint32_t seq
= 0;
1017 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1019 /* MD5 authentication. */
1020 stream_putw(s
, RIP_FAMILY_AUTH
);
1021 stream_putw(s
, RIP_AUTH_MD5
);
1023 /* MD5 AH digest offset field.
1025 * Set to placeholder value here, to true value when RIP-2 Packet length
1026 * is known. Actual value is set in .....().
1028 doff
= stream_get_endp(s
);
1033 stream_putc(s
, key
->index
% 256);
1037 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1038 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1040 * to be configurable.
1042 stream_putc(s
, ri
->md5_auth_len
);
1044 /* Sequence Number (non-decreasing). */
1045 /* RFC2080: The value used in the sequence number is
1046 arbitrary, but two suggestions are the time of the
1047 message's creation or a simple message counter. */
1048 stream_putl(s
, ++seq
);
1050 /* Reserved field must be zero. */
1057 /* If authentication is in used, write the appropriate header
1058 * returns stream offset to which length must later be written
1059 * or 0 if this is not required
1061 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1062 struct key
*key
, char *auth_str
, int len
)
1064 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1066 switch (ri
->auth_type
) {
1067 case RIP_AUTH_SIMPLE_PASSWORD
:
1068 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1069 rip_auth_simple_write(s
, auth_str
, len
);
1072 return rip_auth_md5_ah_write(s
, ri
, key
);
1078 /* Write RIPv2 MD5 authentication data trailer */
1079 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1080 size_t doff
, char *auth_str
, int authlen
)
1083 #ifdef CRYPTO_OPENSSL
1085 #elif CRYPTO_INTERNAL
1088 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1090 /* Make it sure this interface is configured as MD5
1092 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1093 && (authlen
== RIP_AUTH_MD5_SIZE
));
1096 /* Get packet length. */
1097 len
= stream_get_endp(s
);
1099 /* Check packet length. */
1100 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1101 flog_err(EC_RIP_PACKET
,
1102 "%s: packet length %ld is less than minimum length.",
1107 /* Set the digest offset length in the header */
1108 stream_putw_at(s
, doff
, len
);
1110 /* Set authentication data. */
1111 stream_putw(s
, RIP_FAMILY_AUTH
);
1112 stream_putw(s
, RIP_AUTH_DATA
);
1114 /* Generate a digest for the RIP packet. */
1115 #ifdef CRYPTO_OPENSSL
1116 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1117 ctx
= EVP_MD_CTX_new();
1118 EVP_DigestInit(ctx
, EVP_md5());
1119 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1120 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1121 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1122 EVP_MD_CTX_free(ctx
);
1123 #elif CRYPTO_INTERNAL
1124 memset(&ctx
, 0, sizeof(ctx
));
1126 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1127 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1128 MD5Final(digest
, &ctx
);
1131 /* Copy the digest to the packet. */
1132 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1135 /* RIP routing information. */
1136 static void rip_response_process(struct rip_packet
*packet
, int size
,
1137 struct sockaddr_in
*from
,
1138 struct connected
*ifc
)
1140 struct rip_interface
*ri
= ifc
->ifp
->info
;
1141 struct rip
*rip
= ri
->rip
;
1144 struct prefix_ipv4 ifaddr
;
1145 struct prefix_ipv4 ifaddrclass
;
1148 memset(&ifaddr
, 0, sizeof(ifaddr
));
1149 /* We don't know yet. */
1152 /* The Response must be ignored if it is not from the RIP
1153 port. (RFC2453 - Sec. 3.9.2)*/
1154 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1155 zlog_info("response doesn't come from RIP port: %d",
1157 rip_peer_bad_packet(rip
, ri
, from
);
1161 /* The datagram's IPv4 source address should be checked to see
1162 whether the datagram is from a valid neighbor; the source of the
1163 datagram must be on a directly connected network (RFC2453 - Sec.
1165 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1169 "This datagram doesn't come from a valid neighbor: %pI4",
1171 rip_peer_bad_packet(rip
, ri
, from
);
1175 /* It is also worth checking to see whether the response is from one
1176 of the router's own addresses. */
1178 ; /* Alredy done in rip_read () */
1180 /* Update RIP peer. */
1181 rip_peer_update(rip
, ri
, from
, packet
->version
);
1183 /* Set RTE pointer. */
1186 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1187 /* RIPv2 authentication check. */
1188 /* If the Address Family Identifier of the first (and only the
1189 first) entry in the message is 0xFFFF, then the remainder of
1190 the entry contains the authentication. */
1191 /* If the packet gets here it means authentication enabled */
1192 /* Check is done in rip_read(). So, just skipping it */
1193 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1194 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1197 if (rte
->family
!= htons(AF_INET
)) {
1198 /* Address family check. RIP only supports AF_INET. */
1199 zlog_info("Unsupported family %d from %pI4",
1205 if (packet
->version
== RIPv1
&& rte
->tag
!= 0) {
1206 zlog_warn("RIPv1 reserved field is nonzero: %d",
1211 /* - is the destination address valid (e.g., unicast; not net 0
1213 if (!rip_destination_check(rte
->prefix
)) {
1215 "Network is net 0 or net 127 or it is not unicast network");
1216 rip_peer_bad_route(rip
, ri
, from
);
1220 /* Convert metric value to host byte order. */
1221 rte
->metric
= ntohl(rte
->metric
);
1223 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1224 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1225 zlog_info("Route's metric is not in the 1-16 range.");
1226 rip_peer_bad_route(rip
, ri
, from
);
1230 /* RIPv1 does not have nexthop value. */
1231 if (packet
->version
== RIPv1
1232 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1233 zlog_info("RIPv1 packet with nexthop value %pI4",
1235 rip_peer_bad_route(rip
, ri
, from
);
1239 /* That is, if the provided information is ignored, a possibly
1240 sub-optimal, but absolutely valid, route may be taken. If
1241 the received Next Hop is not directly reachable, it should be
1242 treated as 0.0.0.0. */
1243 if (packet
->version
== RIPv2
1244 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1247 /* Multicast address check. */
1248 addrval
= ntohl(rte
->nexthop
.s_addr
);
1249 if (IN_CLASSD(addrval
)) {
1251 "Nexthop %pI4 is multicast address, skip this rte",
1256 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1257 rip
->vrf
->vrf_id
)) {
1258 struct route_node
*rn
;
1259 struct rip_info
*rinfo
;
1261 rn
= route_node_match_ipv4(rip
->table
,
1267 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1270 if (IS_RIP_DEBUG_EVENT
)
1272 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1274 rte
->nexthop
= rinfo
->from
;
1276 if (IS_RIP_DEBUG_EVENT
)
1278 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1280 rte
->nexthop
.s_addr
=
1284 route_unlock_node(rn
);
1286 if (IS_RIP_DEBUG_EVENT
)
1288 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1290 rte
->nexthop
.s_addr
= INADDR_ANY
;
1295 /* For RIPv1, there won't be a valid netmask.
1296 * This is a best guess at the masks. If everyone was using old
1297 * Ciscos before the 'ip subnet zero' option, it would be almost
1300 * Cisco summarize ripv1 advertisements to the classful boundary
1301 * (/16 for class B's) except when the RIP packet does to inside
1302 * the classful network in question.
1304 if ((packet
->version
== RIPv1
1305 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1306 || (packet
->version
== RIPv2
1307 && (rte
->prefix
.s_addr
!= INADDR_ANY
1308 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1309 uint32_t destination
;
1311 if (subnetted
== -1) {
1312 memcpy(&ifaddr
, ifc
->address
, sizeof(ifaddr
));
1313 memcpy(&ifaddrclass
, &ifaddr
,
1314 sizeof(ifaddrclass
));
1315 apply_classful_mask_ipv4(&ifaddrclass
);
1317 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1321 destination
= ntohl(rte
->prefix
.s_addr
);
1323 if (IN_CLASSA(destination
))
1324 masklen2ip(8, &rte
->mask
);
1325 else if (IN_CLASSB(destination
))
1326 masklen2ip(16, &rte
->mask
);
1327 else if (IN_CLASSC(destination
))
1328 masklen2ip(24, &rte
->mask
);
1331 masklen2ip(ifaddrclass
.prefixlen
,
1332 (struct in_addr
*)&destination
);
1333 if ((subnetted
== 1)
1334 && ((rte
->prefix
.s_addr
& destination
)
1335 == ifaddrclass
.prefix
.s_addr
)) {
1336 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1337 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1338 != rte
->prefix
.s_addr
)
1339 masklen2ip(32, &rte
->mask
);
1340 if (IS_RIP_DEBUG_EVENT
)
1341 zlog_debug("Subnetted route %pI4",
1344 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1345 != rte
->prefix
.s_addr
)
1349 if (IS_RIP_DEBUG_EVENT
) {
1350 zlog_debug("Resultant route %pI4",
1352 zlog_debug("Resultant mask %pI4",
1357 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1358 ignore the entry. */
1359 if ((packet
->version
== RIPv2
)
1360 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1361 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1362 != rte
->prefix
.s_addr
)) {
1364 "RIPv2 address %pI4 is not mask /%d applied one",
1365 &rte
->prefix
, ip_masklen(rte
->mask
));
1366 rip_peer_bad_route(rip
, ri
, from
);
1370 /* Default route's netmask is ignored. */
1371 if (packet
->version
== RIPv2
1372 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1373 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1374 if (IS_RIP_DEBUG_EVENT
)
1376 "Default route with non-zero netmask. Set zero to netmask");
1377 rte
->mask
.s_addr
= INADDR_ANY
;
1380 /* Routing table updates. */
1381 rip_rte_process(rte
, from
, ifc
->ifp
);
1385 /* Make socket for RIP protocol. */
1386 int rip_create_socket(struct vrf
*vrf
)
1390 struct sockaddr_in addr
;
1391 const char *vrf_dev
= NULL
;
1393 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1394 addr
.sin_family
= AF_INET
;
1395 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1396 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1397 addr
.sin_len
= sizeof(struct sockaddr_in
);
1398 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1399 /* sending port must always be the RIP port */
1400 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1402 /* Make datagram socket. */
1403 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1404 vrf_dev
= vrf
->name
;
1405 frr_with_privs(&ripd_privs
) {
1406 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1409 flog_err_sys(EC_LIB_SOCKET
,
1410 "Cannot create UDP socket: %s",
1411 safe_strerror(errno
));
1416 sockopt_broadcast(sock
);
1417 sockopt_reuseaddr(sock
);
1418 sockopt_reuseport(sock
);
1419 setsockopt_ipv4_multicast_loop(sock
, 0);
1420 #ifdef IPTOS_PREC_INTERNETCONTROL
1421 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1423 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1425 frr_with_privs(&ripd_privs
) {
1426 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1428 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1429 __func__
, sock
, &addr
.sin_addr
,
1430 (int)ntohs(addr
.sin_port
),
1431 safe_strerror(errno
));
1441 /* RIP packet send to destination address, on interface denoted by
1442 * by connected argument. NULL to argument denotes destination should be
1443 * should be RIP multicast group
1445 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1446 struct connected
*ifc
)
1448 struct rip_interface
*ri
;
1451 struct sockaddr_in sin
;
1455 struct cmsghdr
*cmsgptr
;
1456 char adata
[256] = {};
1457 struct in_pktinfo
*pkt
;
1458 #endif /* GNU_LINUX */
1460 assert(ifc
!= NULL
);
1461 ri
= ifc
->ifp
->info
;
1464 if (IS_RIP_DEBUG_PACKET
) {
1465 #define ADDRESS_SIZE 20
1466 char dst
[ADDRESS_SIZE
];
1469 inet_ntop(AF_INET
, &to
->sin_addr
, dst
, sizeof(dst
));
1471 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1472 inet_ntop(AF_INET
, &sin
.sin_addr
, dst
, sizeof(dst
));
1475 zlog_debug("%s %pI4 > %s (%s)", __func__
,
1476 &ifc
->address
->u
.prefix4
, dst
, ifc
->ifp
->name
);
1479 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1481 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1482 * configured with multiple addresses on the same
1483 * subnet: the first address on the subnet is configured
1484 * "primary", and all subsequent addresses on that subnet
1485 * are treated as "secondary" addresses. In order to avoid
1486 * routing-table bloat on other rip listeners, we do not send
1487 * out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1488 * XXX Since Linux is the only system for which the
1489 * ZEBRA_IFA_SECONDARY flag is set, we would end up
1490 * sending a packet for a "secondary" source address on
1491 * non-linux systems.
1493 if (IS_RIP_DEBUG_PACKET
)
1494 zlog_debug("duplicate dropped");
1498 /* Make destination address. */
1499 memset(&sin
, 0, sizeof(sin
));
1500 sin
.sin_family
= AF_INET
;
1501 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1502 sin
.sin_len
= sizeof(struct sockaddr_in
);
1503 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1505 /* When destination is specified, use it's port and address. */
1507 sin
.sin_port
= to
->sin_port
;
1508 sin
.sin_addr
= to
->sin_addr
;
1510 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1511 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1513 rip_interface_multicast_set(rip
->sock
, ifc
);
1516 memset(&msg
, 0, sizeof(msg
));
1517 msg
.msg_name
= (void *)&sin
;
1518 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1525 msg
.msg_control
= (void *)adata
;
1526 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1528 cmsgptr
= (struct cmsghdr
*)adata
;
1529 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1530 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1531 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1532 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1533 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1534 pkt
->ipi_spec_dst
.s_addr
= ifc
->address
->u
.prefix4
.s_addr
;
1535 #endif /* GNU_LINUX */
1537 ret
= sendmsg(rip
->sock
, &msg
, 0);
1539 if (IS_RIP_DEBUG_EVENT
)
1540 zlog_debug("SEND to %pI4 port %d", &sin
.sin_addr
,
1541 ntohs(sin
.sin_port
));
1544 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1549 /* Add redistributed route to RIP table. */
1550 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1551 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1552 unsigned int metric
, unsigned char distance
,
1556 struct route_node
*rp
= NULL
;
1557 struct rip_info
*rinfo
= NULL
, newinfo
;
1558 struct list
*list
= NULL
;
1560 /* Redistribute route */
1561 ret
= rip_destination_check(p
->prefix
);
1565 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1567 memset(&newinfo
, 0, sizeof(newinfo
));
1568 newinfo
.type
= type
;
1569 newinfo
.sub_type
= sub_type
;
1571 newinfo
.external_metric
= metric
;
1572 newinfo
.distance
= distance
;
1573 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1578 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1579 rinfo
= listgetdata(listhead(list
));
1581 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1582 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1583 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1584 route_unlock_node(rp
);
1588 /* Manually configured RIP route check. */
1589 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1590 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1591 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1592 if (type
!= ZEBRA_ROUTE_RIP
1593 || ((sub_type
!= RIP_ROUTE_STATIC
)
1594 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1595 route_unlock_node(rp
);
1600 (void)rip_ecmp_replace(rip
, &newinfo
);
1601 route_unlock_node(rp
);
1603 (void)rip_ecmp_add(rip
, &newinfo
);
1605 if (IS_RIP_DEBUG_EVENT
) {
1606 zlog_debug("Redistribute new prefix %pFX", p
);
1609 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1612 /* Delete redistributed route from RIP table. */
1613 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1614 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1617 struct route_node
*rp
;
1618 struct rip_info
*rinfo
;
1620 ret
= rip_destination_check(p
->prefix
);
1624 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1626 struct list
*list
= rp
->info
;
1628 if (list
!= NULL
&& listcount(list
) != 0) {
1629 rinfo
= listgetdata(listhead(list
));
1630 if (rinfo
!= NULL
&& rinfo
->type
== type
1631 && rinfo
->sub_type
== sub_type
1632 && rinfo
->nh
.ifindex
== ifindex
) {
1633 /* Perform poisoned reverse. */
1634 rinfo
->metric
= RIP_METRIC_INFINITY
;
1635 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1636 rip_garbage_collect
,
1638 EVENT_OFF(rinfo
->t_timeout
);
1639 rinfo
->flags
|= RIP_RTF_CHANGED
;
1641 if (IS_RIP_DEBUG_EVENT
)
1643 "Poison %pFX on the interface %s with an infinity metric [delete]",
1649 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1652 route_unlock_node(rp
);
1656 /* Response to request called from rip_read ().*/
1657 static void rip_request_process(struct rip_packet
*packet
, int size
,
1658 struct sockaddr_in
*from
, struct connected
*ifc
)
1663 struct prefix_ipv4 p
;
1664 struct route_node
*rp
;
1665 struct rip_info
*rinfo
;
1666 struct rip_interface
*ri
;
1668 /* Does not reponse to the requests on the loopback interfaces */
1669 if (if_is_loopback(ifc
->ifp
))
1672 /* Check RIP process is enabled on this interface. */
1673 ri
= ifc
->ifp
->info
;
1678 /* When passive interface is specified, suppress responses */
1682 /* RIP peer update. */
1683 rip_peer_update(rip
, ri
, from
, packet
->version
);
1685 lim
= ((caddr_t
)packet
) + size
;
1688 /* The Request is processed entry by entry. If there are no
1689 entries, no response is given. */
1690 if (lim
== (caddr_t
)rte
)
1693 /* There is one special case. If there is exactly one entry in the
1694 request, and it has an address family identifier of zero and a
1695 metric of infinity (i.e., 16), then this is a request to send the
1696 entire routing table. */
1697 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1698 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1699 /* All route with split horizon */
1700 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1702 if (ntohs(rte
->family
) != AF_INET
)
1705 /* Examine the list of RTEs in the Request one by one. For each
1706 entry, look up the destination in the router's routing
1707 database and, if there is a route, put that route's metric in
1708 the metric field of the RTE. If there is no explicit route
1709 to the specified destination, put infinity in the metric
1710 field. Once all the entries have been filled in, change the
1711 command from Request to Response and send the datagram back
1712 to the requestor. */
1715 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1716 p
.prefix
= rte
->prefix
;
1717 p
.prefixlen
= ip_masklen(rte
->mask
);
1718 apply_mask_ipv4(&p
);
1720 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1722 rinfo
= listgetdata(
1723 listhead((struct list
*)rp
->info
));
1724 rte
->metric
= htonl(rinfo
->metric
);
1725 route_unlock_node(rp
);
1727 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1729 packet
->command
= RIP_RESPONSE
;
1731 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1733 rip
->counters
.queries
++;
1736 /* First entry point of RIP packet. */
1737 static void rip_read(struct event
*t
)
1739 struct rip
*rip
= EVENT_ARG(t
);
1743 union rip_buf rip_buf
;
1744 struct rip_packet
*packet
;
1745 struct sockaddr_in from
;
1749 struct interface
*ifp
= NULL
;
1750 struct connected
*ifc
;
1751 struct rip_interface
*ri
= NULL
;
1754 /* Fetch socket then register myself. */
1757 /* Add myself to tne next event */
1758 rip_event(rip
, RIP_READ
, sock
);
1760 /* RIPd manages only IPv4. */
1761 memset(&from
, 0, sizeof(from
));
1762 fromlen
= sizeof(struct sockaddr_in
);
1764 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1765 (struct sockaddr
*)&from
, &fromlen
);
1767 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1768 safe_strerror(errno
));
1772 /* Check is this packet comming from myself? */
1773 if (if_check_address(rip
, from
.sin_addr
)) {
1774 if (IS_RIP_DEBUG_PACKET
)
1775 zlog_debug("ignore packet comes from myself (VRF %s)",
1780 /* Which interface is this packet comes from. */
1781 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1788 /* RIP packet received */
1789 if (IS_RIP_DEBUG_EVENT
)
1790 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1791 &from
.sin_addr
, ntohs(from
.sin_port
),
1792 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1794 /* If this packet come from unknown interface, ignore it. */
1795 if (ifp
== NULL
|| ri
== NULL
) {
1797 "%s: cannot find interface for packet from %pI4 port %d (VRF %s)",
1798 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1804 p
.u
.prefix4
= from
.sin_addr
;
1805 p
.prefixlen
= IPV4_MAX_BITLEN
;
1807 ifc
= connected_lookup_prefix(ifp
, &p
);
1811 "%s: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1812 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1813 ifp
->name
, rip
->vrf_name
);
1817 /* Packet length check. */
1818 if (len
< RIP_PACKET_MINSIZ
) {
1819 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1821 rip_peer_bad_packet(rip
, ri
, &from
);
1824 if (len
> RIP_PACKET_MAXSIZ
) {
1825 zlog_warn("packet size %d is larger than max size %d", len
,
1827 rip_peer_bad_packet(rip
, ri
, &from
);
1831 /* Packet alignment check. */
1832 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1833 zlog_warn("packet size %d is wrong for RIP packet alignment",
1835 rip_peer_bad_packet(rip
, ri
, &from
);
1839 /* Set RTE number. */
1840 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1842 /* For easy to handle. */
1843 packet
= &rip_buf
.rip_packet
;
1845 /* RIP version check. */
1846 if (packet
->version
== 0) {
1847 zlog_info("version 0 with command %d received.",
1849 rip_peer_bad_packet(rip
, ri
, &from
);
1853 /* Dump RIP packet. */
1854 if (IS_RIP_DEBUG_RECV
)
1855 rip_packet_dump(packet
, len
, "RECV");
1857 /* RIP version adjust. This code should rethink now. RFC1058 says
1858 that "Version 1 implementations are to ignore this extra data and
1859 process only the fields specified in this document.". So RIPv3
1860 packet should be treated as RIPv1 ignoring must be zero field. */
1861 if (packet
->version
> RIPv2
)
1862 packet
->version
= RIPv2
;
1864 /* Is RIP running or is this RIP neighbor ?*/
1865 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1866 if (IS_RIP_DEBUG_EVENT
)
1867 zlog_debug("RIP is not enabled on interface %s.",
1869 rip_peer_bad_packet(rip
, ri
, &from
);
1873 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1874 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1876 if (vrecv
== RI_RIP_VERSION_NONE
1877 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1878 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1879 if (IS_RIP_DEBUG_PACKET
)
1881 " packet's v%d doesn't fit to if version spec",
1883 rip_peer_bad_packet(rip
, ri
, &from
);
1887 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1888 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1889 accepted; authenticated RIP-2 messages shall be discarded. */
1890 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1891 && (packet
->version
== RIPv2
)
1892 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1893 if (IS_RIP_DEBUG_EVENT
)
1895 "packet RIPv%d is dropped because authentication disabled",
1897 ripd_notif_send_auth_type_failure(ifp
->name
);
1898 rip_peer_bad_packet(rip
, ri
, &from
);
1903 If the router is configured to authenticate RIP-2 messages, then
1904 RIP-1 messages and RIP-2 messages which pass authentication
1905 testing shall be accepted; unauthenticated and failed
1906 authentication RIP-2 messages shall be discarded. For maximum
1907 security, RIP-1 messages should be ignored when authentication is
1908 in use (see section 4.1); otherwise, the routing information from
1909 authenticated messages will be propagated by RIP-1 routers in an
1910 unauthenticated manner.
1912 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1913 * always reply regardless of authentication settings, because:
1915 * - if there other authorised routers on-link, the REQUESTor can
1916 * passively obtain the routing updates anyway
1917 * - if there are no other authorised routers on-link, RIP can
1918 * easily be disabled for the link to prevent giving out information
1919 * on state of this routers RIP routing table..
1921 * I.e. if RIPv1 has any place anymore these days, it's as a very
1922 * simple way to distribute routing information (e.g. to embedded
1923 * hosts / appliances) and the ability to give out RIPv1
1924 * routing-information freely, while still requiring RIPv2
1925 * authentication for any RESPONSEs might be vaguely useful.
1927 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1928 /* Discard RIPv1 messages other than REQUESTs */
1929 if (packet
->command
!= RIP_REQUEST
) {
1930 if (IS_RIP_DEBUG_PACKET
)
1932 "RIPv1 dropped because authentication enabled");
1933 ripd_notif_send_auth_type_failure(ifp
->name
);
1934 rip_peer_bad_packet(rip
, ri
, &from
);
1937 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1938 const char *auth_desc
;
1941 /* There definitely is no authentication in the packet.
1943 if (IS_RIP_DEBUG_PACKET
)
1945 "RIPv2 authentication failed: no auth RTE in packet");
1946 ripd_notif_send_auth_type_failure(ifp
->name
);
1947 rip_peer_bad_packet(rip
, ri
, &from
);
1951 /* First RTE must be an Authentication Family RTE */
1952 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1953 if (IS_RIP_DEBUG_PACKET
)
1955 "RIPv2 dropped because authentication enabled");
1956 ripd_notif_send_auth_type_failure(ifp
->name
);
1957 rip_peer_bad_packet(rip
, ri
, &from
);
1961 /* Check RIPv2 authentication. */
1962 switch (ntohs(packet
->rte
->tag
)) {
1963 case RIP_AUTH_SIMPLE_PASSWORD
:
1964 auth_desc
= "simple";
1965 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1970 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1971 /* Reset RIP packet length to trim MD5 data. */
1977 auth_desc
= "unknown type";
1978 if (IS_RIP_DEBUG_PACKET
)
1980 "RIPv2 Unknown authentication type %d",
1981 ntohs(packet
->rte
->tag
));
1985 if (IS_RIP_DEBUG_PACKET
)
1986 zlog_debug("RIPv2 %s authentication success",
1989 if (IS_RIP_DEBUG_PACKET
)
1990 zlog_debug("RIPv2 %s authentication failure",
1992 ripd_notif_send_auth_failure(ifp
->name
);
1993 rip_peer_bad_packet(rip
, ri
, &from
);
1998 /* Process each command. */
1999 switch (packet
->command
) {
2001 rip_response_process(packet
, len
, &from
, ifc
);
2005 rip_request_process(packet
, len
, &from
, ifc
);
2010 "Obsolete command %s received, please sent it to routed",
2011 lookup_msg(rip_msg
, packet
->command
, NULL
));
2012 rip_peer_bad_packet(rip
, ri
, &from
);
2014 case RIP_POLL_ENTRY
:
2015 zlog_info("Obsolete command %s received",
2016 lookup_msg(rip_msg
, packet
->command
, NULL
));
2017 rip_peer_bad_packet(rip
, ri
, &from
);
2020 zlog_info("Unknown RIP command %d received", packet
->command
);
2021 rip_peer_bad_packet(rip
, ri
, &from
);
2026 /* Write routing table entry to the stream and return next index of
2027 the routing table entry in the stream. */
2028 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2029 uint8_t version
, struct rip_info
*rinfo
)
2031 struct in_addr mask
;
2033 /* Write routing table entry. */
2034 if (version
== RIPv1
) {
2035 stream_putw(s
, AF_INET
);
2037 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2038 stream_put_ipv4(s
, 0);
2039 stream_put_ipv4(s
, 0);
2040 stream_putl(s
, rinfo
->metric_out
);
2042 masklen2ip(p
->prefixlen
, &mask
);
2044 stream_putw(s
, AF_INET
);
2045 stream_putw(s
, rinfo
->tag_out
);
2046 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2047 stream_put_ipv4(s
, mask
.s_addr
);
2048 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2049 stream_putl(s
, rinfo
->metric_out
);
2055 /* Send update to the ifp or spcified neighbor. */
2056 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2057 int route_type
, uint8_t version
)
2062 struct route_node
*rp
;
2063 struct rip_info
*rinfo
;
2064 struct rip_interface
*ri
;
2065 struct prefix_ipv4
*p
;
2066 struct prefix_ipv4 classfull
;
2067 struct prefix_ipv4 ifaddrclass
;
2068 struct key
*key
= NULL
;
2069 /* this might need to made dynamic if RIP ever supported auth methods
2070 with larger key string sizes */
2071 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2072 size_t doff
= 0; /* offset of digest offset field */
2076 struct list
*list
= NULL
;
2077 struct listnode
*listnode
= NULL
;
2079 /* Logging output event. */
2080 if (IS_RIP_DEBUG_EVENT
) {
2082 zlog_debug("update routes to neighbor %pI4",
2085 zlog_debug("update routes on interface %s ifindex %d",
2086 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2089 /* Get RIP interface. */
2090 ri
= ifc
->ifp
->info
;
2093 /* Set output stream. */
2096 /* Reset stream and RTE counter. */
2098 rtemax
= RIP_MAX_RTE
;
2100 /* If output interface is in simple password authentication mode, we
2101 need space for authentication data. */
2102 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2105 /* If output interface is in MD5 authentication mode, we need space
2106 for authentication header and data. */
2107 if (ri
->auth_type
== RIP_AUTH_MD5
)
2110 /* If output interface is in simple password authentication mode
2111 and string or keychain is specified we need space for auth. data */
2112 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2113 if (ri
->key_chain
) {
2114 struct keychain
*keychain
;
2116 keychain
= keychain_lookup(ri
->key_chain
);
2118 key
= key_lookup_for_send(keychain
);
2120 /* to be passed to auth functions later */
2121 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2122 if (strlen(auth_str
) == 0)
2126 if (version
== RIPv1
) {
2127 memcpy(&ifaddrclass
, ifc
->address
, sizeof(ifaddrclass
));
2128 apply_classful_mask_ipv4(&ifaddrclass
);
2130 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2134 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2140 if (listcount(list
) == 0)
2143 rinfo
= listgetdata(listhead(list
));
2145 * For RIPv1, if we are subnetted, output subnets in our
2146 * network that have the same mask as the output "interface".
2147 * For other networks, only the classfull version is output.
2149 if (version
== RIPv1
) {
2150 p
= (struct prefix_ipv4
*)&rp
->p
;
2152 if (IS_RIP_DEBUG_PACKET
)
2154 "RIPv1 mask check, %pFX considered for output",
2158 prefix_match((struct prefix
*)&ifaddrclass
,
2160 if ((ifc
->address
->prefixlen
!=
2162 (rp
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2165 memcpy(&classfull
, &rp
->p
,
2166 sizeof(struct prefix_ipv4
));
2167 apply_classful_mask_ipv4(&classfull
);
2168 if (rp
->p
.u
.prefix4
.s_addr
!= INADDR_ANY
&&
2169 classfull
.prefixlen
!= rp
->p
.prefixlen
)
2172 if (IS_RIP_DEBUG_PACKET
)
2174 "RIPv1 mask check, %pFX made it through",
2177 p
= (struct prefix_ipv4
*)&rp
->p
;
2179 /* Apply output filters. */
2180 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2184 /* Changed route only output. */
2185 if (route_type
== rip_changed_route
&&
2186 (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2189 /* Split horizon. */
2190 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2192 * We perform split horizon for RIP and connected
2193 * route. For rip routes, we want to suppress the
2194 * route if we would end up sending the route back on
2195 * the interface that we learned it from, with a
2196 * higher metric. For connected routes, we suppress
2197 * the route if the prefix is a subset of the source
2198 * address that we are going to use for the packet
2199 * (in order to handle the case when multiple subnets
2200 * are configured on the same interface).
2203 struct rip_info
*tmp_rinfo
= NULL
;
2204 struct connected
*tmp_ifc
= NULL
;
2206 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2207 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2208 tmp_rinfo
->nh
.ifindex
==
2209 ifc
->ifp
->ifindex
) {
2214 if (!suppress
&& rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2215 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2217 if (prefix_match((struct prefix
*)p
,
2218 tmp_ifc
->address
)) {
2228 /* Preparation for route-map. */
2229 rinfo
->metric_set
= 0;
2230 rinfo
->nexthop_out
.s_addr
= 0;
2231 rinfo
->metric_out
= rinfo
->metric
;
2232 rinfo
->tag_out
= rinfo
->tag
;
2233 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2235 /* In order to avoid some local loops, if the RIP route has
2236 * a nexthop via this interface, keep the nexthop, otherwise
2237 * set it to 0. The nexthop should not be propagated beyond
2238 * the local broadcast/multicast area in order to avoid an
2239 * IGP multi-level recursive look-up. see (4.4)
2241 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2242 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2244 /* Interface route-map */
2245 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2246 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_OUT
],
2247 (struct prefix
*)p
, rinfo
);
2249 if (ret
== RMAP_DENYMATCH
) {
2250 if (IS_RIP_DEBUG_PACKET
)
2252 "RIP %pFX is filtered by route-map out",
2258 /* Apply redistribute route map - continue, if deny */
2259 if (rip
->redist
[rinfo
->type
].route_map
.name
&&
2260 rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2261 ret
= route_map_apply(
2262 rip
->redist
[rinfo
->type
].route_map
.map
,
2263 (struct prefix
*)p
, rinfo
);
2265 if (ret
== RMAP_DENYMATCH
) {
2266 if (IS_RIP_DEBUG_PACKET
)
2268 "%pFX is filtered by route-map",
2274 /* When route-map does not set metric. */
2275 if (!rinfo
->metric_set
) {
2276 /* If redistribute metric is set. */
2277 if (rip
->redist
[rinfo
->type
].metric_config
&&
2278 rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2280 rip
->redist
[rinfo
->type
].metric
;
2282 /* If the route is not connected or localy
2283 * generated one, use default-metric value
2285 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&&
2286 rinfo
->type
!= ZEBRA_ROUTE_CONNECT
&&
2287 rinfo
->metric
!= RIP_METRIC_INFINITY
)
2288 rinfo
->metric_out
= rip
->default_metric
;
2292 /* Apply offset-list */
2293 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2294 rip_offset_list_apply_out(p
, ifc
->ifp
,
2295 &rinfo
->metric_out
);
2297 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2298 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2300 /* Perform split-horizon with poisoned reverse
2301 * for RIP and connected routes.
2303 if (ri
->split_horizon
== RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2305 * We perform split horizon for RIP and connected
2306 * route. For rip routes, we want to suppress the
2307 * route if we would end up sending the route back
2308 * on the interface that we learned it from, with a
2309 * higher metric. For connected routes, we suppress
2310 * the route if the prefix is a subset of the source
2311 * address that we are going to use for the packet
2312 * (in order to handle the case when multiple
2313 * subnets are configured on the same interface).
2315 struct rip_info
*tmp_rinfo
= NULL
;
2316 struct connected
*tmp_ifc
= NULL
;
2318 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2319 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2320 tmp_rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2321 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2323 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
&&
2324 rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2325 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2327 if (prefix_match((struct prefix
*)p
,
2328 tmp_ifc
->address
)) {
2330 RIP_METRIC_INFINITY
;
2336 /* Prepare preamble, auth headers, if needs be */
2338 stream_putc(s
, RIP_RESPONSE
);
2339 stream_putc(s
, version
);
2342 /* auth header for !v1 && !no_auth */
2343 if ((ri
->auth_type
!= RIP_NO_AUTH
) &&
2345 doff
= rip_auth_header_write(
2346 s
, ri
, key
, auth_str
,
2347 RIP_AUTH_SIMPLE_SIZE
);
2350 /* Write RTE to the stream. */
2351 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2352 if (num
== rtemax
) {
2353 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2354 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2355 RIP_AUTH_SIMPLE_SIZE
);
2357 ret
= rip_send_packet(STREAM_DATA(s
),
2358 stream_get_endp(s
), to
, ifc
);
2360 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2362 (struct rip_packet
*)STREAM_DATA(s
),
2363 stream_get_endp(s
), "SEND");
2369 /* Flush unwritten RTE. */
2371 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2372 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2373 RIP_AUTH_SIMPLE_SIZE
);
2375 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2378 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2379 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2380 stream_get_endp(s
), "SEND");
2384 /* Statistics updates. */
2388 /* Send RIP packet to the interface. */
2389 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2392 struct interface
*ifp
= ifc
->ifp
;
2393 struct rip_interface
*ri
= ifp
->info
;
2394 struct sockaddr_in to
;
2396 /* When RIP version is 2 and multicast enable interface. */
2397 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2398 if (IS_RIP_DEBUG_EVENT
)
2399 zlog_debug("multicast announce on %s ", ifp
->name
);
2401 rip_output_process(ifc
, NULL
, route_type
, version
);
2405 /* If we can't send multicast packet, send it with unicast. */
2406 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2407 if (ifc
->address
->family
== AF_INET
) {
2408 /* Destination address and port setting. */
2409 memset(&to
, 0, sizeof(to
));
2410 if (ifc
->destination
)
2411 /* use specified broadcast or peer destination
2413 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2414 else if (ifc
->address
->prefixlen
< IPV4_MAX_BITLEN
)
2415 /* calculate the appropriate broadcast address
2417 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2418 ifc
->address
->u
.prefix4
.s_addr
,
2419 ifc
->address
->prefixlen
);
2421 /* do not know where to send the packet */
2423 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2425 if (IS_RIP_DEBUG_EVENT
)
2426 zlog_debug("%s announce to %pI4 on %s",
2427 CONNECTED_PEER(ifc
) ? "unicast"
2429 &to
.sin_addr
, ifp
->name
);
2431 rip_output_process(ifc
, &to
, route_type
, version
);
2436 /* Update send to all interface and neighbor. */
2437 static void rip_update_process(struct rip
*rip
, int route_type
)
2439 struct listnode
*ifnode
, *ifnnode
;
2440 struct connected
*connected
;
2441 struct interface
*ifp
;
2442 struct rip_interface
*ri
;
2443 struct route_node
*rp
;
2444 struct sockaddr_in to
;
2447 /* Send RIP update to each interface. */
2448 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2449 if (if_is_loopback(ifp
))
2452 if (!if_is_operative(ifp
))
2455 /* Fetch RIP interface information. */
2458 /* When passive interface is specified, suppress announce to the
2467 * If there is no version configuration in the
2468 * interface, use rip's version setting.
2470 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? rip
->version_send
2473 if (IS_RIP_DEBUG_EVENT
)
2474 zlog_debug("SEND UPDATE to %s ifindex %d", ifp
->name
,
2477 /* send update on each connected network */
2478 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2480 if (connected
->address
->family
== AF_INET
) {
2482 rip_update_interface(connected
, RIPv1
,
2484 if ((vsend
& RIPv2
) && if_is_multicast(ifp
))
2485 rip_update_interface(connected
, RIPv2
,
2491 /* RIP send updates to each neighbor. */
2492 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
)) {
2493 if (rp
->info
== NULL
)
2498 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2502 "Neighbor %pI4 doesn't have connected interface!",
2507 /* Set destination address and port */
2508 memset(&to
, 0, sizeof(struct sockaddr_in
));
2509 to
.sin_addr
= p
->u
.prefix4
;
2510 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2512 /* RIP version is rip's configuration. */
2513 rip_output_process(connected
, &to
, route_type
,
2518 /* RIP's periodical timer. */
2519 static void rip_update(struct event
*t
)
2521 struct rip
*rip
= EVENT_ARG(t
);
2523 if (IS_RIP_DEBUG_EVENT
)
2524 zlog_debug("update timer fire!");
2526 /* Process update output. */
2527 rip_update_process(rip
, rip_all_route
);
2529 /* Triggered updates may be suppressed if a regular update is due by
2530 the time the triggered update would be sent. */
2531 EVENT_OFF(rip
->t_triggered_interval
);
2534 /* Register myself. */
2535 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2538 /* Walk down the RIP routing table then clear changed flag. */
2539 static void rip_clear_changed_flag(struct rip
*rip
)
2541 struct route_node
*rp
;
2542 struct rip_info
*rinfo
= NULL
;
2543 struct list
*list
= NULL
;
2544 struct listnode
*listnode
= NULL
;
2546 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2552 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2553 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2554 /* This flag can be set only on the first entry. */
2560 /* Triggered update interval timer. */
2561 static void rip_triggered_interval(struct event
*t
)
2563 struct rip
*rip
= EVENT_ARG(t
);
2567 rip_triggered_update(t
);
2571 /* Execute triggered update. */
2572 static void rip_triggered_update(struct event
*t
)
2574 struct rip
*rip
= EVENT_ARG(t
);
2577 /* Cancel interval timer. */
2578 EVENT_OFF(rip
->t_triggered_interval
);
2581 /* Logging triggered update. */
2582 if (IS_RIP_DEBUG_EVENT
)
2583 zlog_debug("triggered update!");
2585 /* Split Horizon processing is done when generating triggered
2586 updates as well as normal updates (see section 2.6). */
2587 rip_update_process(rip
, rip_changed_route
);
2589 /* Once all of the triggered updates have been generated, the route
2590 change flags should be cleared. */
2591 rip_clear_changed_flag(rip
);
2593 /* After a triggered update is sent, a timer should be set for a
2594 random interval between 1 and 5 seconds. If other changes that
2595 would trigger updates occur before the timer expires, a single
2596 update is triggered when the timer expires. */
2597 interval
= (frr_weak_random() % 5) + 1;
2599 event_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2600 &rip
->t_triggered_interval
);
2603 /* Withdraw redistributed route. */
2604 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2606 struct route_node
*rp
;
2607 struct rip_info
*rinfo
= NULL
;
2608 struct list
*list
= NULL
;
2610 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2616 rinfo
= listgetdata(listhead(list
));
2618 if (rinfo
->type
!= type
)
2621 if (rinfo
->sub_type
== RIP_ROUTE_INTERFACE
)
2624 /* Perform poisoned reverse. */
2625 rinfo
->metric
= RIP_METRIC_INFINITY
;
2626 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
2628 EVENT_OFF(rinfo
->t_timeout
);
2629 rinfo
->flags
|= RIP_RTF_CHANGED
;
2631 if (IS_RIP_DEBUG_EVENT
) {
2632 struct prefix_ipv4
*p
= (struct prefix_ipv4
*)&rp
->p
;
2635 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2637 ifindex2ifname(rinfo
->nh
.ifindex
,
2641 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2645 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2649 vrf
= vrf_lookup_by_id(vrf_id
);
2656 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2660 rip
.vrf_name
= (char *)vrf_name
;
2662 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2665 /* Update ECMP routes to zebra when `allow-ecmp` changed. */
2666 void rip_ecmp_change(struct rip
*rip
)
2668 struct route_node
*rp
;
2669 struct rip_info
*rinfo
;
2671 struct listnode
*node
, *nextnode
;
2673 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2675 if (list
&& listcount(list
) > 1) {
2676 while (listcount(list
) > rip
->ecmp
) {
2677 struct rip_info
*from_highest
= NULL
;
2679 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
,
2681 if (!from_highest
||
2685 &from_highest
->from
) > 0))
2686 from_highest
= rinfo
;
2689 rip_ecmp_delete(rip
, from_highest
);
2695 /* Create new RIP instance and set it to global variable. */
2696 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2700 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2701 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2703 /* Set initial value. */
2704 rip
->ecmp
= yang_get_default_uint8("%s/allow-ecmp", RIP_INSTANCE
);
2705 rip
->default_metric
=
2706 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2708 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2709 rip
->passive_default
=
2710 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2711 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2713 rip
->timeout_time
= yang_get_default_uint32(
2714 "%s/timers/holddown-interval", RIP_INSTANCE
);
2715 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2718 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2720 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2722 /* Initialize RIP data structures. */
2723 rip
->table
= route_table_init();
2724 route_table_set_info(rip
->table
, rip
);
2725 rip
->neighbor
= route_table_init();
2726 rip
->peer_list
= list_new();
2727 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2728 rip
->peer_list
->del
= rip_peer_list_del
;
2729 rip
->distance_table
= route_table_init();
2730 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2731 rip
->enable_interface
= vector_init(1);
2732 rip
->enable_network
= route_table_init();
2733 rip
->passive_nondefault
= vector_init(1);
2734 rip
->offset_list_master
= list_new();
2735 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2736 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2738 /* Distribute list install. */
2739 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2740 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2741 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2743 /* if rmap install. */
2744 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2745 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2746 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2748 /* Make output stream. */
2749 rip
->obuf
= stream_new(1500);
2751 /* Enable the routing instance if possible. */
2752 if (vrf
&& vrf_is_enabled(vrf
))
2753 rip_instance_enable(rip
, vrf
, socket
);
2759 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2764 /* Sned RIP request to the destination. */
2765 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2766 uint8_t version
, struct connected
*connected
)
2769 struct rip_packet rip_packet
;
2770 struct listnode
*node
, *nnode
;
2772 memset(&rip_packet
, 0, sizeof(rip_packet
));
2774 rip_packet
.command
= RIP_REQUEST
;
2775 rip_packet
.version
= version
;
2776 rte
= rip_packet
.rte
;
2777 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2781 * connected is only sent for ripv1 case, or when
2782 * interface does not support multicast. Caller loops
2783 * over each connected address for this case.
2785 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2787 != sizeof(rip_packet
))
2790 return sizeof(rip_packet
);
2793 /* send request on each connected network */
2794 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2795 struct prefix_ipv4
*p
;
2797 p
= (struct prefix_ipv4
*)connected
->address
;
2799 if (p
->family
!= AF_INET
)
2802 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2804 != sizeof(rip_packet
))
2807 return sizeof(rip_packet
);
2810 static int rip_update_jitter(unsigned long time
)
2812 #define JITTER_BOUND 4
2813 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2814 Given that, we cannot let time be less than JITTER_BOUND seconds.
2815 The RIPv2 RFC says jitter should be small compared to
2816 update_time. We consider 1/JITTER_BOUND to be small.
2819 int jitter_input
= time
;
2822 if (jitter_input
< JITTER_BOUND
)
2823 jitter_input
= JITTER_BOUND
;
2825 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2828 return jitter
/ JITTER_BOUND
;
2831 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2837 event_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2839 case RIP_UPDATE_EVENT
:
2840 EVENT_OFF(rip
->t_update
);
2841 jitter
= rip_update_jitter(rip
->update_time
);
2842 event_add_timer(master
, rip_update
, rip
,
2843 sock
? 2 : rip
->update_time
+ jitter
,
2846 case RIP_TRIGGERED_UPDATE
:
2847 if (rip
->t_triggered_interval
)
2850 event_add_event(master
, rip_triggered_update
, rip
, 0,
2851 &rip
->t_triggered_update
);
2858 struct rip_distance
*rip_distance_new(void)
2860 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2863 void rip_distance_free(struct rip_distance
*rdistance
)
2865 if (rdistance
->access_list
)
2866 free(rdistance
->access_list
);
2867 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2870 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2871 struct route_node
*node
)
2873 struct rip_distance
*rdistance
;
2875 rdistance
= node
->info
;
2877 rip_distance_free(rdistance
);
2880 /* Apply RIP information to distance method. */
2881 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2883 struct route_node
*rn
;
2884 struct prefix_ipv4 p
;
2885 struct rip_distance
*rdistance
;
2886 struct access_list
*alist
;
2888 memset(&p
, 0, sizeof(p
));
2890 p
.prefix
= rinfo
->from
;
2891 p
.prefixlen
= IPV4_MAX_BITLEN
;
2893 /* Check source address. */
2894 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2896 rdistance
= rn
->info
;
2897 route_unlock_node(rn
);
2899 if (rdistance
->access_list
) {
2900 alist
= access_list_lookup(AFI_IP
,
2901 rdistance
->access_list
);
2904 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2908 return rdistance
->distance
;
2911 return rip
->distance
;
2914 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2916 struct route_node
*rn
;
2917 struct rip_distance
*rdistance
;
2921 vty_out(vty
, " Distance: (default is %u)\n",
2922 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2924 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
)) {
2925 rdistance
= rn
->info
;
2927 if (rdistance
== NULL
)
2931 vty_out(vty
, " Address Distance List\n");
2934 snprintfrr(buf
, sizeof(buf
), "%pFX", &rn
->p
);
2935 vty_out(vty
, " %-20s %4d %s\n", buf
, rdistance
->distance
,
2936 rdistance
->access_list
? rdistance
->access_list
: "");
2940 /* Update ECMP routes to zebra when ECMP is disabled. */
2941 void rip_ecmp_disable(struct rip
*rip
)
2943 struct route_node
*rp
;
2944 struct rip_info
*rinfo
, *tmp_rinfo
;
2946 struct listnode
*node
, *nextnode
;
2948 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2953 if (listcount(list
) == 0)
2956 rinfo
= listgetdata(listhead(list
));
2957 if (!rip_route_rte(rinfo
))
2960 /* Drop all other entries, except the first one. */
2961 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
2962 if (tmp_rinfo
== rinfo
)
2965 EVENT_OFF(tmp_rinfo
->t_timeout
);
2966 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
2967 list_delete_node(list
, node
);
2968 rip_info_free(tmp_rinfo
);
2972 rip_zebra_ipv4_add(rip
, rp
);
2974 /* Set the route change flag. */
2975 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2977 /* Signal the output process to trigger an update. */
2978 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2982 /* Print out routes update time. */
2983 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2988 char timebuf
[TIME_BUF
];
2989 struct event
*thread
;
2991 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2992 clock
= event_timer_remain_second(thread
);
2993 gmtime_r(&clock
, &tm
);
2994 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2995 vty_out(vty
, "%5s", timebuf
);
2996 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2997 clock
= event_timer_remain_second(thread
);
2998 gmtime_r(&clock
, &tm
);
2999 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
3000 vty_out(vty
, "%5s", timebuf
);
3004 static const char *rip_route_type_print(int sub_type
)
3009 case RIP_ROUTE_STATIC
:
3011 case RIP_ROUTE_DEFAULT
:
3013 case RIP_ROUTE_REDISTRIBUTE
:
3015 case RIP_ROUTE_INTERFACE
:
3024 "show ip rip [vrf NAME]",
3031 struct route_node
*np
;
3032 struct rip_info
*rinfo
= NULL
;
3033 struct list
*list
= NULL
;
3034 struct listnode
*listnode
= NULL
;
3035 const char *vrf_name
;
3038 if (argv_find(argv
, argc
, "vrf", &idx
))
3039 vrf_name
= argv
[idx
+ 1]->arg
;
3041 vrf_name
= VRF_DEFAULT_NAME
;
3043 rip
= rip_lookup_by_vrf_name(vrf_name
);
3045 vty_out(vty
, "%% RIP instance not found\n");
3048 if (!rip
->enabled
) {
3049 vty_out(vty
, "%% RIP instance is disabled\n");
3054 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3056 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3057 " (i) - interface\n\n"
3058 " Network Next Hop Metric From Tag Time\n");
3060 for (np
= route_top(rip
->table
); np
; np
= route_next(np
)) {
3066 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3069 len
= vty_out(vty
, "%c(%s) %pFX",
3070 /* np->lock, For debugging. */
3071 zebra_route_char(rinfo
->type
),
3072 rip_route_type_print(rinfo
->sub_type
),
3078 vty_out(vty
, "%*s", len
, " ");
3080 switch (rinfo
->nh
.type
) {
3081 case NEXTHOP_TYPE_IPV4
:
3082 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3083 vty_out(vty
, "%-20pI4 %2d ",
3084 &rinfo
->nh
.gate
.ipv4
, rinfo
->metric
);
3086 case NEXTHOP_TYPE_IFINDEX
:
3087 vty_out(vty
, "0.0.0.0 %2d ",
3090 case NEXTHOP_TYPE_BLACKHOLE
:
3091 vty_out(vty
, "blackhole %2d ",
3094 case NEXTHOP_TYPE_IPV6
:
3095 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3096 vty_out(vty
, "V6 Address Hidden %2d ",
3101 /* Route which exist in kernel routing table. */
3102 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
) &&
3103 (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3104 vty_out(vty
, "%-15pI4 ", &rinfo
->from
);
3105 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3106 (route_tag_t
)rinfo
->tag
);
3107 rip_vty_out_uptime(vty
, rinfo
);
3108 } else if (rinfo
->metric
== RIP_METRIC_INFINITY
) {
3109 vty_out(vty
, "self ");
3110 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3111 (route_tag_t
)rinfo
->tag
);
3112 rip_vty_out_uptime(vty
, rinfo
);
3114 if (rinfo
->external_metric
) {
3116 vty
, "self (%s:%d)",
3117 zebra_route_string(rinfo
->type
),
3118 rinfo
->external_metric
);
3121 vty_out(vty
, "%*s", len
, " ");
3123 vty_out(vty
, "self ");
3124 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3125 (route_tag_t
)rinfo
->tag
);
3134 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3135 DEFUN (show_ip_rip_status
,
3136 show_ip_rip_status_cmd
,
3137 "show ip rip [vrf NAME] status",
3142 "IP routing protocol process parameters and statistics\n")
3145 struct interface
*ifp
;
3146 struct rip_interface
*ri
;
3147 extern const struct message ri_version_msg
[];
3148 const char *send_version
;
3149 const char *receive_version
;
3150 const char *vrf_name
;
3153 if (argv_find(argv
, argc
, "vrf", &idx
))
3154 vrf_name
= argv
[idx
+ 1]->arg
;
3156 vrf_name
= VRF_DEFAULT_NAME
;
3158 rip
= rip_lookup_by_vrf_name(vrf_name
);
3160 vty_out(vty
, "%% RIP instance not found\n");
3163 if (!rip
->enabled
) {
3164 vty_out(vty
, "%% RIP instance is disabled\n");
3168 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3169 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3171 vty_out(vty
, " next due in %lu seconds\n",
3172 event_timer_remain_second(rip
->t_update
));
3173 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3174 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3176 /* Filtering status show. */
3177 config_show_distribute(vty
, rip
->distribute_ctx
);
3179 /* Default metric information. */
3180 vty_out(vty
, " Default redistribution metric is %u\n",
3181 rip
->default_metric
);
3183 /* Redistribute information. */
3184 vty_out(vty
, " Redistributing:");
3185 rip_show_redistribute_config(vty
, rip
);
3188 vty_out(vty
, " Default version control: send version %s,",
3189 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3190 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3191 vty_out(vty
, " receive any version \n");
3193 vty_out(vty
, " receive version %s \n",
3194 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3196 vty_out(vty
, " Interface Send Recv Key-chain\n");
3198 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3204 if (ri
->enable_network
|| ri
->enable_interface
) {
3205 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3207 lookup_msg(ri_version_msg
,
3208 rip
->version_send
, NULL
);
3210 send_version
= lookup_msg(ri_version_msg
,
3213 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3215 lookup_msg(ri_version_msg
,
3216 rip
->version_recv
, NULL
);
3218 receive_version
= lookup_msg(
3219 ri_version_msg
, ri
->ri_receive
, NULL
);
3221 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3222 send_version
, receive_version
,
3223 ri
->key_chain
? ri
->key_chain
: "");
3227 vty_out(vty
, " Routing for Networks:\n");
3228 rip_show_network_config(vty
, rip
);
3230 int found_passive
= 0;
3231 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3234 if ((ri
->enable_network
|| ri
->enable_interface
) &&
3236 if (!found_passive
) {
3237 vty_out(vty
, " Passive Interface(s):\n");
3240 vty_out(vty
, " %s\n", ifp
->name
);
3244 vty_out(vty
, " Routing Information Sources:\n");
3246 " Gateway BadPackets BadRoutes Distance Last Update\n");
3247 rip_peer_display(vty
, rip
);
3249 rip_distance_show(vty
, rip
);
3254 /* RIP configuration write function. */
3255 static int config_write_rip(struct vty
*vty
)
3260 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3261 char xpath
[XPATH_MAXLEN
];
3262 struct lyd_node
*dnode
;
3264 snprintf(xpath
, sizeof(xpath
),
3265 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3267 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3270 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3272 /* Distribute configuration. */
3273 config_write_distribute(vty
, rip
->distribute_ctx
);
3275 vty_out(vty
, "exit\n");
3283 static int config_write_rip(struct vty
*vty
);
3284 /* RIP node structure. */
3285 static struct cmd_node rip_node
= {
3288 .parent_node
= CONFIG_NODE
,
3289 .prompt
= "%s(config-router)# ",
3290 .config_write
= config_write_rip
,
3293 /* Distribute-list update functions. */
3294 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3295 struct distribute
*dist
)
3297 struct interface
*ifp
;
3298 struct rip_interface
*ri
;
3299 struct access_list
*alist
;
3300 struct prefix_list
*plist
;
3302 if (!ctx
->vrf
|| !dist
->ifname
)
3305 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3311 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3312 alist
= access_list_lookup(AFI_IP
,
3313 dist
->list
[DISTRIBUTE_V4_IN
]);
3315 ri
->list
[RIP_FILTER_IN
] = alist
;
3317 ri
->list
[RIP_FILTER_IN
] = NULL
;
3319 ri
->list
[RIP_FILTER_IN
] = NULL
;
3321 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3322 alist
= access_list_lookup(AFI_IP
,
3323 dist
->list
[DISTRIBUTE_V4_OUT
]);
3325 ri
->list
[RIP_FILTER_OUT
] = alist
;
3327 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3329 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3331 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3332 plist
= prefix_list_lookup(AFI_IP
,
3333 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3335 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3337 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3339 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3341 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3342 plist
= prefix_list_lookup(AFI_IP
,
3343 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3345 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3347 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3349 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3352 void rip_distribute_update_interface(struct interface
*ifp
)
3354 struct rip_interface
*ri
= ifp
->info
;
3355 struct rip
*rip
= ri
->rip
;
3356 struct distribute
*dist
;
3360 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3362 rip_distribute_update(rip
->distribute_ctx
, dist
);
3365 /* Update all interface's distribute list. */
3367 static void rip_distribute_update_all(struct prefix_list
*notused
)
3369 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3370 struct interface
*ifp
;
3372 FOR_ALL_INTERFACES (vrf
, ifp
)
3373 rip_distribute_update_interface(ifp
);
3376 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3378 rip_distribute_update_all(NULL
);
3381 /* Delete all added rip route. */
3382 void rip_clean(struct rip
*rip
)
3384 rip_interfaces_clean(rip
);
3387 rip_instance_disable(rip
);
3389 stream_free(rip
->obuf
);
3391 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3392 if (rip
->redist
[i
].route_map
.name
)
3393 free(rip
->redist
[i
].route_map
.name
);
3395 route_table_finish(rip
->table
);
3396 route_table_finish(rip
->neighbor
);
3397 list_delete(&rip
->peer_list
);
3398 distribute_list_delete(&rip
->distribute_ctx
);
3399 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3401 rip_clean_network(rip
);
3402 rip_passive_nondefault_clean(rip
);
3403 vector_free(rip
->enable_interface
);
3404 route_table_finish(rip
->enable_network
);
3405 vector_free(rip
->passive_nondefault
);
3406 list_delete(&rip
->offset_list_master
);
3407 route_table_finish(rip
->distance_table
);
3409 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3410 XFREE(MTYPE_RIP_BFD_PROFILE
, rip
->default_bfd_profile
);
3411 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3412 XFREE(MTYPE_RIP
, rip
);
3415 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3416 struct if_rmap
*if_rmap
)
3418 struct interface
*ifp
= NULL
;
3419 struct rip_interface
*ri
;
3420 struct route_map
*rmap
;
3421 struct vrf
*vrf
= NULL
;
3424 vrf
= vrf_lookup_by_name(ctx
->name
);
3426 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3431 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3432 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3434 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3436 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3438 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3440 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3441 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3443 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3445 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3447 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3450 void rip_if_rmap_update_interface(struct interface
*ifp
)
3452 struct rip_interface
*ri
= ifp
->info
;
3453 struct rip
*rip
= ri
->rip
;
3454 struct if_rmap
*if_rmap
;
3455 struct if_rmap_ctx
*ctx
;
3459 ctx
= rip
->if_rmap_ctx
;
3462 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3464 rip_if_rmap_update(ctx
, if_rmap
);
3467 static void rip_routemap_update_redistribute(struct rip
*rip
)
3469 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3470 if (rip
->redist
[i
].route_map
.name
) {
3471 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3472 rip
->redist
[i
].route_map
.name
);
3473 route_map_counter_increment(
3474 rip
->redist
[i
].route_map
.map
);
3480 static void rip_routemap_update(const char *notused
)
3482 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3484 struct interface
*ifp
;
3486 FOR_ALL_INTERFACES (vrf
, ifp
)
3487 rip_if_rmap_update_interface(ifp
);
3491 rip_routemap_update_redistribute(rip
);
3494 /* Link RIP instance to VRF. */
3495 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3497 struct interface
*ifp
;
3500 rip
->distribute_ctx
->vrf
= vrf
;
3503 FOR_ALL_INTERFACES (vrf
, ifp
)
3504 rip_interface_sync(ifp
);
3507 /* Unlink RIP instance from VRF. */
3508 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3510 struct interface
*ifp
;
3513 rip
->distribute_ctx
->vrf
= NULL
;
3516 FOR_ALL_INTERFACES (vrf
, ifp
)
3517 rip_interface_sync(ifp
);
3520 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3524 rip_vrf_link(rip
, vrf
);
3525 rip
->enabled
= true;
3527 /* Resend all redistribute requests. */
3528 rip_redistribute_enable(rip
);
3530 /* Create read and timer thread. */
3531 rip_event(rip
, RIP_READ
, rip
->sock
);
3532 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3534 rip_zebra_vrf_register(vrf
);
3537 static void rip_instance_disable(struct rip
*rip
)
3539 struct vrf
*vrf
= rip
->vrf
;
3540 struct route_node
*rp
;
3542 /* Clear RIP routes */
3543 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3544 struct rip_info
*rinfo
;
3546 struct listnode
*listnode
;
3548 if ((list
= rp
->info
) == NULL
)
3551 rinfo
= listgetdata(listhead(list
));
3552 if (rip_route_rte(rinfo
))
3553 rip_zebra_ipv4_delete(rip
, rp
);
3555 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3556 EVENT_OFF(rinfo
->t_timeout
);
3557 EVENT_OFF(rinfo
->t_garbage_collect
);
3558 rip_info_free(rinfo
);
3562 route_unlock_node(rp
);
3565 /* Flush all redistribute requests. */
3566 rip_redistribute_disable(rip
);
3568 /* Cancel RIP related timers. */
3569 EVENT_OFF(rip
->t_update
);
3570 EVENT_OFF(rip
->t_triggered_update
);
3571 EVENT_OFF(rip
->t_triggered_interval
);
3573 /* Cancel read thread. */
3574 EVENT_OFF(rip
->t_read
);
3576 /* Close RIP socket. */
3580 /* Clear existing peers. */
3581 list_delete_all_node(rip
->peer_list
);
3583 rip_zebra_vrf_deregister(vrf
);
3585 rip_vrf_unlink(rip
, vrf
);
3586 rip
->enabled
= false;
3589 static int rip_vrf_new(struct vrf
*vrf
)
3591 if (IS_RIP_DEBUG_EVENT
)
3592 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3598 static int rip_vrf_delete(struct vrf
*vrf
)
3602 if (IS_RIP_DEBUG_EVENT
)
3603 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3606 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3615 static int rip_vrf_enable(struct vrf
*vrf
)
3620 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3621 if (!rip
|| rip
->enabled
)
3624 if (IS_RIP_DEBUG_EVENT
)
3625 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3628 /* Activate the VRF RIP instance. */
3629 if (!rip
->enabled
) {
3630 socket
= rip_create_socket(vrf
);
3634 rip_instance_enable(rip
, vrf
, socket
);
3640 static int rip_vrf_disable(struct vrf
*vrf
)
3644 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3645 if (!rip
|| !rip
->enabled
)
3648 if (IS_RIP_DEBUG_EVENT
)
3649 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3652 /* Deactivate the VRF RIP instance. */
3654 rip_instance_disable(rip
);
3659 void rip_vrf_init(void)
3661 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
);
3666 void rip_vrf_terminate(void)
3671 /* Allocate new rip structure and set default value. */
3674 /* Install top nodes. */
3675 install_node(&rip_node
);
3677 /* Install rip commands. */
3678 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3679 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3681 install_default(RIP_NODE
);
3683 /* Debug related init. */
3686 /* Access list install. */
3688 access_list_add_hook(rip_distribute_update_all_wrapper
);
3689 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3691 /* Prefix list initialize.*/
3693 prefix_list_add_hook(rip_distribute_update_all
);
3694 prefix_list_delete_hook(rip_distribute_update_all
);
3697 rip_route_map_init();
3699 route_map_add_hook(rip_routemap_update
);
3700 route_map_delete_hook(rip_routemap_update
);
3702 if_rmap_init(RIP_NODE
);