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_debug.h"
38 #include "ripd/rip_errors.h"
39 #include "ripd/rip_interface.h"
41 /* UDP receive buffer size */
42 #define RIP_UDP_RCV_BUF 41600
44 DEFINE_MGROUP(RIPD
, "ripd");
45 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure");
46 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name");
47 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info");
48 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance");
51 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
53 static void rip_triggered_update(struct event
*);
54 static int rip_update_jitter(unsigned long);
55 static void rip_distance_table_node_cleanup(struct route_table
*table
,
56 struct route_node
*node
);
57 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
58 static void rip_instance_disable(struct rip
*rip
);
60 static void rip_distribute_update(struct distribute_ctx
*ctx
,
61 struct distribute
*dist
);
63 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
64 struct if_rmap
*if_rmap
);
66 /* RIP output routes type. */
67 enum { rip_all_route
, rip_changed_route
};
69 /* RIP command strings. */
70 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
71 {RIP_RESPONSE
, "RESPONSE"},
72 {RIP_TRACEON
, "TRACEON"},
73 {RIP_TRACEOFF
, "TRACEOFF"},
75 {RIP_POLL_ENTRY
, "POLL ENTRY"},
78 /* Generate rb-tree of RIP instances. */
79 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
81 return strcmp(a
->vrf_name
, b
->vrf_name
);
83 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
85 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
87 /* Utility function to set broadcast option to the socket. */
88 static int sockopt_broadcast(int sock
)
93 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
96 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
102 int rip_route_rte(struct rip_info
*rinfo
)
104 return (rinfo
->type
== ZEBRA_ROUTE_RIP
105 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
108 static struct rip_info
*rip_info_new(void)
110 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
113 void rip_info_free(struct rip_info
*rinfo
)
115 XFREE(MTYPE_RIP_INFO
, rinfo
);
118 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
120 return route_table_get_info(rinfo
->rp
->table
);
123 /* RIP route garbage collect timer. */
124 static void rip_garbage_collect(struct event
*t
)
126 struct rip_info
*rinfo
;
127 struct route_node
*rp
;
129 rinfo
= EVENT_ARG(t
);
131 /* Off timeout timer. */
132 EVENT_OFF(rinfo
->t_timeout
);
134 /* Get route_node pointer. */
137 /* Unlock route_node. */
138 listnode_delete(rp
->info
, rinfo
);
139 if (list_isempty((struct list
*)rp
->info
)) {
140 list_delete((struct list
**)&rp
->info
);
141 route_unlock_node(rp
);
144 /* Free RIP routing information. */
145 rip_info_free(rinfo
);
148 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
150 /* Add new route to the ECMP list.
151 * RETURN: the new entry added in the list, or NULL if it is not the first
152 * entry and ECMP is not allowed.
154 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
156 struct route_node
*rp
= rinfo_new
->rp
;
157 struct rip_info
*rinfo
= NULL
;
158 struct rip_info
*rinfo_exist
= NULL
;
159 struct list
*list
= NULL
;
160 struct listnode
*node
= NULL
;
161 struct listnode
*nnode
= NULL
;
163 if (rp
->info
== NULL
)
164 rp
->info
= list_new();
165 list
= (struct list
*)rp
->info
;
167 /* If ECMP is not allowed and some entry already exists in the list,
169 if (listcount(list
) && !rip
->ecmp
)
172 /* Add or replace an existing ECMP path with lower neighbor IP */
173 if (listcount(list
) && listcount(list
) >= rip
->ecmp
) {
174 struct rip_info
*from_highest
= NULL
;
176 /* Find the rip_info struct that has the highest nexthop IP */
177 for (ALL_LIST_ELEMENTS(list
, node
, nnode
, rinfo_exist
))
180 IPV4_ADDR_CMP(&rinfo_exist
->from
,
181 &from_highest
->from
) > 0)) {
182 from_highest
= rinfo_exist
;
185 /* If we have a route in ECMP group, delete the old
186 * one that has a higher next-hop address. Lower IP is
189 if (rip
->ecmp
> 1 && from_highest
&&
190 IPV4_ADDR_CMP(&from_highest
->from
, &rinfo_new
->from
) > 0) {
191 rip_ecmp_delete(rip
, from_highest
);
199 rinfo
= rip_info_new();
200 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
201 listnode_add(list
, rinfo
);
203 if (rip_route_rte(rinfo
)) {
204 rip_timeout_update(rip
, rinfo
);
205 rip_zebra_ipv4_add(rip
, rp
);
208 /* Set the route change flag on the first entry. */
209 rinfo
= listgetdata(listhead(list
));
210 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
212 /* Signal the output process to trigger an update (see section 2.5). */
213 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
218 /* Replace the ECMP list with the new route.
219 * RETURN: the new entry added in the list
221 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
223 struct route_node
*rp
= rinfo_new
->rp
;
224 struct list
*list
= (struct list
*)rp
->info
;
225 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
226 struct listnode
*node
= NULL
, *nextnode
= NULL
;
228 if (list
== NULL
|| listcount(list
) == 0)
229 return rip_ecmp_add(rip
, rinfo_new
);
231 /* Get the first entry */
232 rinfo
= listgetdata(listhead(list
));
234 /* Learnt route replaced by a local one. Delete it from zebra. */
235 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
236 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
237 rip_zebra_ipv4_delete(rip
, rp
);
239 /* Re-use the first entry, and delete the others. */
240 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
241 if (tmp_rinfo
== rinfo
)
244 EVENT_OFF(tmp_rinfo
->t_timeout
);
245 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
246 list_delete_node(list
, node
);
247 rip_info_free(tmp_rinfo
);
250 EVENT_OFF(rinfo
->t_timeout
);
251 EVENT_OFF(rinfo
->t_garbage_collect
);
252 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
254 if (rip_route_rte(rinfo
)) {
255 rip_timeout_update(rip
, rinfo
);
256 /* The ADD message implies an update. */
257 rip_zebra_ipv4_add(rip
, rp
);
260 /* Set the route change flag. */
261 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
263 /* Signal the output process to trigger an update (see section 2.5). */
264 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
269 /* Delete one route from the ECMP list.
271 * null - the entry is freed, and other entries exist in the list
272 * the entry - the entry is the last one in the list; its metric is set
273 * to INFINITY, and the garbage collector is started for it
275 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
277 struct route_node
*rp
= rinfo
->rp
;
278 struct list
*list
= (struct list
*)rp
->info
;
280 EVENT_OFF(rinfo
->t_timeout
);
282 if (listcount(list
) > 1) {
283 /* Some other ECMP entries still exist. Just delete this entry.
285 EVENT_OFF(rinfo
->t_garbage_collect
);
286 listnode_delete(list
, rinfo
);
287 if (rip_route_rte(rinfo
)
288 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
289 /* The ADD message implies the update. */
290 rip_zebra_ipv4_add(rip
, rp
);
291 rip_info_free(rinfo
);
294 assert(rinfo
== listgetdata(listhead(list
)));
296 /* This is the only entry left in the list. We must keep it in
297 * the list for garbage collection time, with INFINITY metric.
300 rinfo
->metric
= RIP_METRIC_INFINITY
;
301 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
304 if (rip_route_rte(rinfo
)
305 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
306 rip_zebra_ipv4_delete(rip
, rp
);
309 /* Set the route change flag on the first entry. */
310 rinfo
= listgetdata(listhead(list
));
311 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
313 /* Signal the output process to trigger an update (see section 2.5). */
314 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
319 /* Timeout RIP routes. */
320 static void rip_timeout(struct event
*t
)
322 struct rip_info
*rinfo
= EVENT_ARG(t
);
323 struct rip
*rip
= rip_info_get_instance(rinfo
);
325 rip_ecmp_delete(rip
, rinfo
);
328 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
330 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
331 EVENT_OFF(rinfo
->t_timeout
);
332 event_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
337 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
338 struct rip_interface
*ri
)
340 struct distribute
*dist
;
341 struct access_list
*alist
;
342 struct prefix_list
*plist
;
343 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
345 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
347 /* Input distribute-list filtering. */
348 if (ri
->list
[rip_distribute
] &&
349 access_list_apply(ri
->list
[rip_distribute
], (struct prefix
*)p
) ==
351 if (IS_RIP_DEBUG_PACKET
)
352 zlog_debug("%pFX filtered by distribute %s", p
, inout
);
356 if (ri
->prefix
[rip_distribute
] &&
357 prefix_list_apply(ri
->prefix
[rip_distribute
], (struct prefix
*)p
) ==
359 if (IS_RIP_DEBUG_PACKET
)
360 zlog_debug("%pFX filtered by prefix-list %s", p
, inout
);
364 /* All interface filter check. */
365 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
369 if (dist
->list
[distribute
]) {
370 alist
= access_list_lookup(AFI_IP
, dist
->list
[distribute
]);
373 if (access_list_apply(alist
, (struct prefix
*)p
) ==
375 if (IS_RIP_DEBUG_PACKET
)
377 "%pFX filtered by distribute %s",
383 if (dist
->prefix
[distribute
]) {
384 plist
= prefix_list_lookup(AFI_IP
, dist
->prefix
[distribute
]);
387 if (prefix_list_apply(plist
, (struct prefix
*)p
) ==
389 if (IS_RIP_DEBUG_PACKET
)
391 "%pFX filtered by prefix-list %s",
401 /* Check nexthop address validity. */
402 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
404 struct interface
*ifp
;
405 struct listnode
*cnode
;
406 struct connected
*ifc
;
409 /* If nexthop address matches local configured address then it is
412 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
413 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
416 if (p
->family
== AF_INET
417 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
424 /* RIP add route to routing table. */
425 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
426 struct interface
*ifp
)
430 struct prefix_ipv4 p
;
431 struct route_node
*rp
;
432 struct rip_info
*rinfo
= NULL
, newinfo
;
433 struct rip_interface
*ri
;
434 struct in_addr
*nexthop
;
436 unsigned char old_dist
, new_dist
;
437 struct list
*list
= NULL
;
438 struct listnode
*node
= NULL
;
440 /* Make prefix structure. */
441 memset(&p
, 0, sizeof(struct prefix_ipv4
));
443 p
.prefix
= rte
->prefix
;
444 p
.prefixlen
= ip_masklen(rte
->mask
);
446 /* Make sure mask is applied. */
452 /* Apply input filters. */
453 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
457 memset(&newinfo
, 0, sizeof(newinfo
));
458 newinfo
.type
= ZEBRA_ROUTE_RIP
;
459 newinfo
.sub_type
= RIP_ROUTE_RTE
;
460 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
461 newinfo
.from
= from
->sin_addr
;
462 newinfo
.nh
.ifindex
= ifp
->ifindex
;
463 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
464 newinfo
.metric
= rte
->metric
;
465 newinfo
.metric_out
= rte
->metric
; /* XXX */
466 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
468 /* Modify entry according to the interface routemap. */
469 if (ri
->routemap
[RIP_FILTER_IN
]) {
470 /* The object should be of the type of rip_info */
471 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
472 (struct prefix
*)&p
, &newinfo
);
474 if (ret
== RMAP_DENYMATCH
) {
475 if (IS_RIP_DEBUG_PACKET
)
477 "RIP %pFX is filtered by route-map in",
482 /* Get back the object */
483 rte
->nexthop
= newinfo
.nexthop_out
;
484 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
485 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
489 /* Once the entry has been validated, update the metric by
490 adding the cost of the network on which the message
491 arrived. If the result is greater than infinity, use infinity
492 (RFC2453 Sec. 3.9.2) */
493 /* Zebra ripd can handle offset-list in. */
494 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
496 /* If offset-list does not modify the metric use interface's
499 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
501 if (rte
->metric
> RIP_METRIC_INFINITY
)
502 rte
->metric
= RIP_METRIC_INFINITY
;
504 /* Set nexthop pointer. */
505 if (rte
->nexthop
.s_addr
== INADDR_ANY
)
506 nexthop
= &from
->sin_addr
;
508 nexthop
= &rte
->nexthop
;
510 /* Check if nexthop address is myself, then do nothing. */
511 if (rip_nexthop_check(rip
, nexthop
) < 0) {
512 if (IS_RIP_DEBUG_PACKET
)
513 zlog_debug("Nexthop address %pI4 is myself",
518 /* Get index for the prefix. */
519 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
522 newinfo
.nh
.gate
.ipv4
= *nexthop
;
523 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
524 newinfo
.metric
= rte
->metric
;
525 newinfo
.tag
= ntohs(rte
->tag
);
526 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
528 new_dist
= newinfo
.distance
? newinfo
.distance
529 : ZEBRA_RIP_DISTANCE_DEFAULT
;
531 /* Check to see whether there is already RIP route on the table. */
532 if ((list
= rp
->info
) != NULL
)
533 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
534 /* Need to compare with redistributed entry or local
536 if (!rip_route_rte(rinfo
))
539 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
540 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
543 if (listnextnode(node
))
546 /* Not found in the list */
548 if (rte
->metric
> rinfo
->metric
) {
549 /* New route has a greater metric.
551 route_unlock_node(rp
);
555 if (rte
->metric
< rinfo
->metric
)
556 /* New route has a smaller metric.
557 * Replace the ECMP list
558 * with the new one in below. */
561 /* Metrics are same. We compare the distances.
563 old_dist
= rinfo
->distance
? rinfo
->distance
564 : ZEBRA_RIP_DISTANCE_DEFAULT
;
566 if (new_dist
> old_dist
) {
567 /* New route has a greater distance.
569 route_unlock_node(rp
);
573 if (new_dist
< old_dist
)
574 /* New route has a smaller distance.
575 * Replace the ECMP list
576 * with the new one in below. */
579 /* Metrics and distances are both same. Keep
581 * the new route is added in the ECMP list in
586 /* Local static route. */
587 if (rinfo
->type
== ZEBRA_ROUTE_RIP
588 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
589 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
590 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
591 route_unlock_node(rp
);
595 /* Redistributed route check. */
596 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
597 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
598 old_dist
= rinfo
->distance
;
599 /* Only routes directly connected to an interface
601 * may have a valid NULL distance */
602 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
605 : ZEBRA_RIP_DISTANCE_DEFAULT
;
606 /* If imported route does not have STRICT precedence,
607 mark it as a ghost */
608 if (new_dist
<= old_dist
609 && rte
->metric
!= RIP_METRIC_INFINITY
)
610 rip_ecmp_replace(rip
, &newinfo
);
612 route_unlock_node(rp
);
619 route_unlock_node(rp
);
621 /* Now, check to see whether there is already an explicit route
622 for the destination prefix. If there is no such route, add
623 this route to the routing table, unless the metric is
624 infinity (there is no point in adding a route which
626 if (rte
->metric
!= RIP_METRIC_INFINITY
)
627 rip_ecmp_add(rip
, &newinfo
);
629 /* Route is there but we are not sure the route is RIP or not.
632 /* If there is an existing route, compare the next hop address
633 to the address of the router from which the datagram came.
634 If this datagram is from the same router as the existing
635 route, reinitialize the timeout. */
636 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
637 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
639 old_dist
= rinfo
->distance
? rinfo
->distance
640 : ZEBRA_RIP_DISTANCE_DEFAULT
;
642 /* Next, compare the metrics. If the datagram is from the same
643 router as the existing route, and the new metric is different
644 than the old one; or, if the new metric is lower than the old
645 one, or if the tag has been changed; or if there is a route
646 with a lower administrave distance; or an update of the
647 distance on the actual route; do the following actions: */
648 if ((same
&& rinfo
->metric
!= rte
->metric
)
649 || (rte
->metric
< rinfo
->metric
)
650 || ((same
) && (rinfo
->metric
== rte
->metric
)
651 && (newinfo
.tag
!= rinfo
->tag
))
652 || (old_dist
> new_dist
)
653 || ((old_dist
!= new_dist
) && same
)) {
654 if (listcount(list
) == 1) {
655 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
656 rip_ecmp_replace(rip
, &newinfo
);
658 rip_ecmp_delete(rip
, rinfo
);
660 if (newinfo
.metric
< rinfo
->metric
)
661 rip_ecmp_replace(rip
, &newinfo
);
662 else if (newinfo
.metric
> rinfo
->metric
)
663 rip_ecmp_delete(rip
, rinfo
);
664 else if (new_dist
< old_dist
)
665 rip_ecmp_replace(rip
, &newinfo
);
666 else if (new_dist
> old_dist
)
667 rip_ecmp_delete(rip
, rinfo
);
669 int update
= CHECK_FLAG(rinfo
->flags
,
674 assert(newinfo
.metric
675 != RIP_METRIC_INFINITY
);
677 EVENT_OFF(rinfo
->t_timeout
);
678 EVENT_OFF(rinfo
->t_garbage_collect
);
679 memcpy(rinfo
, &newinfo
,
680 sizeof(struct rip_info
));
681 rip_timeout_update(rip
, rinfo
);
684 rip_zebra_ipv4_add(rip
, rp
);
686 /* - Set the route change flag on the
688 rinfo
= listgetdata(listhead(list
));
689 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
690 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
693 } else /* same & no change */
694 rip_timeout_update(rip
, rinfo
);
696 /* Unlock tempolary lock of the route. */
697 route_unlock_node(rp
);
701 /* Dump RIP packet */
702 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
707 const char *command_str
;
711 /* Set command string. */
712 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
713 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
715 command_str
= "unknown";
717 /* Dump packet header. */
718 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
719 packet
->version
, size
);
721 /* Dump each routing table entry. */
724 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
725 if (packet
->version
== RIPv2
) {
726 netmask
= ip_masklen(rte
->mask
);
728 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
730 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
731 p
= (uint8_t *)&rte
->prefix
;
734 " family 0x%X type %d auth string: %s",
737 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
738 struct rip_md5_info
*md5
;
740 md5
= (struct rip_md5_info
*)&packet
744 " family 0x%X type %d (MD5 authentication)",
748 " RIP-2 packet len %d Key ID %d Auth Data len %d",
749 ntohs(md5
->packet_len
),
750 md5
->keyid
, md5
->auth_len
);
751 zlog_debug(" Sequence Number %ld",
752 (unsigned long)ntohl(
754 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
755 p
= (uint8_t *)&rte
->prefix
;
758 " family 0x%X type %d (MD5 data)",
762 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
763 p
[0], p
[1], p
[2], p
[3], p
[4],
764 p
[5], p
[6], p
[7], p
[8], p
[9],
765 p
[10], p
[11], p
[12], p
[13],
769 " family 0x%X type %d (Unknown auth type)",
775 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
777 &rte
->prefix
, netmask
, &rte
->nexthop
,
779 (route_tag_t
)ntohs(rte
->tag
),
780 (unsigned long)ntohl(rte
->metric
));
782 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
784 &rte
->prefix
, ntohs(rte
->family
),
785 (route_tag_t
)ntohs(rte
->tag
),
786 (unsigned long)ntohl(rte
->metric
));
791 /* Check if the destination address is valid (unicast; not net 0
792 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
793 check net 0 because we accept default route. */
794 static int rip_destination_check(struct in_addr addr
)
796 uint32_t destination
;
798 /* Convert to host byte order. */
799 destination
= ntohl(addr
.s_addr
);
801 if (IPV4_NET127(destination
))
804 /* Net 0 may match to the default route. */
805 if (IPV4_NET0(destination
) && destination
!= 0)
808 /* Unicast address must belong to class A, B, C. */
809 if (IN_CLASSA(destination
))
811 if (IN_CLASSB(destination
))
813 if (IN_CLASSC(destination
))
819 /* RIP version 2 authentication. */
820 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
821 struct interface
*ifp
)
823 struct rip_interface
*ri
;
824 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
827 /* reject passwords with zeros in the middle of the string */
828 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
829 if (auth_str
[i
] != '\0')
833 if (IS_RIP_DEBUG_EVENT
)
834 zlog_debug("RIPv2 simple password authentication from %pI4",
839 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
840 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
843 /* Simple password authentication. */
845 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
849 struct keychain
*keychain
;
852 keychain
= keychain_lookup(ri
->key_chain
);
853 if (keychain
== NULL
|| keychain
->key
== NULL
)
856 key
= key_match_for_accept(keychain
, auth_str
);
863 /* RIP version 2 authentication with MD5. */
864 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
865 int length
, struct interface
*ifp
)
867 struct rip_interface
*ri
;
868 struct rip_md5_info
*md5
;
869 struct rip_md5_data
*md5data
;
870 struct keychain
*keychain
;
872 #ifdef CRYPTO_OPENSSL
874 #elif CRYPTO_INTERNAL
877 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
879 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
881 if (IS_RIP_DEBUG_EVENT
)
882 zlog_debug("RIPv2 MD5 authentication from %pI4",
886 md5
= (struct rip_md5_info
*)&packet
->rte
;
888 /* Check auth type. */
889 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
892 /* If the authentication length is less than 16, then it must be wrong
894 * any interpretation of rfc2082. Some implementations also interpret
895 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
896 * RIP_AUTH_MD5_COMPAT_SIZE.
898 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
899 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
900 if (IS_RIP_DEBUG_EVENT
)
902 "RIPv2 MD5 authentication, strange authentication length field %d",
907 /* grab and verify check packet length */
908 packet_len
= ntohs(md5
->packet_len
);
910 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
911 if (IS_RIP_DEBUG_EVENT
)
913 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
914 md5
->packet_len
, length
);
918 /* retrieve authentication data */
919 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
922 keychain
= keychain_lookup(ri
->key_chain
);
923 if (keychain
== NULL
)
926 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
927 if (key
== NULL
|| key
->string
== NULL
)
930 memcpy(auth_str
, key
->string
,
931 MIN(sizeof(auth_str
), strlen(key
->string
)));
932 } else if (ri
->auth_str
)
933 memcpy(auth_str
, ri
->auth_str
,
934 MIN(sizeof(auth_str
), strlen(ri
->auth_str
)));
936 if (auth_str
[0] == 0)
939 /* MD5 digest authentication. */
940 #ifdef CRYPTO_OPENSSL
941 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
942 ctx
= EVP_MD_CTX_new();
943 EVP_DigestInit(ctx
, EVP_md5());
944 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
945 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
946 EVP_DigestFinal(ctx
, digest
, &md5_size
);
947 EVP_MD_CTX_free(ctx
);
948 #elif CRYPTO_INTERNAL
949 memset(&ctx
, 0, sizeof(ctx
));
951 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
952 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
953 MD5Final(digest
, &ctx
);
956 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
962 /* Pick correct auth string for sends, prepare auth_str buffer for use.
963 * (left justified and padded).
965 * presumes one of ri or key is valid, and that the auth strings they point
966 * to are nul terminated. If neither are present, auth_str will be fully
970 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
971 char *auth_str
, int len
)
975 memset(auth_str
, 0, len
);
976 if (key
&& key
->string
)
977 memcpy(auth_str
, key
->string
,
978 MIN((size_t)len
, strlen(key
->string
)));
979 else if (ri
->auth_str
)
980 memcpy(auth_str
, ri
->auth_str
,
981 MIN((size_t)len
, strlen(ri
->auth_str
)));
986 /* Write RIPv2 simple password authentication information
988 * auth_str is presumed to be 2 bytes and correctly prepared
989 * (left justified and zero padded).
991 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
993 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
995 stream_putw(s
, RIP_FAMILY_AUTH
);
996 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
997 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
1002 /* write RIPv2 MD5 "authentication header"
1003 * (uses the auth key data field)
1005 * Digest offset field is set to 0.
1007 * returns: offset of the digest offset field, which must be set when
1008 * length to the auth-data MD5 digest is known.
1010 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1014 static uint32_t seq
= 0;
1016 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1018 /* MD5 authentication. */
1019 stream_putw(s
, RIP_FAMILY_AUTH
);
1020 stream_putw(s
, RIP_AUTH_MD5
);
1022 /* MD5 AH digest offset field.
1024 * Set to placeholder value here, to true value when RIP-2 Packet length
1025 * is known. Actual value is set in .....().
1027 doff
= stream_get_endp(s
);
1032 stream_putc(s
, key
->index
% 256);
1036 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1037 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1039 * to be configurable.
1041 stream_putc(s
, ri
->md5_auth_len
);
1043 /* Sequence Number (non-decreasing). */
1044 /* RFC2080: The value used in the sequence number is
1045 arbitrary, but two suggestions are the time of the
1046 message's creation or a simple message counter. */
1047 stream_putl(s
, ++seq
);
1049 /* Reserved field must be zero. */
1056 /* If authentication is in used, write the appropriate header
1057 * returns stream offset to which length must later be written
1058 * or 0 if this is not required
1060 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1061 struct key
*key
, char *auth_str
, int len
)
1063 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1065 switch (ri
->auth_type
) {
1066 case RIP_AUTH_SIMPLE_PASSWORD
:
1067 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1068 rip_auth_simple_write(s
, auth_str
, len
);
1071 return rip_auth_md5_ah_write(s
, ri
, key
);
1077 /* Write RIPv2 MD5 authentication data trailer */
1078 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1079 size_t doff
, char *auth_str
, int authlen
)
1082 #ifdef CRYPTO_OPENSSL
1084 #elif CRYPTO_INTERNAL
1087 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1089 /* Make it sure this interface is configured as MD5
1091 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1092 && (authlen
== RIP_AUTH_MD5_SIZE
));
1095 /* Get packet length. */
1096 len
= stream_get_endp(s
);
1098 /* Check packet length. */
1099 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1100 flog_err(EC_RIP_PACKET
,
1101 "%s: packet length %ld is less than minimum length.",
1106 /* Set the digest offset length in the header */
1107 stream_putw_at(s
, doff
, len
);
1109 /* Set authentication data. */
1110 stream_putw(s
, RIP_FAMILY_AUTH
);
1111 stream_putw(s
, RIP_AUTH_DATA
);
1113 /* Generate a digest for the RIP packet. */
1114 #ifdef CRYPTO_OPENSSL
1115 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1116 ctx
= EVP_MD_CTX_new();
1117 EVP_DigestInit(ctx
, EVP_md5());
1118 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1119 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1120 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1121 EVP_MD_CTX_free(ctx
);
1122 #elif CRYPTO_INTERNAL
1123 memset(&ctx
, 0, sizeof(ctx
));
1125 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1126 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1127 MD5Final(digest
, &ctx
);
1130 /* Copy the digest to the packet. */
1131 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1134 /* RIP routing information. */
1135 static void rip_response_process(struct rip_packet
*packet
, int size
,
1136 struct sockaddr_in
*from
,
1137 struct connected
*ifc
)
1139 struct rip_interface
*ri
= ifc
->ifp
->info
;
1140 struct rip
*rip
= ri
->rip
;
1143 struct prefix_ipv4 ifaddr
;
1144 struct prefix_ipv4 ifaddrclass
;
1147 memset(&ifaddr
, 0, sizeof(ifaddr
));
1148 /* We don't know yet. */
1151 /* The Response must be ignored if it is not from the RIP
1152 port. (RFC2453 - Sec. 3.9.2)*/
1153 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1154 zlog_info("response doesn't come from RIP port: %d",
1156 rip_peer_bad_packet(rip
, ri
, from
);
1160 /* The datagram's IPv4 source address should be checked to see
1161 whether the datagram is from a valid neighbor; the source of the
1162 datagram must be on a directly connected network (RFC2453 - Sec.
1164 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1168 "This datagram doesn't come from a valid neighbor: %pI4",
1170 rip_peer_bad_packet(rip
, ri
, from
);
1174 /* It is also worth checking to see whether the response is from one
1175 of the router's own addresses. */
1177 ; /* Alredy done in rip_read () */
1179 /* Update RIP peer. */
1180 rip_peer_update(rip
, ri
, from
, packet
->version
);
1182 /* Set RTE pointer. */
1185 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1186 /* RIPv2 authentication check. */
1187 /* If the Address Family Identifier of the first (and only the
1188 first) entry in the message is 0xFFFF, then the remainder of
1189 the entry contains the authentication. */
1190 /* If the packet gets here it means authentication enabled */
1191 /* Check is done in rip_read(). So, just skipping it */
1192 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1193 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1196 if (rte
->family
!= htons(AF_INET
)) {
1197 /* Address family check. RIP only supports AF_INET. */
1198 zlog_info("Unsupported family %d from %pI4",
1204 if (packet
->version
== RIPv1
&& rte
->tag
!= 0) {
1205 zlog_warn("RIPv1 reserved field is nonzero: %d",
1210 /* - is the destination address valid (e.g., unicast; not net 0
1212 if (!rip_destination_check(rte
->prefix
)) {
1214 "Network is net 0 or net 127 or it is not unicast network");
1215 rip_peer_bad_route(rip
, ri
, from
);
1219 /* Convert metric value to host byte order. */
1220 rte
->metric
= ntohl(rte
->metric
);
1222 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1223 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1224 zlog_info("Route's metric is not in the 1-16 range.");
1225 rip_peer_bad_route(rip
, ri
, from
);
1229 /* RIPv1 does not have nexthop value. */
1230 if (packet
->version
== RIPv1
1231 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1232 zlog_info("RIPv1 packet with nexthop value %pI4",
1234 rip_peer_bad_route(rip
, ri
, from
);
1238 /* That is, if the provided information is ignored, a possibly
1239 sub-optimal, but absolutely valid, route may be taken. If
1240 the received Next Hop is not directly reachable, it should be
1241 treated as 0.0.0.0. */
1242 if (packet
->version
== RIPv2
1243 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1246 /* Multicast address check. */
1247 addrval
= ntohl(rte
->nexthop
.s_addr
);
1248 if (IN_CLASSD(addrval
)) {
1250 "Nexthop %pI4 is multicast address, skip this rte",
1255 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1256 rip
->vrf
->vrf_id
)) {
1257 struct route_node
*rn
;
1258 struct rip_info
*rinfo
;
1260 rn
= route_node_match_ipv4(rip
->table
,
1266 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1269 if (IS_RIP_DEBUG_EVENT
)
1271 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1273 rte
->nexthop
= rinfo
->from
;
1275 if (IS_RIP_DEBUG_EVENT
)
1277 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1279 rte
->nexthop
.s_addr
=
1283 route_unlock_node(rn
);
1285 if (IS_RIP_DEBUG_EVENT
)
1287 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1289 rte
->nexthop
.s_addr
= INADDR_ANY
;
1294 /* For RIPv1, there won't be a valid netmask.
1295 * This is a best guess at the masks. If everyone was using old
1296 * Ciscos before the 'ip subnet zero' option, it would be almost
1299 * Cisco summarize ripv1 advertisements to the classful boundary
1300 * (/16 for class B's) except when the RIP packet does to inside
1301 * the classful network in question.
1303 if ((packet
->version
== RIPv1
1304 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1305 || (packet
->version
== RIPv2
1306 && (rte
->prefix
.s_addr
!= INADDR_ANY
1307 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1308 uint32_t destination
;
1310 if (subnetted
== -1) {
1311 memcpy(&ifaddr
, ifc
->address
, sizeof(ifaddr
));
1312 memcpy(&ifaddrclass
, &ifaddr
,
1313 sizeof(ifaddrclass
));
1314 apply_classful_mask_ipv4(&ifaddrclass
);
1316 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1320 destination
= ntohl(rte
->prefix
.s_addr
);
1322 if (IN_CLASSA(destination
))
1323 masklen2ip(8, &rte
->mask
);
1324 else if (IN_CLASSB(destination
))
1325 masklen2ip(16, &rte
->mask
);
1326 else if (IN_CLASSC(destination
))
1327 masklen2ip(24, &rte
->mask
);
1330 masklen2ip(ifaddrclass
.prefixlen
,
1331 (struct in_addr
*)&destination
);
1332 if ((subnetted
== 1)
1333 && ((rte
->prefix
.s_addr
& destination
)
1334 == ifaddrclass
.prefix
.s_addr
)) {
1335 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1336 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1337 != rte
->prefix
.s_addr
)
1338 masklen2ip(32, &rte
->mask
);
1339 if (IS_RIP_DEBUG_EVENT
)
1340 zlog_debug("Subnetted route %pI4",
1343 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1344 != rte
->prefix
.s_addr
)
1348 if (IS_RIP_DEBUG_EVENT
) {
1349 zlog_debug("Resultant route %pI4",
1351 zlog_debug("Resultant mask %pI4",
1356 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1357 ignore the entry. */
1358 if ((packet
->version
== RIPv2
)
1359 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1360 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1361 != rte
->prefix
.s_addr
)) {
1363 "RIPv2 address %pI4 is not mask /%d applied one",
1364 &rte
->prefix
, ip_masklen(rte
->mask
));
1365 rip_peer_bad_route(rip
, ri
, from
);
1369 /* Default route's netmask is ignored. */
1370 if (packet
->version
== RIPv2
1371 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1372 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1373 if (IS_RIP_DEBUG_EVENT
)
1375 "Default route with non-zero netmask. Set zero to netmask");
1376 rte
->mask
.s_addr
= INADDR_ANY
;
1379 /* Routing table updates. */
1380 rip_rte_process(rte
, from
, ifc
->ifp
);
1384 /* Make socket for RIP protocol. */
1385 int rip_create_socket(struct vrf
*vrf
)
1389 struct sockaddr_in addr
;
1390 const char *vrf_dev
= NULL
;
1392 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1393 addr
.sin_family
= AF_INET
;
1394 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1395 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1396 addr
.sin_len
= sizeof(struct sockaddr_in
);
1397 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1398 /* sending port must always be the RIP port */
1399 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1401 /* Make datagram socket. */
1402 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1403 vrf_dev
= vrf
->name
;
1404 frr_with_privs(&ripd_privs
) {
1405 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1408 flog_err_sys(EC_LIB_SOCKET
,
1409 "Cannot create UDP socket: %s",
1410 safe_strerror(errno
));
1415 sockopt_broadcast(sock
);
1416 sockopt_reuseaddr(sock
);
1417 sockopt_reuseport(sock
);
1418 setsockopt_ipv4_multicast_loop(sock
, 0);
1419 #ifdef IPTOS_PREC_INTERNETCONTROL
1420 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1422 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1424 frr_with_privs(&ripd_privs
) {
1425 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1427 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1428 __func__
, sock
, &addr
.sin_addr
,
1429 (int)ntohs(addr
.sin_port
),
1430 safe_strerror(errno
));
1440 /* RIP packet send to destination address, on interface denoted by
1441 * by connected argument. NULL to argument denotes destination should be
1442 * should be RIP multicast group
1444 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1445 struct connected
*ifc
)
1447 struct rip_interface
*ri
;
1450 struct sockaddr_in sin
;
1454 struct cmsghdr
*cmsgptr
;
1455 char adata
[256] = {};
1456 struct in_pktinfo
*pkt
;
1457 #endif /* GNU_LINUX */
1459 assert(ifc
!= NULL
);
1460 ri
= ifc
->ifp
->info
;
1463 if (IS_RIP_DEBUG_PACKET
) {
1464 #define ADDRESS_SIZE 20
1465 char dst
[ADDRESS_SIZE
];
1468 inet_ntop(AF_INET
, &to
->sin_addr
, dst
, sizeof(dst
));
1470 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1471 inet_ntop(AF_INET
, &sin
.sin_addr
, dst
, sizeof(dst
));
1474 zlog_debug("%s %pI4 > %s (%s)", __func__
,
1475 &ifc
->address
->u
.prefix4
, dst
, ifc
->ifp
->name
);
1478 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1480 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1481 * configured with multiple addresses on the same
1482 * subnet: the first address on the subnet is configured
1483 * "primary", and all subsequent addresses on that subnet
1484 * are treated as "secondary" addresses. In order to avoid
1485 * routing-table bloat on other rip listeners, we do not send
1486 * out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1487 * XXX Since Linux is the only system for which the
1488 * ZEBRA_IFA_SECONDARY flag is set, we would end up
1489 * sending a packet for a "secondary" source address on
1490 * non-linux systems.
1492 if (IS_RIP_DEBUG_PACKET
)
1493 zlog_debug("duplicate dropped");
1497 /* Make destination address. */
1498 memset(&sin
, 0, sizeof(sin
));
1499 sin
.sin_family
= AF_INET
;
1500 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1501 sin
.sin_len
= sizeof(struct sockaddr_in
);
1502 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1504 /* When destination is specified, use it's port and address. */
1506 sin
.sin_port
= to
->sin_port
;
1507 sin
.sin_addr
= to
->sin_addr
;
1509 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1510 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1512 rip_interface_multicast_set(rip
->sock
, ifc
);
1515 memset(&msg
, 0, sizeof(msg
));
1516 msg
.msg_name
= (void *)&sin
;
1517 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1524 msg
.msg_control
= (void *)adata
;
1525 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1527 cmsgptr
= (struct cmsghdr
*)adata
;
1528 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1529 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1530 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1531 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1532 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1533 pkt
->ipi_spec_dst
.s_addr
= ifc
->address
->u
.prefix4
.s_addr
;
1534 #endif /* GNU_LINUX */
1536 ret
= sendmsg(rip
->sock
, &msg
, 0);
1538 if (IS_RIP_DEBUG_EVENT
)
1539 zlog_debug("SEND to %pI4 port %d", &sin
.sin_addr
,
1540 ntohs(sin
.sin_port
));
1543 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1548 /* Add redistributed route to RIP table. */
1549 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1550 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1551 unsigned int metric
, unsigned char distance
,
1555 struct route_node
*rp
= NULL
;
1556 struct rip_info
*rinfo
= NULL
, newinfo
;
1557 struct list
*list
= NULL
;
1559 /* Redistribute route */
1560 ret
= rip_destination_check(p
->prefix
);
1564 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1566 memset(&newinfo
, 0, sizeof(newinfo
));
1567 newinfo
.type
= type
;
1568 newinfo
.sub_type
= sub_type
;
1570 newinfo
.external_metric
= metric
;
1571 newinfo
.distance
= distance
;
1572 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1577 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1578 rinfo
= listgetdata(listhead(list
));
1580 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1581 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1582 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1583 route_unlock_node(rp
);
1587 /* Manually configured RIP route check. */
1588 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1589 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1590 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1591 if (type
!= ZEBRA_ROUTE_RIP
1592 || ((sub_type
!= RIP_ROUTE_STATIC
)
1593 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1594 route_unlock_node(rp
);
1599 (void)rip_ecmp_replace(rip
, &newinfo
);
1600 route_unlock_node(rp
);
1602 (void)rip_ecmp_add(rip
, &newinfo
);
1604 if (IS_RIP_DEBUG_EVENT
) {
1605 zlog_debug("Redistribute new prefix %pFX", p
);
1608 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1611 /* Delete redistributed route from RIP table. */
1612 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1613 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1616 struct route_node
*rp
;
1617 struct rip_info
*rinfo
;
1619 ret
= rip_destination_check(p
->prefix
);
1623 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1625 struct list
*list
= rp
->info
;
1627 if (list
!= NULL
&& listcount(list
) != 0) {
1628 rinfo
= listgetdata(listhead(list
));
1629 if (rinfo
!= NULL
&& rinfo
->type
== type
1630 && rinfo
->sub_type
== sub_type
1631 && rinfo
->nh
.ifindex
== ifindex
) {
1632 /* Perform poisoned reverse. */
1633 rinfo
->metric
= RIP_METRIC_INFINITY
;
1634 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1635 rip_garbage_collect
,
1637 EVENT_OFF(rinfo
->t_timeout
);
1638 rinfo
->flags
|= RIP_RTF_CHANGED
;
1640 if (IS_RIP_DEBUG_EVENT
)
1642 "Poison %pFX on the interface %s with an infinity metric [delete]",
1648 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1651 route_unlock_node(rp
);
1655 /* Response to request called from rip_read ().*/
1656 static void rip_request_process(struct rip_packet
*packet
, int size
,
1657 struct sockaddr_in
*from
, struct connected
*ifc
)
1662 struct prefix_ipv4 p
;
1663 struct route_node
*rp
;
1664 struct rip_info
*rinfo
;
1665 struct rip_interface
*ri
;
1667 /* Does not reponse to the requests on the loopback interfaces */
1668 if (if_is_loopback(ifc
->ifp
))
1671 /* Check RIP process is enabled on this interface. */
1672 ri
= ifc
->ifp
->info
;
1677 /* When passive interface is specified, suppress responses */
1681 /* RIP peer update. */
1682 rip_peer_update(rip
, ri
, from
, packet
->version
);
1684 lim
= ((caddr_t
)packet
) + size
;
1687 /* The Request is processed entry by entry. If there are no
1688 entries, no response is given. */
1689 if (lim
== (caddr_t
)rte
)
1692 /* There is one special case. If there is exactly one entry in the
1693 request, and it has an address family identifier of zero and a
1694 metric of infinity (i.e., 16), then this is a request to send the
1695 entire routing table. */
1696 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1697 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1698 /* All route with split horizon */
1699 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1701 if (ntohs(rte
->family
) != AF_INET
)
1704 /* Examine the list of RTEs in the Request one by one. For each
1705 entry, look up the destination in the router's routing
1706 database and, if there is a route, put that route's metric in
1707 the metric field of the RTE. If there is no explicit route
1708 to the specified destination, put infinity in the metric
1709 field. Once all the entries have been filled in, change the
1710 command from Request to Response and send the datagram back
1711 to the requestor. */
1714 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1715 p
.prefix
= rte
->prefix
;
1716 p
.prefixlen
= ip_masklen(rte
->mask
);
1717 apply_mask_ipv4(&p
);
1719 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1721 rinfo
= listgetdata(
1722 listhead((struct list
*)rp
->info
));
1723 rte
->metric
= htonl(rinfo
->metric
);
1724 route_unlock_node(rp
);
1726 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1728 packet
->command
= RIP_RESPONSE
;
1730 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1732 rip
->counters
.queries
++;
1735 /* First entry point of RIP packet. */
1736 static void rip_read(struct event
*t
)
1738 struct rip
*rip
= EVENT_ARG(t
);
1742 union rip_buf rip_buf
;
1743 struct rip_packet
*packet
;
1744 struct sockaddr_in from
;
1748 struct interface
*ifp
= NULL
;
1749 struct connected
*ifc
;
1750 struct rip_interface
*ri
= NULL
;
1753 /* Fetch socket then register myself. */
1756 /* Add myself to tne next event */
1757 rip_event(rip
, RIP_READ
, sock
);
1759 /* RIPd manages only IPv4. */
1760 memset(&from
, 0, sizeof(from
));
1761 fromlen
= sizeof(struct sockaddr_in
);
1763 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1764 (struct sockaddr
*)&from
, &fromlen
);
1766 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1767 safe_strerror(errno
));
1771 /* Check is this packet comming from myself? */
1772 if (if_check_address(rip
, from
.sin_addr
)) {
1773 if (IS_RIP_DEBUG_PACKET
)
1774 zlog_debug("ignore packet comes from myself (VRF %s)",
1779 /* Which interface is this packet comes from. */
1780 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1787 /* RIP packet received */
1788 if (IS_RIP_DEBUG_EVENT
)
1789 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1790 &from
.sin_addr
, ntohs(from
.sin_port
),
1791 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1793 /* If this packet come from unknown interface, ignore it. */
1794 if (ifp
== NULL
|| ri
== NULL
) {
1796 "%s: cannot find interface for packet from %pI4 port %d (VRF %s)",
1797 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1803 p
.u
.prefix4
= from
.sin_addr
;
1804 p
.prefixlen
= IPV4_MAX_BITLEN
;
1806 ifc
= connected_lookup_prefix(ifp
, &p
);
1810 "%s: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1811 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1812 ifp
->name
, rip
->vrf_name
);
1816 /* Packet length check. */
1817 if (len
< RIP_PACKET_MINSIZ
) {
1818 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1820 rip_peer_bad_packet(rip
, ri
, &from
);
1823 if (len
> RIP_PACKET_MAXSIZ
) {
1824 zlog_warn("packet size %d is larger than max size %d", len
,
1826 rip_peer_bad_packet(rip
, ri
, &from
);
1830 /* Packet alignment check. */
1831 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1832 zlog_warn("packet size %d is wrong for RIP packet alignment",
1834 rip_peer_bad_packet(rip
, ri
, &from
);
1838 /* Set RTE number. */
1839 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1841 /* For easy to handle. */
1842 packet
= &rip_buf
.rip_packet
;
1844 /* RIP version check. */
1845 if (packet
->version
== 0) {
1846 zlog_info("version 0 with command %d received.",
1848 rip_peer_bad_packet(rip
, ri
, &from
);
1852 /* Dump RIP packet. */
1853 if (IS_RIP_DEBUG_RECV
)
1854 rip_packet_dump(packet
, len
, "RECV");
1856 /* RIP version adjust. This code should rethink now. RFC1058 says
1857 that "Version 1 implementations are to ignore this extra data and
1858 process only the fields specified in this document.". So RIPv3
1859 packet should be treated as RIPv1 ignoring must be zero field. */
1860 if (packet
->version
> RIPv2
)
1861 packet
->version
= RIPv2
;
1863 /* Is RIP running or is this RIP neighbor ?*/
1864 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1865 if (IS_RIP_DEBUG_EVENT
)
1866 zlog_debug("RIP is not enabled on interface %s.",
1868 rip_peer_bad_packet(rip
, ri
, &from
);
1872 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1873 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1875 if (vrecv
== RI_RIP_VERSION_NONE
1876 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1877 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1878 if (IS_RIP_DEBUG_PACKET
)
1880 " packet's v%d doesn't fit to if version spec",
1882 rip_peer_bad_packet(rip
, ri
, &from
);
1886 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1887 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1888 accepted; authenticated RIP-2 messages shall be discarded. */
1889 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1890 && (packet
->version
== RIPv2
)
1891 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1892 if (IS_RIP_DEBUG_EVENT
)
1894 "packet RIPv%d is dropped because authentication disabled",
1896 ripd_notif_send_auth_type_failure(ifp
->name
);
1897 rip_peer_bad_packet(rip
, ri
, &from
);
1902 If the router is configured to authenticate RIP-2 messages, then
1903 RIP-1 messages and RIP-2 messages which pass authentication
1904 testing shall be accepted; unauthenticated and failed
1905 authentication RIP-2 messages shall be discarded. For maximum
1906 security, RIP-1 messages should be ignored when authentication is
1907 in use (see section 4.1); otherwise, the routing information from
1908 authenticated messages will be propagated by RIP-1 routers in an
1909 unauthenticated manner.
1911 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1912 * always reply regardless of authentication settings, because:
1914 * - if there other authorised routers on-link, the REQUESTor can
1915 * passively obtain the routing updates anyway
1916 * - if there are no other authorised routers on-link, RIP can
1917 * easily be disabled for the link to prevent giving out information
1918 * on state of this routers RIP routing table..
1920 * I.e. if RIPv1 has any place anymore these days, it's as a very
1921 * simple way to distribute routing information (e.g. to embedded
1922 * hosts / appliances) and the ability to give out RIPv1
1923 * routing-information freely, while still requiring RIPv2
1924 * authentication for any RESPONSEs might be vaguely useful.
1926 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1927 /* Discard RIPv1 messages other than REQUESTs */
1928 if (packet
->command
!= RIP_REQUEST
) {
1929 if (IS_RIP_DEBUG_PACKET
)
1931 "RIPv1 dropped because authentication enabled");
1932 ripd_notif_send_auth_type_failure(ifp
->name
);
1933 rip_peer_bad_packet(rip
, ri
, &from
);
1936 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1937 const char *auth_desc
;
1940 /* There definitely is no authentication in the packet.
1942 if (IS_RIP_DEBUG_PACKET
)
1944 "RIPv2 authentication failed: no auth RTE in packet");
1945 ripd_notif_send_auth_type_failure(ifp
->name
);
1946 rip_peer_bad_packet(rip
, ri
, &from
);
1950 /* First RTE must be an Authentication Family RTE */
1951 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1952 if (IS_RIP_DEBUG_PACKET
)
1954 "RIPv2 dropped because authentication enabled");
1955 ripd_notif_send_auth_type_failure(ifp
->name
);
1956 rip_peer_bad_packet(rip
, ri
, &from
);
1960 /* Check RIPv2 authentication. */
1961 switch (ntohs(packet
->rte
->tag
)) {
1962 case RIP_AUTH_SIMPLE_PASSWORD
:
1963 auth_desc
= "simple";
1964 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1969 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1970 /* Reset RIP packet length to trim MD5 data. */
1976 auth_desc
= "unknown type";
1977 if (IS_RIP_DEBUG_PACKET
)
1979 "RIPv2 Unknown authentication type %d",
1980 ntohs(packet
->rte
->tag
));
1984 if (IS_RIP_DEBUG_PACKET
)
1985 zlog_debug("RIPv2 %s authentication success",
1988 if (IS_RIP_DEBUG_PACKET
)
1989 zlog_debug("RIPv2 %s authentication failure",
1991 ripd_notif_send_auth_failure(ifp
->name
);
1992 rip_peer_bad_packet(rip
, ri
, &from
);
1997 /* Process each command. */
1998 switch (packet
->command
) {
2000 rip_response_process(packet
, len
, &from
, ifc
);
2004 rip_request_process(packet
, len
, &from
, ifc
);
2009 "Obsolete command %s received, please sent it to routed",
2010 lookup_msg(rip_msg
, packet
->command
, NULL
));
2011 rip_peer_bad_packet(rip
, ri
, &from
);
2013 case RIP_POLL_ENTRY
:
2014 zlog_info("Obsolete command %s received",
2015 lookup_msg(rip_msg
, packet
->command
, NULL
));
2016 rip_peer_bad_packet(rip
, ri
, &from
);
2019 zlog_info("Unknown RIP command %d received", packet
->command
);
2020 rip_peer_bad_packet(rip
, ri
, &from
);
2025 /* Write routing table entry to the stream and return next index of
2026 the routing table entry in the stream. */
2027 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2028 uint8_t version
, struct rip_info
*rinfo
)
2030 struct in_addr mask
;
2032 /* Write routing table entry. */
2033 if (version
== RIPv1
) {
2034 stream_putw(s
, AF_INET
);
2036 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2037 stream_put_ipv4(s
, 0);
2038 stream_put_ipv4(s
, 0);
2039 stream_putl(s
, rinfo
->metric_out
);
2041 masklen2ip(p
->prefixlen
, &mask
);
2043 stream_putw(s
, AF_INET
);
2044 stream_putw(s
, rinfo
->tag_out
);
2045 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2046 stream_put_ipv4(s
, mask
.s_addr
);
2047 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2048 stream_putl(s
, rinfo
->metric_out
);
2054 /* Send update to the ifp or spcified neighbor. */
2055 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2056 int route_type
, uint8_t version
)
2061 struct route_node
*rp
;
2062 struct rip_info
*rinfo
;
2063 struct rip_interface
*ri
;
2064 struct prefix_ipv4
*p
;
2065 struct prefix_ipv4 classfull
;
2066 struct prefix_ipv4 ifaddrclass
;
2067 struct key
*key
= NULL
;
2068 /* this might need to made dynamic if RIP ever supported auth methods
2069 with larger key string sizes */
2070 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2071 size_t doff
= 0; /* offset of digest offset field */
2075 struct list
*list
= NULL
;
2076 struct listnode
*listnode
= NULL
;
2078 /* Logging output event. */
2079 if (IS_RIP_DEBUG_EVENT
) {
2081 zlog_debug("update routes to neighbor %pI4",
2084 zlog_debug("update routes on interface %s ifindex %d",
2085 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2088 /* Get RIP interface. */
2089 ri
= ifc
->ifp
->info
;
2092 /* Set output stream. */
2095 /* Reset stream and RTE counter. */
2097 rtemax
= RIP_MAX_RTE
;
2099 /* If output interface is in simple password authentication mode, we
2100 need space for authentication data. */
2101 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2104 /* If output interface is in MD5 authentication mode, we need space
2105 for authentication header and data. */
2106 if (ri
->auth_type
== RIP_AUTH_MD5
)
2109 /* If output interface is in simple password authentication mode
2110 and string or keychain is specified we need space for auth. data */
2111 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2112 if (ri
->key_chain
) {
2113 struct keychain
*keychain
;
2115 keychain
= keychain_lookup(ri
->key_chain
);
2117 key
= key_lookup_for_send(keychain
);
2119 /* to be passed to auth functions later */
2120 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2121 if (strlen(auth_str
) == 0)
2125 if (version
== RIPv1
) {
2126 memcpy(&ifaddrclass
, ifc
->address
, sizeof(ifaddrclass
));
2127 apply_classful_mask_ipv4(&ifaddrclass
);
2129 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2133 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2139 if (listcount(list
) == 0)
2142 rinfo
= listgetdata(listhead(list
));
2144 * For RIPv1, if we are subnetted, output subnets in our
2145 * network that have the same mask as the output "interface".
2146 * For other networks, only the classfull version is output.
2148 if (version
== RIPv1
) {
2149 p
= (struct prefix_ipv4
*)&rp
->p
;
2151 if (IS_RIP_DEBUG_PACKET
)
2153 "RIPv1 mask check, %pFX considered for output",
2157 prefix_match((struct prefix
*)&ifaddrclass
,
2159 if ((ifc
->address
->prefixlen
!=
2161 (rp
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2164 memcpy(&classfull
, &rp
->p
,
2165 sizeof(struct prefix_ipv4
));
2166 apply_classful_mask_ipv4(&classfull
);
2167 if (rp
->p
.u
.prefix4
.s_addr
!= INADDR_ANY
&&
2168 classfull
.prefixlen
!= rp
->p
.prefixlen
)
2171 if (IS_RIP_DEBUG_PACKET
)
2173 "RIPv1 mask check, %pFX made it through",
2176 p
= (struct prefix_ipv4
*)&rp
->p
;
2178 /* Apply output filters. */
2179 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2183 /* Changed route only output. */
2184 if (route_type
== rip_changed_route
&&
2185 (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2188 /* Split horizon. */
2189 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2191 * We perform split horizon for RIP and connected
2192 * route. For rip routes, we want to suppress the
2193 * route if we would end up sending the route back on
2194 * the interface that we learned it from, with a
2195 * higher metric. For connected routes, we suppress
2196 * the route if the prefix is a subset of the source
2197 * address that we are going to use for the packet
2198 * (in order to handle the case when multiple subnets
2199 * are configured on the same interface).
2202 struct rip_info
*tmp_rinfo
= NULL
;
2203 struct connected
*tmp_ifc
= NULL
;
2205 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2206 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2207 tmp_rinfo
->nh
.ifindex
==
2208 ifc
->ifp
->ifindex
) {
2213 if (!suppress
&& rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2214 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2216 if (prefix_match((struct prefix
*)p
,
2217 tmp_ifc
->address
)) {
2227 /* Preparation for route-map. */
2228 rinfo
->metric_set
= 0;
2229 rinfo
->nexthop_out
.s_addr
= 0;
2230 rinfo
->metric_out
= rinfo
->metric
;
2231 rinfo
->tag_out
= rinfo
->tag
;
2232 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2234 /* In order to avoid some local loops, if the RIP route has
2235 * a nexthop via this interface, keep the nexthop, otherwise
2236 * set it to 0. The nexthop should not be propagated beyond
2237 * the local broadcast/multicast area in order to avoid an
2238 * IGP multi-level recursive look-up. see (4.4)
2240 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2241 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2243 /* Interface route-map */
2244 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2245 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_OUT
],
2246 (struct prefix
*)p
, rinfo
);
2248 if (ret
== RMAP_DENYMATCH
) {
2249 if (IS_RIP_DEBUG_PACKET
)
2251 "RIP %pFX is filtered by route-map out",
2257 /* Apply redistribute route map - continue, if deny */
2258 if (rip
->redist
[rinfo
->type
].route_map
.name
&&
2259 rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2260 ret
= route_map_apply(
2261 rip
->redist
[rinfo
->type
].route_map
.map
,
2262 (struct prefix
*)p
, rinfo
);
2264 if (ret
== RMAP_DENYMATCH
) {
2265 if (IS_RIP_DEBUG_PACKET
)
2267 "%pFX is filtered by route-map",
2273 /* When route-map does not set metric. */
2274 if (!rinfo
->metric_set
) {
2275 /* If redistribute metric is set. */
2276 if (rip
->redist
[rinfo
->type
].metric_config
&&
2277 rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2279 rip
->redist
[rinfo
->type
].metric
;
2281 /* If the route is not connected or localy
2282 * generated one, use default-metric value
2284 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&&
2285 rinfo
->type
!= ZEBRA_ROUTE_CONNECT
&&
2286 rinfo
->metric
!= RIP_METRIC_INFINITY
)
2287 rinfo
->metric_out
= rip
->default_metric
;
2291 /* Apply offset-list */
2292 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2293 rip_offset_list_apply_out(p
, ifc
->ifp
,
2294 &rinfo
->metric_out
);
2296 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2297 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2299 /* Perform split-horizon with poisoned reverse
2300 * for RIP and connected routes.
2302 if (ri
->split_horizon
== RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2304 * We perform split horizon for RIP and connected
2305 * route. For rip routes, we want to suppress the
2306 * route if we would end up sending the route back
2307 * on the interface that we learned it from, with a
2308 * higher metric. For connected routes, we suppress
2309 * the route if the prefix is a subset of the source
2310 * address that we are going to use for the packet
2311 * (in order to handle the case when multiple
2312 * subnets are configured on the same interface).
2314 struct rip_info
*tmp_rinfo
= NULL
;
2315 struct connected
*tmp_ifc
= NULL
;
2317 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2318 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2319 tmp_rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2320 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2322 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
&&
2323 rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2324 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2326 if (prefix_match((struct prefix
*)p
,
2327 tmp_ifc
->address
)) {
2329 RIP_METRIC_INFINITY
;
2335 /* Prepare preamble, auth headers, if needs be */
2337 stream_putc(s
, RIP_RESPONSE
);
2338 stream_putc(s
, version
);
2341 /* auth header for !v1 && !no_auth */
2342 if ((ri
->auth_type
!= RIP_NO_AUTH
) &&
2344 doff
= rip_auth_header_write(
2345 s
, ri
, key
, auth_str
,
2346 RIP_AUTH_SIMPLE_SIZE
);
2349 /* Write RTE to the stream. */
2350 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2351 if (num
== rtemax
) {
2352 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2353 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2354 RIP_AUTH_SIMPLE_SIZE
);
2356 ret
= rip_send_packet(STREAM_DATA(s
),
2357 stream_get_endp(s
), to
, ifc
);
2359 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2361 (struct rip_packet
*)STREAM_DATA(s
),
2362 stream_get_endp(s
), "SEND");
2368 /* Flush unwritten RTE. */
2370 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2371 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2372 RIP_AUTH_SIMPLE_SIZE
);
2374 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2377 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2378 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2379 stream_get_endp(s
), "SEND");
2383 /* Statistics updates. */
2387 /* Send RIP packet to the interface. */
2388 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2391 struct interface
*ifp
= ifc
->ifp
;
2392 struct rip_interface
*ri
= ifp
->info
;
2393 struct sockaddr_in to
;
2395 /* When RIP version is 2 and multicast enable interface. */
2396 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2397 if (IS_RIP_DEBUG_EVENT
)
2398 zlog_debug("multicast announce on %s ", ifp
->name
);
2400 rip_output_process(ifc
, NULL
, route_type
, version
);
2404 /* If we can't send multicast packet, send it with unicast. */
2405 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2406 if (ifc
->address
->family
== AF_INET
) {
2407 /* Destination address and port setting. */
2408 memset(&to
, 0, sizeof(to
));
2409 if (ifc
->destination
)
2410 /* use specified broadcast or peer destination
2412 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2413 else if (ifc
->address
->prefixlen
< IPV4_MAX_BITLEN
)
2414 /* calculate the appropriate broadcast address
2416 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2417 ifc
->address
->u
.prefix4
.s_addr
,
2418 ifc
->address
->prefixlen
);
2420 /* do not know where to send the packet */
2422 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2424 if (IS_RIP_DEBUG_EVENT
)
2425 zlog_debug("%s announce to %pI4 on %s",
2426 CONNECTED_PEER(ifc
) ? "unicast"
2428 &to
.sin_addr
, ifp
->name
);
2430 rip_output_process(ifc
, &to
, route_type
, version
);
2435 /* Update send to all interface and neighbor. */
2436 static void rip_update_process(struct rip
*rip
, int route_type
)
2438 struct listnode
*ifnode
, *ifnnode
;
2439 struct connected
*connected
;
2440 struct interface
*ifp
;
2441 struct rip_interface
*ri
;
2442 struct route_node
*rp
;
2443 struct sockaddr_in to
;
2446 /* Send RIP update to each interface. */
2447 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2448 if (if_is_loopback(ifp
))
2451 if (!if_is_operative(ifp
))
2454 /* Fetch RIP interface information. */
2457 /* When passive interface is specified, suppress announce to the
2466 * If there is no version configuration in the
2467 * interface, use rip's version setting.
2469 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? rip
->version_send
2472 if (IS_RIP_DEBUG_EVENT
)
2473 zlog_debug("SEND UPDATE to %s ifindex %d", ifp
->name
,
2476 /* send update on each connected network */
2477 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2479 if (connected
->address
->family
== AF_INET
) {
2481 rip_update_interface(connected
, RIPv1
,
2483 if ((vsend
& RIPv2
) && if_is_multicast(ifp
))
2484 rip_update_interface(connected
, RIPv2
,
2490 /* RIP send updates to each neighbor. */
2491 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
)) {
2492 if (rp
->info
== NULL
)
2497 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2501 "Neighbor %pI4 doesn't have connected interface!",
2506 /* Set destination address and port */
2507 memset(&to
, 0, sizeof(struct sockaddr_in
));
2508 to
.sin_addr
= p
->u
.prefix4
;
2509 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2511 /* RIP version is rip's configuration. */
2512 rip_output_process(connected
, &to
, route_type
,
2517 /* RIP's periodical timer. */
2518 static void rip_update(struct event
*t
)
2520 struct rip
*rip
= EVENT_ARG(t
);
2522 if (IS_RIP_DEBUG_EVENT
)
2523 zlog_debug("update timer fire!");
2525 /* Process update output. */
2526 rip_update_process(rip
, rip_all_route
);
2528 /* Triggered updates may be suppressed if a regular update is due by
2529 the time the triggered update would be sent. */
2530 EVENT_OFF(rip
->t_triggered_interval
);
2533 /* Register myself. */
2534 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2537 /* Walk down the RIP routing table then clear changed flag. */
2538 static void rip_clear_changed_flag(struct rip
*rip
)
2540 struct route_node
*rp
;
2541 struct rip_info
*rinfo
= NULL
;
2542 struct list
*list
= NULL
;
2543 struct listnode
*listnode
= NULL
;
2545 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2551 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2552 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2553 /* This flag can be set only on the first entry. */
2559 /* Triggered update interval timer. */
2560 static void rip_triggered_interval(struct event
*t
)
2562 struct rip
*rip
= EVENT_ARG(t
);
2566 rip_triggered_update(t
);
2570 /* Execute triggered update. */
2571 static void rip_triggered_update(struct event
*t
)
2573 struct rip
*rip
= EVENT_ARG(t
);
2576 /* Cancel interval timer. */
2577 EVENT_OFF(rip
->t_triggered_interval
);
2580 /* Logging triggered update. */
2581 if (IS_RIP_DEBUG_EVENT
)
2582 zlog_debug("triggered update!");
2584 /* Split Horizon processing is done when generating triggered
2585 updates as well as normal updates (see section 2.6). */
2586 rip_update_process(rip
, rip_changed_route
);
2588 /* Once all of the triggered updates have been generated, the route
2589 change flags should be cleared. */
2590 rip_clear_changed_flag(rip
);
2592 /* After a triggered update is sent, a timer should be set for a
2593 random interval between 1 and 5 seconds. If other changes that
2594 would trigger updates occur before the timer expires, a single
2595 update is triggered when the timer expires. */
2596 interval
= (frr_weak_random() % 5) + 1;
2598 event_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2599 &rip
->t_triggered_interval
);
2602 /* Withdraw redistributed route. */
2603 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2605 struct route_node
*rp
;
2606 struct rip_info
*rinfo
= NULL
;
2607 struct list
*list
= NULL
;
2609 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2615 rinfo
= listgetdata(listhead(list
));
2617 if (rinfo
->type
!= type
)
2620 if (rinfo
->sub_type
== RIP_ROUTE_INTERFACE
)
2623 /* Perform poisoned reverse. */
2624 rinfo
->metric
= RIP_METRIC_INFINITY
;
2625 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
2627 EVENT_OFF(rinfo
->t_timeout
);
2628 rinfo
->flags
|= RIP_RTF_CHANGED
;
2630 if (IS_RIP_DEBUG_EVENT
) {
2631 struct prefix_ipv4
*p
= (struct prefix_ipv4
*)&rp
->p
;
2634 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2636 ifindex2ifname(rinfo
->nh
.ifindex
,
2640 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2644 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2648 vrf
= vrf_lookup_by_id(vrf_id
);
2655 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2659 rip
.vrf_name
= (char *)vrf_name
;
2661 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2664 /* Update ECMP routes to zebra when `allow-ecmp` changed. */
2665 void rip_ecmp_change(struct rip
*rip
)
2667 struct route_node
*rp
;
2668 struct rip_info
*rinfo
;
2670 struct listnode
*node
, *nextnode
;
2672 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2674 if (list
&& listcount(list
) > 1) {
2675 while (listcount(list
) > rip
->ecmp
) {
2676 struct rip_info
*from_highest
= NULL
;
2678 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
,
2680 if (!from_highest
||
2684 &from_highest
->from
) > 0))
2685 from_highest
= rinfo
;
2688 rip_ecmp_delete(rip
, from_highest
);
2694 /* Create new RIP instance and set it to global variable. */
2695 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2699 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2700 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2702 /* Set initial value. */
2703 rip
->ecmp
= yang_get_default_uint8("%s/allow-ecmp", RIP_INSTANCE
);
2704 rip
->default_metric
=
2705 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2707 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2708 rip
->passive_default
=
2709 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2710 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2712 rip
->timeout_time
= yang_get_default_uint32(
2713 "%s/timers/holddown-interval", RIP_INSTANCE
);
2714 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2717 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2719 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2721 /* Initialize RIP data structures. */
2722 rip
->table
= route_table_init();
2723 route_table_set_info(rip
->table
, rip
);
2724 rip
->neighbor
= route_table_init();
2725 rip
->peer_list
= list_new();
2726 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2727 rip
->peer_list
->del
= rip_peer_list_del
;
2728 rip
->distance_table
= route_table_init();
2729 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2730 rip
->enable_interface
= vector_init(1);
2731 rip
->enable_network
= route_table_init();
2732 rip
->passive_nondefault
= vector_init(1);
2733 rip
->offset_list_master
= list_new();
2734 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2735 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2737 /* Distribute list install. */
2738 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2739 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2740 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2742 /* if rmap install. */
2743 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2744 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2745 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2747 /* Make output stream. */
2748 rip
->obuf
= stream_new(1500);
2750 /* Enable the routing instance if possible. */
2751 if (vrf
&& vrf_is_enabled(vrf
))
2752 rip_instance_enable(rip
, vrf
, socket
);
2758 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2763 /* Sned RIP request to the destination. */
2764 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2765 uint8_t version
, struct connected
*connected
)
2768 struct rip_packet rip_packet
;
2769 struct listnode
*node
, *nnode
;
2771 memset(&rip_packet
, 0, sizeof(rip_packet
));
2773 rip_packet
.command
= RIP_REQUEST
;
2774 rip_packet
.version
= version
;
2775 rte
= rip_packet
.rte
;
2776 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2780 * connected is only sent for ripv1 case, or when
2781 * interface does not support multicast. Caller loops
2782 * over each connected address for this case.
2784 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2786 != sizeof(rip_packet
))
2789 return sizeof(rip_packet
);
2792 /* send request on each connected network */
2793 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2794 struct prefix_ipv4
*p
;
2796 p
= (struct prefix_ipv4
*)connected
->address
;
2798 if (p
->family
!= AF_INET
)
2801 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2803 != sizeof(rip_packet
))
2806 return sizeof(rip_packet
);
2809 static int rip_update_jitter(unsigned long time
)
2811 #define JITTER_BOUND 4
2812 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2813 Given that, we cannot let time be less than JITTER_BOUND seconds.
2814 The RIPv2 RFC says jitter should be small compared to
2815 update_time. We consider 1/JITTER_BOUND to be small.
2818 int jitter_input
= time
;
2821 if (jitter_input
< JITTER_BOUND
)
2822 jitter_input
= JITTER_BOUND
;
2824 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2827 return jitter
/ JITTER_BOUND
;
2830 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2836 event_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2838 case RIP_UPDATE_EVENT
:
2839 EVENT_OFF(rip
->t_update
);
2840 jitter
= rip_update_jitter(rip
->update_time
);
2841 event_add_timer(master
, rip_update
, rip
,
2842 sock
? 2 : rip
->update_time
+ jitter
,
2845 case RIP_TRIGGERED_UPDATE
:
2846 if (rip
->t_triggered_interval
)
2849 event_add_event(master
, rip_triggered_update
, rip
, 0,
2850 &rip
->t_triggered_update
);
2857 struct rip_distance
*rip_distance_new(void)
2859 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2862 void rip_distance_free(struct rip_distance
*rdistance
)
2864 if (rdistance
->access_list
)
2865 free(rdistance
->access_list
);
2866 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2869 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2870 struct route_node
*node
)
2872 struct rip_distance
*rdistance
;
2874 rdistance
= node
->info
;
2876 rip_distance_free(rdistance
);
2879 /* Apply RIP information to distance method. */
2880 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2882 struct route_node
*rn
;
2883 struct prefix_ipv4 p
;
2884 struct rip_distance
*rdistance
;
2885 struct access_list
*alist
;
2887 memset(&p
, 0, sizeof(p
));
2889 p
.prefix
= rinfo
->from
;
2890 p
.prefixlen
= IPV4_MAX_BITLEN
;
2892 /* Check source address. */
2893 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2895 rdistance
= rn
->info
;
2896 route_unlock_node(rn
);
2898 if (rdistance
->access_list
) {
2899 alist
= access_list_lookup(AFI_IP
,
2900 rdistance
->access_list
);
2903 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2907 return rdistance
->distance
;
2910 return rip
->distance
;
2913 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2915 struct route_node
*rn
;
2916 struct rip_distance
*rdistance
;
2920 vty_out(vty
, " Distance: (default is %u)\n",
2921 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2923 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
)) {
2924 rdistance
= rn
->info
;
2926 if (rdistance
== NULL
)
2930 vty_out(vty
, " Address Distance List\n");
2933 snprintfrr(buf
, sizeof(buf
), "%pFX", &rn
->p
);
2934 vty_out(vty
, " %-20s %4d %s\n", buf
, rdistance
->distance
,
2935 rdistance
->access_list
? rdistance
->access_list
: "");
2939 /* Update ECMP routes to zebra when ECMP is disabled. */
2940 void rip_ecmp_disable(struct rip
*rip
)
2942 struct route_node
*rp
;
2943 struct rip_info
*rinfo
, *tmp_rinfo
;
2945 struct listnode
*node
, *nextnode
;
2947 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2952 if (listcount(list
) == 0)
2955 rinfo
= listgetdata(listhead(list
));
2956 if (!rip_route_rte(rinfo
))
2959 /* Drop all other entries, except the first one. */
2960 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
2961 if (tmp_rinfo
== rinfo
)
2964 EVENT_OFF(tmp_rinfo
->t_timeout
);
2965 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
2966 list_delete_node(list
, node
);
2967 rip_info_free(tmp_rinfo
);
2971 rip_zebra_ipv4_add(rip
, rp
);
2973 /* Set the route change flag. */
2974 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2976 /* Signal the output process to trigger an update. */
2977 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2981 /* Print out routes update time. */
2982 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2987 char timebuf
[TIME_BUF
];
2988 struct event
*thread
;
2990 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2991 clock
= event_timer_remain_second(thread
);
2992 gmtime_r(&clock
, &tm
);
2993 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2994 vty_out(vty
, "%5s", timebuf
);
2995 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2996 clock
= event_timer_remain_second(thread
);
2997 gmtime_r(&clock
, &tm
);
2998 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2999 vty_out(vty
, "%5s", timebuf
);
3003 static const char *rip_route_type_print(int sub_type
)
3008 case RIP_ROUTE_STATIC
:
3010 case RIP_ROUTE_DEFAULT
:
3012 case RIP_ROUTE_REDISTRIBUTE
:
3014 case RIP_ROUTE_INTERFACE
:
3023 "show ip rip [vrf NAME]",
3030 struct route_node
*np
;
3031 struct rip_info
*rinfo
= NULL
;
3032 struct list
*list
= NULL
;
3033 struct listnode
*listnode
= NULL
;
3034 const char *vrf_name
;
3037 if (argv_find(argv
, argc
, "vrf", &idx
))
3038 vrf_name
= argv
[idx
+ 1]->arg
;
3040 vrf_name
= VRF_DEFAULT_NAME
;
3042 rip
= rip_lookup_by_vrf_name(vrf_name
);
3044 vty_out(vty
, "%% RIP instance not found\n");
3047 if (!rip
->enabled
) {
3048 vty_out(vty
, "%% RIP instance is disabled\n");
3053 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3055 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3056 " (i) - interface\n\n"
3057 " Network Next Hop Metric From Tag Time\n");
3059 for (np
= route_top(rip
->table
); np
; np
= route_next(np
)) {
3065 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3068 len
= vty_out(vty
, "%c(%s) %pFX",
3069 /* np->lock, For debugging. */
3070 zebra_route_char(rinfo
->type
),
3071 rip_route_type_print(rinfo
->sub_type
),
3077 vty_out(vty
, "%*s", len
, " ");
3079 switch (rinfo
->nh
.type
) {
3080 case NEXTHOP_TYPE_IPV4
:
3081 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3082 vty_out(vty
, "%-20pI4 %2d ",
3083 &rinfo
->nh
.gate
.ipv4
, rinfo
->metric
);
3085 case NEXTHOP_TYPE_IFINDEX
:
3086 vty_out(vty
, "0.0.0.0 %2d ",
3089 case NEXTHOP_TYPE_BLACKHOLE
:
3090 vty_out(vty
, "blackhole %2d ",
3093 case NEXTHOP_TYPE_IPV6
:
3094 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3095 vty_out(vty
, "V6 Address Hidden %2d ",
3100 /* Route which exist in kernel routing table. */
3101 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
) &&
3102 (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3103 vty_out(vty
, "%-15pI4 ", &rinfo
->from
);
3104 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3105 (route_tag_t
)rinfo
->tag
);
3106 rip_vty_out_uptime(vty
, rinfo
);
3107 } else if (rinfo
->metric
== RIP_METRIC_INFINITY
) {
3108 vty_out(vty
, "self ");
3109 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3110 (route_tag_t
)rinfo
->tag
);
3111 rip_vty_out_uptime(vty
, rinfo
);
3113 if (rinfo
->external_metric
) {
3115 vty
, "self (%s:%d)",
3116 zebra_route_string(rinfo
->type
),
3117 rinfo
->external_metric
);
3120 vty_out(vty
, "%*s", len
, " ");
3122 vty_out(vty
, "self ");
3123 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3124 (route_tag_t
)rinfo
->tag
);
3133 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3134 DEFUN (show_ip_rip_status
,
3135 show_ip_rip_status_cmd
,
3136 "show ip rip [vrf NAME] status",
3141 "IP routing protocol process parameters and statistics\n")
3144 struct interface
*ifp
;
3145 struct rip_interface
*ri
;
3146 extern const struct message ri_version_msg
[];
3147 const char *send_version
;
3148 const char *receive_version
;
3149 const char *vrf_name
;
3152 if (argv_find(argv
, argc
, "vrf", &idx
))
3153 vrf_name
= argv
[idx
+ 1]->arg
;
3155 vrf_name
= VRF_DEFAULT_NAME
;
3157 rip
= rip_lookup_by_vrf_name(vrf_name
);
3159 vty_out(vty
, "%% RIP instance not found\n");
3162 if (!rip
->enabled
) {
3163 vty_out(vty
, "%% RIP instance is disabled\n");
3167 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3168 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3170 vty_out(vty
, " next due in %lu seconds\n",
3171 event_timer_remain_second(rip
->t_update
));
3172 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3173 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3175 /* Filtering status show. */
3176 config_show_distribute(vty
, rip
->distribute_ctx
);
3178 /* Default metric information. */
3179 vty_out(vty
, " Default redistribution metric is %u\n",
3180 rip
->default_metric
);
3182 /* Redistribute information. */
3183 vty_out(vty
, " Redistributing:");
3184 rip_show_redistribute_config(vty
, rip
);
3187 vty_out(vty
, " Default version control: send version %s,",
3188 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3189 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3190 vty_out(vty
, " receive any version \n");
3192 vty_out(vty
, " receive version %s \n",
3193 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3195 vty_out(vty
, " Interface Send Recv Key-chain\n");
3197 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3203 if (ri
->enable_network
|| ri
->enable_interface
) {
3204 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3206 lookup_msg(ri_version_msg
,
3207 rip
->version_send
, NULL
);
3209 send_version
= lookup_msg(ri_version_msg
,
3212 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3214 lookup_msg(ri_version_msg
,
3215 rip
->version_recv
, NULL
);
3217 receive_version
= lookup_msg(
3218 ri_version_msg
, ri
->ri_receive
, NULL
);
3220 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3221 send_version
, receive_version
,
3222 ri
->key_chain
? ri
->key_chain
: "");
3226 vty_out(vty
, " Routing for Networks:\n");
3227 rip_show_network_config(vty
, rip
);
3229 int found_passive
= 0;
3230 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3233 if ((ri
->enable_network
|| ri
->enable_interface
) &&
3235 if (!found_passive
) {
3236 vty_out(vty
, " Passive Interface(s):\n");
3239 vty_out(vty
, " %s\n", ifp
->name
);
3243 vty_out(vty
, " Routing Information Sources:\n");
3245 " Gateway BadPackets BadRoutes Distance Last Update\n");
3246 rip_peer_display(vty
, rip
);
3248 rip_distance_show(vty
, rip
);
3253 /* RIP configuration write function. */
3254 static int config_write_rip(struct vty
*vty
)
3259 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3260 char xpath
[XPATH_MAXLEN
];
3261 struct lyd_node
*dnode
;
3263 snprintf(xpath
, sizeof(xpath
),
3264 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3266 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3269 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3271 /* Distribute configuration. */
3272 config_write_distribute(vty
, rip
->distribute_ctx
);
3274 vty_out(vty
, "exit\n");
3282 static int config_write_rip(struct vty
*vty
);
3283 /* RIP node structure. */
3284 static struct cmd_node rip_node
= {
3287 .parent_node
= CONFIG_NODE
,
3288 .prompt
= "%s(config-router)# ",
3289 .config_write
= config_write_rip
,
3292 /* Distribute-list update functions. */
3293 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3294 struct distribute
*dist
)
3296 struct interface
*ifp
;
3297 struct rip_interface
*ri
;
3298 struct access_list
*alist
;
3299 struct prefix_list
*plist
;
3301 if (!ctx
->vrf
|| !dist
->ifname
)
3304 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3310 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3311 alist
= access_list_lookup(AFI_IP
,
3312 dist
->list
[DISTRIBUTE_V4_IN
]);
3314 ri
->list
[RIP_FILTER_IN
] = alist
;
3316 ri
->list
[RIP_FILTER_IN
] = NULL
;
3318 ri
->list
[RIP_FILTER_IN
] = NULL
;
3320 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3321 alist
= access_list_lookup(AFI_IP
,
3322 dist
->list
[DISTRIBUTE_V4_OUT
]);
3324 ri
->list
[RIP_FILTER_OUT
] = alist
;
3326 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3328 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3330 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3331 plist
= prefix_list_lookup(AFI_IP
,
3332 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3334 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3336 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3338 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3340 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3341 plist
= prefix_list_lookup(AFI_IP
,
3342 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3344 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3346 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3348 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3351 void rip_distribute_update_interface(struct interface
*ifp
)
3353 struct rip_interface
*ri
= ifp
->info
;
3354 struct rip
*rip
= ri
->rip
;
3355 struct distribute
*dist
;
3359 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3361 rip_distribute_update(rip
->distribute_ctx
, dist
);
3364 /* Update all interface's distribute list. */
3366 static void rip_distribute_update_all(struct prefix_list
*notused
)
3368 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3369 struct interface
*ifp
;
3371 FOR_ALL_INTERFACES (vrf
, ifp
)
3372 rip_distribute_update_interface(ifp
);
3375 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3377 rip_distribute_update_all(NULL
);
3380 /* Delete all added rip route. */
3381 void rip_clean(struct rip
*rip
)
3383 rip_interfaces_clean(rip
);
3386 rip_instance_disable(rip
);
3388 stream_free(rip
->obuf
);
3390 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3391 if (rip
->redist
[i
].route_map
.name
)
3392 free(rip
->redist
[i
].route_map
.name
);
3394 route_table_finish(rip
->table
);
3395 route_table_finish(rip
->neighbor
);
3396 list_delete(&rip
->peer_list
);
3397 distribute_list_delete(&rip
->distribute_ctx
);
3398 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3400 rip_clean_network(rip
);
3401 rip_passive_nondefault_clean(rip
);
3402 vector_free(rip
->enable_interface
);
3403 route_table_finish(rip
->enable_network
);
3404 vector_free(rip
->passive_nondefault
);
3405 list_delete(&rip
->offset_list_master
);
3406 route_table_finish(rip
->distance_table
);
3408 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3409 XFREE(MTYPE_TMP
, rip
->default_bfd_profile
);
3410 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3411 XFREE(MTYPE_RIP
, rip
);
3414 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3415 struct if_rmap
*if_rmap
)
3417 struct interface
*ifp
= NULL
;
3418 struct rip_interface
*ri
;
3419 struct route_map
*rmap
;
3420 struct vrf
*vrf
= NULL
;
3423 vrf
= vrf_lookup_by_name(ctx
->name
);
3425 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3430 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3431 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3433 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3435 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3437 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3439 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3440 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3442 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3444 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3446 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3449 void rip_if_rmap_update_interface(struct interface
*ifp
)
3451 struct rip_interface
*ri
= ifp
->info
;
3452 struct rip
*rip
= ri
->rip
;
3453 struct if_rmap
*if_rmap
;
3454 struct if_rmap_ctx
*ctx
;
3458 ctx
= rip
->if_rmap_ctx
;
3461 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3463 rip_if_rmap_update(ctx
, if_rmap
);
3466 static void rip_routemap_update_redistribute(struct rip
*rip
)
3468 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3469 if (rip
->redist
[i
].route_map
.name
) {
3470 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3471 rip
->redist
[i
].route_map
.name
);
3472 route_map_counter_increment(
3473 rip
->redist
[i
].route_map
.map
);
3479 static void rip_routemap_update(const char *notused
)
3481 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3483 struct interface
*ifp
;
3485 FOR_ALL_INTERFACES (vrf
, ifp
)
3486 rip_if_rmap_update_interface(ifp
);
3490 rip_routemap_update_redistribute(rip
);
3493 /* Link RIP instance to VRF. */
3494 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3496 struct interface
*ifp
;
3499 rip
->distribute_ctx
->vrf
= vrf
;
3502 FOR_ALL_INTERFACES (vrf
, ifp
)
3503 rip_interface_sync(ifp
);
3506 /* Unlink RIP instance from VRF. */
3507 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3509 struct interface
*ifp
;
3512 rip
->distribute_ctx
->vrf
= NULL
;
3515 FOR_ALL_INTERFACES (vrf
, ifp
)
3516 rip_interface_sync(ifp
);
3519 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3523 rip_vrf_link(rip
, vrf
);
3524 rip
->enabled
= true;
3526 /* Resend all redistribute requests. */
3527 rip_redistribute_enable(rip
);
3529 /* Create read and timer thread. */
3530 rip_event(rip
, RIP_READ
, rip
->sock
);
3531 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3533 rip_zebra_vrf_register(vrf
);
3536 static void rip_instance_disable(struct rip
*rip
)
3538 struct vrf
*vrf
= rip
->vrf
;
3539 struct route_node
*rp
;
3541 /* Clear RIP routes */
3542 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3543 struct rip_info
*rinfo
;
3545 struct listnode
*listnode
;
3547 if ((list
= rp
->info
) == NULL
)
3550 rinfo
= listgetdata(listhead(list
));
3551 if (rip_route_rte(rinfo
))
3552 rip_zebra_ipv4_delete(rip
, rp
);
3554 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3555 EVENT_OFF(rinfo
->t_timeout
);
3556 EVENT_OFF(rinfo
->t_garbage_collect
);
3557 rip_info_free(rinfo
);
3561 route_unlock_node(rp
);
3564 /* Flush all redistribute requests. */
3565 rip_redistribute_disable(rip
);
3567 /* Cancel RIP related timers. */
3568 EVENT_OFF(rip
->t_update
);
3569 EVENT_OFF(rip
->t_triggered_update
);
3570 EVENT_OFF(rip
->t_triggered_interval
);
3572 /* Cancel read thread. */
3573 EVENT_OFF(rip
->t_read
);
3575 /* Close RIP socket. */
3579 /* Clear existing peers. */
3580 list_delete_all_node(rip
->peer_list
);
3582 rip_zebra_vrf_deregister(vrf
);
3584 rip_vrf_unlink(rip
, vrf
);
3585 rip
->enabled
= false;
3588 static int rip_vrf_new(struct vrf
*vrf
)
3590 if (IS_RIP_DEBUG_EVENT
)
3591 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3597 static int rip_vrf_delete(struct vrf
*vrf
)
3601 if (IS_RIP_DEBUG_EVENT
)
3602 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3605 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3614 static int rip_vrf_enable(struct vrf
*vrf
)
3619 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3620 if (!rip
|| rip
->enabled
)
3623 if (IS_RIP_DEBUG_EVENT
)
3624 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3627 /* Activate the VRF RIP instance. */
3628 if (!rip
->enabled
) {
3629 socket
= rip_create_socket(vrf
);
3633 rip_instance_enable(rip
, vrf
, socket
);
3639 static int rip_vrf_disable(struct vrf
*vrf
)
3643 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3644 if (!rip
|| !rip
->enabled
)
3647 if (IS_RIP_DEBUG_EVENT
)
3648 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3651 /* Deactivate the VRF RIP instance. */
3653 rip_instance_disable(rip
);
3658 void rip_vrf_init(void)
3660 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
);
3665 void rip_vrf_terminate(void)
3670 /* Allocate new rip structure and set default value. */
3673 /* Install top nodes. */
3674 install_node(&rip_node
);
3676 /* Install rip commands. */
3677 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3678 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3680 install_default(RIP_NODE
);
3682 /* Debug related init. */
3685 /* Access list install. */
3687 access_list_add_hook(rip_distribute_update_all_wrapper
);
3688 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3690 /* Prefix list initialize.*/
3692 prefix_list_add_hook(rip_distribute_update_all
);
3693 prefix_list_delete_hook(rip_distribute_update_all
);
3696 rip_route_map_init();
3698 route_map_add_hook(rip_routemap_update
);
3699 route_map_delete_hook(rip_routemap_update
);
3701 if_rmap_init(RIP_NODE
);