1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RIP version 1 and 2.
3 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
19 #include "sockunion.h"
24 #include "distribute.h"
25 #ifdef CRYPTO_INTERNAL
30 #include "lib_errors.h"
31 #include "northbound_cli.h"
33 #include "lib/printfrr.h"
35 #include "ripd/ripd.h"
36 #include "ripd/rip_nb.h"
37 #include "ripd/rip_bfd.h"
38 #include "ripd/rip_debug.h"
39 #include "ripd/rip_errors.h"
40 #include "ripd/rip_interface.h"
42 /* UDP receive buffer size */
43 #define RIP_UDP_RCV_BUF 41600
45 DEFINE_MGROUP(RIPD
, "ripd");
46 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure");
47 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name");
48 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info");
49 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance");
52 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
54 static void rip_triggered_update(struct event
*);
55 static int rip_update_jitter(unsigned long);
56 static void rip_distance_table_node_cleanup(struct route_table
*table
,
57 struct route_node
*node
);
58 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
59 static void rip_instance_disable(struct rip
*rip
);
61 static void rip_distribute_update(struct distribute_ctx
*ctx
,
62 struct distribute
*dist
);
64 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
65 struct if_rmap
*if_rmap
);
67 /* RIP output routes type. */
68 enum { rip_all_route
, rip_changed_route
};
70 /* RIP command strings. */
71 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
72 {RIP_RESPONSE
, "RESPONSE"},
73 {RIP_TRACEON
, "TRACEON"},
74 {RIP_TRACEOFF
, "TRACEOFF"},
76 {RIP_POLL_ENTRY
, "POLL ENTRY"},
79 /* Generate rb-tree of RIP instances. */
80 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
82 return strcmp(a
->vrf_name
, b
->vrf_name
);
84 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
86 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
88 /* Utility function to set broadcast option to the socket. */
89 static int sockopt_broadcast(int sock
)
94 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
97 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
103 int rip_route_rte(struct rip_info
*rinfo
)
105 return (rinfo
->type
== ZEBRA_ROUTE_RIP
106 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
109 static struct rip_info
*rip_info_new(void)
111 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
114 void rip_info_free(struct rip_info
*rinfo
)
116 XFREE(MTYPE_RIP_INFO
, rinfo
);
119 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
121 return route_table_get_info(rinfo
->rp
->table
);
124 /* RIP route garbage collect timer. */
125 static void rip_garbage_collect(struct event
*t
)
127 struct rip_info
*rinfo
;
128 struct route_node
*rp
;
130 rinfo
= EVENT_ARG(t
);
132 /* Off timeout timer. */
133 EVENT_OFF(rinfo
->t_timeout
);
135 /* Get route_node pointer. */
138 /* Unlock route_node. */
139 listnode_delete(rp
->info
, rinfo
);
140 if (list_isempty((struct list
*)rp
->info
)) {
141 list_delete((struct list
**)&rp
->info
);
142 route_unlock_node(rp
);
145 /* Free RIP routing information. */
146 rip_info_free(rinfo
);
149 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
151 /* Add new route to the ECMP list.
152 * RETURN: the new entry added in the list, or NULL if it is not the first
153 * entry and ECMP is not allowed.
155 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
157 struct route_node
*rp
= rinfo_new
->rp
;
158 struct rip_info
*rinfo
= NULL
;
159 struct list
*list
= NULL
;
161 if (rp
->info
== NULL
)
162 rp
->info
= list_new();
163 list
= (struct list
*)rp
->info
;
165 /* If ECMP is not allowed and some entry already exists in the list,
167 if (listcount(list
) && !rip
->ecmp
)
170 rinfo
= rip_info_new();
171 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
172 listnode_add(list
, rinfo
);
174 if (rip_route_rte(rinfo
)) {
175 rip_timeout_update(rip
, rinfo
);
176 rip_zebra_ipv4_add(rip
, rp
);
179 /* Set the route change flag on the first entry. */
180 rinfo
= listgetdata(listhead(list
));
181 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
183 /* Signal the output process to trigger an update (see section 2.5). */
184 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
189 /* Replace the ECMP list with the new route.
190 * RETURN: the new entry added in the list
192 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
194 struct route_node
*rp
= rinfo_new
->rp
;
195 struct list
*list
= (struct list
*)rp
->info
;
196 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
197 struct listnode
*node
= NULL
, *nextnode
= NULL
;
199 if (list
== NULL
|| listcount(list
) == 0)
200 return rip_ecmp_add(rip
, rinfo_new
);
202 /* Get the first entry */
203 rinfo
= listgetdata(listhead(list
));
205 /* Learnt route replaced by a local one. Delete it from zebra. */
206 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
207 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
208 rip_zebra_ipv4_delete(rip
, rp
);
210 /* Re-use the first entry, and delete the others. */
211 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
212 if (tmp_rinfo
== rinfo
)
215 EVENT_OFF(tmp_rinfo
->t_timeout
);
216 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
217 list_delete_node(list
, node
);
218 rip_info_free(tmp_rinfo
);
221 EVENT_OFF(rinfo
->t_timeout
);
222 EVENT_OFF(rinfo
->t_garbage_collect
);
223 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
225 if (rip_route_rte(rinfo
)) {
226 rip_timeout_update(rip
, rinfo
);
227 /* The ADD message implies an update. */
228 rip_zebra_ipv4_add(rip
, rp
);
231 /* Set the route change flag. */
232 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
234 /* Signal the output process to trigger an update (see section 2.5). */
235 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
240 /* Delete one route from the ECMP list.
242 * null - the entry is freed, and other entries exist in the list
243 * the entry - the entry is the last one in the list; its metric is set
244 * to INFINITY, and the garbage collector is started for it
246 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
248 struct route_node
*rp
= rinfo
->rp
;
249 struct list
*list
= (struct list
*)rp
->info
;
251 EVENT_OFF(rinfo
->t_timeout
);
253 if (listcount(list
) > 1) {
254 /* Some other ECMP entries still exist. Just delete this entry.
256 EVENT_OFF(rinfo
->t_garbage_collect
);
257 listnode_delete(list
, rinfo
);
258 if (rip_route_rte(rinfo
)
259 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
260 /* The ADD message implies the update. */
261 rip_zebra_ipv4_add(rip
, rp
);
262 rip_info_free(rinfo
);
265 assert(rinfo
== listgetdata(listhead(list
)));
267 /* This is the only entry left in the list. We must keep it in
268 * the list for garbage collection time, with INFINITY metric.
271 rinfo
->metric
= RIP_METRIC_INFINITY
;
272 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
275 if (rip_route_rte(rinfo
)
276 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
277 rip_zebra_ipv4_delete(rip
, rp
);
280 /* Set the route change flag on the first entry. */
281 rinfo
= listgetdata(listhead(list
));
282 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
284 /* Signal the output process to trigger an update (see section 2.5). */
285 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
290 /* Timeout RIP routes. */
291 static void rip_timeout(struct event
*t
)
293 struct rip_info
*rinfo
= EVENT_ARG(t
);
294 struct rip
*rip
= rip_info_get_instance(rinfo
);
296 rip_ecmp_delete(rip
, rinfo
);
299 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
301 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
302 EVENT_OFF(rinfo
->t_timeout
);
303 event_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
308 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
309 struct rip_interface
*ri
)
311 struct distribute
*dist
;
312 struct access_list
*alist
;
313 struct prefix_list
*plist
;
314 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
316 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
318 /* Input distribute-list filtering. */
319 if (ri
->list
[rip_distribute
] &&
320 access_list_apply(ri
->list
[rip_distribute
], (struct prefix
*)p
) ==
322 if (IS_RIP_DEBUG_PACKET
)
323 zlog_debug("%pFX filtered by distribute %s", p
, inout
);
327 if (ri
->prefix
[rip_distribute
] &&
328 prefix_list_apply(ri
->prefix
[rip_distribute
], (struct prefix
*)p
) ==
330 if (IS_RIP_DEBUG_PACKET
)
331 zlog_debug("%pFX filtered by prefix-list %s", p
, inout
);
335 /* All interface filter check. */
336 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
340 if (dist
->list
[distribute
]) {
341 alist
= access_list_lookup(AFI_IP
, dist
->list
[distribute
]);
344 if (access_list_apply(alist
, (struct prefix
*)p
) ==
346 if (IS_RIP_DEBUG_PACKET
)
348 "%pFX filtered by distribute %s",
354 if (dist
->prefix
[distribute
]) {
355 plist
= prefix_list_lookup(AFI_IP
, dist
->prefix
[distribute
]);
358 if (prefix_list_apply(plist
, (struct prefix
*)p
) ==
360 if (IS_RIP_DEBUG_PACKET
)
362 "%pFX filtered by prefix-list %s",
372 /* Check nexthop address validity. */
373 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
375 struct interface
*ifp
;
376 struct listnode
*cnode
;
377 struct connected
*ifc
;
380 /* If nexthop address matches local configured address then it is
383 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
384 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
387 if (p
->family
== AF_INET
388 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
395 /* RIP add route to routing table. */
396 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
397 struct interface
*ifp
)
401 struct prefix_ipv4 p
;
402 struct route_node
*rp
;
403 struct rip_info
*rinfo
= NULL
, newinfo
;
404 struct rip_interface
*ri
;
405 struct in_addr
*nexthop
;
407 unsigned char old_dist
, new_dist
;
408 struct list
*list
= NULL
;
409 struct listnode
*node
= NULL
;
411 /* Make prefix structure. */
412 memset(&p
, 0, sizeof(struct prefix_ipv4
));
414 p
.prefix
= rte
->prefix
;
415 p
.prefixlen
= ip_masklen(rte
->mask
);
417 /* Make sure mask is applied. */
423 /* Apply input filters. */
424 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
428 memset(&newinfo
, 0, sizeof(newinfo
));
429 newinfo
.type
= ZEBRA_ROUTE_RIP
;
430 newinfo
.sub_type
= RIP_ROUTE_RTE
;
431 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
432 newinfo
.from
= from
->sin_addr
;
433 newinfo
.nh
.ifindex
= ifp
->ifindex
;
434 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
435 newinfo
.metric
= rte
->metric
;
436 newinfo
.metric_out
= rte
->metric
; /* XXX */
437 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
439 /* Modify entry according to the interface routemap. */
440 if (ri
->routemap
[RIP_FILTER_IN
]) {
441 /* The object should be of the type of rip_info */
442 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
443 (struct prefix
*)&p
, &newinfo
);
445 if (ret
== RMAP_DENYMATCH
) {
446 if (IS_RIP_DEBUG_PACKET
)
448 "RIP %pFX is filtered by route-map in",
453 /* Get back the object */
454 rte
->nexthop
= newinfo
.nexthop_out
;
455 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
456 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
460 /* Once the entry has been validated, update the metric by
461 adding the cost of the network on which the message
462 arrived. If the result is greater than infinity, use infinity
463 (RFC2453 Sec. 3.9.2) */
464 /* Zebra ripd can handle offset-list in. */
465 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
467 /* If offset-list does not modify the metric use interface's
470 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
472 if (rte
->metric
> RIP_METRIC_INFINITY
)
473 rte
->metric
= RIP_METRIC_INFINITY
;
475 /* Set nexthop pointer. */
476 if (rte
->nexthop
.s_addr
== INADDR_ANY
)
477 nexthop
= &from
->sin_addr
;
479 nexthop
= &rte
->nexthop
;
481 /* Check if nexthop address is myself, then do nothing. */
482 if (rip_nexthop_check(rip
, nexthop
) < 0) {
483 if (IS_RIP_DEBUG_PACKET
)
484 zlog_debug("Nexthop address %pI4 is myself",
489 /* Get index for the prefix. */
490 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
493 newinfo
.nh
.gate
.ipv4
= *nexthop
;
494 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
495 newinfo
.metric
= rte
->metric
;
496 newinfo
.tag
= ntohs(rte
->tag
);
497 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
499 new_dist
= newinfo
.distance
? newinfo
.distance
500 : ZEBRA_RIP_DISTANCE_DEFAULT
;
502 /* Check to see whether there is already RIP route on the table. */
503 if ((list
= rp
->info
) != NULL
)
504 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
505 /* Need to compare with redistributed entry or local
507 if (!rip_route_rte(rinfo
))
510 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
511 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
514 if (listnextnode(node
))
517 /* Not found in the list */
519 if (rte
->metric
> rinfo
->metric
) {
520 /* New route has a greater metric.
522 route_unlock_node(rp
);
526 if (rte
->metric
< rinfo
->metric
)
527 /* New route has a smaller metric.
528 * Replace the ECMP list
529 * with the new one in below. */
532 /* Metrics are same. We compare the distances.
534 old_dist
= rinfo
->distance
? rinfo
->distance
535 : ZEBRA_RIP_DISTANCE_DEFAULT
;
537 if (new_dist
> old_dist
) {
538 /* New route has a greater distance.
540 route_unlock_node(rp
);
544 if (new_dist
< old_dist
)
545 /* New route has a smaller distance.
546 * Replace the ECMP list
547 * with the new one in below. */
550 /* Metrics and distances are both same. Keep
552 * the new route is added in the ECMP list in
557 /* Local static route. */
558 if (rinfo
->type
== ZEBRA_ROUTE_RIP
559 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
560 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
561 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
562 route_unlock_node(rp
);
566 /* Redistributed route check. */
567 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
568 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
569 old_dist
= rinfo
->distance
;
570 /* Only routes directly connected to an interface
572 * may have a valid NULL distance */
573 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
576 : ZEBRA_RIP_DISTANCE_DEFAULT
;
577 /* If imported route does not have STRICT precedence,
578 mark it as a ghost */
579 if (new_dist
<= old_dist
580 && rte
->metric
!= RIP_METRIC_INFINITY
)
581 rip_ecmp_replace(rip
, &newinfo
);
583 route_unlock_node(rp
);
590 route_unlock_node(rp
);
592 /* Now, check to see whether there is already an explicit route
593 for the destination prefix. If there is no such route, add
594 this route to the routing table, unless the metric is
595 infinity (there is no point in adding a route which
597 if (rte
->metric
!= RIP_METRIC_INFINITY
)
598 rip_ecmp_add(rip
, &newinfo
);
600 /* Route is there but we are not sure the route is RIP or not.
603 /* If there is an existing route, compare the next hop address
604 to the address of the router from which the datagram came.
605 If this datagram is from the same router as the existing
606 route, reinitialize the timeout. */
607 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
608 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
610 old_dist
= rinfo
->distance
? rinfo
->distance
611 : ZEBRA_RIP_DISTANCE_DEFAULT
;
613 /* Next, compare the metrics. If the datagram is from the same
614 router as the existing route, and the new metric is different
615 than the old one; or, if the new metric is lower than the old
616 one, or if the tag has been changed; or if there is a route
617 with a lower administrave distance; or an update of the
618 distance on the actual route; do the following actions: */
619 if ((same
&& rinfo
->metric
!= rte
->metric
)
620 || (rte
->metric
< rinfo
->metric
)
621 || ((same
) && (rinfo
->metric
== rte
->metric
)
622 && (newinfo
.tag
!= rinfo
->tag
))
623 || (old_dist
> new_dist
)
624 || ((old_dist
!= new_dist
) && same
)) {
625 if (listcount(list
) == 1) {
626 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
627 rip_ecmp_replace(rip
, &newinfo
);
629 rip_ecmp_delete(rip
, rinfo
);
631 if (newinfo
.metric
< rinfo
->metric
)
632 rip_ecmp_replace(rip
, &newinfo
);
633 else if (newinfo
.metric
> rinfo
->metric
)
634 rip_ecmp_delete(rip
, rinfo
);
635 else if (new_dist
< old_dist
)
636 rip_ecmp_replace(rip
, &newinfo
);
637 else if (new_dist
> old_dist
)
638 rip_ecmp_delete(rip
, rinfo
);
640 int update
= CHECK_FLAG(rinfo
->flags
,
645 assert(newinfo
.metric
646 != RIP_METRIC_INFINITY
);
648 EVENT_OFF(rinfo
->t_timeout
);
649 EVENT_OFF(rinfo
->t_garbage_collect
);
650 memcpy(rinfo
, &newinfo
,
651 sizeof(struct rip_info
));
652 rip_timeout_update(rip
, rinfo
);
655 rip_zebra_ipv4_add(rip
, rp
);
657 /* - Set the route change flag on the
659 rinfo
= listgetdata(listhead(list
));
660 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
661 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
664 } else /* same & no change */
665 rip_timeout_update(rip
, rinfo
);
667 /* Unlock tempolary lock of the route. */
668 route_unlock_node(rp
);
672 /* Dump RIP packet */
673 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
678 const char *command_str
;
682 /* Set command string. */
683 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
684 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
686 command_str
= "unknown";
688 /* Dump packet header. */
689 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
690 packet
->version
, size
);
692 /* Dump each routing table entry. */
695 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
696 if (packet
->version
== RIPv2
) {
697 netmask
= ip_masklen(rte
->mask
);
699 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
701 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
702 p
= (uint8_t *)&rte
->prefix
;
705 " family 0x%X type %d auth string: %s",
708 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
709 struct rip_md5_info
*md5
;
711 md5
= (struct rip_md5_info
*)&packet
715 " family 0x%X type %d (MD5 authentication)",
719 " RIP-2 packet len %d Key ID %d Auth Data len %d",
720 ntohs(md5
->packet_len
),
721 md5
->keyid
, md5
->auth_len
);
722 zlog_debug(" Sequence Number %ld",
723 (unsigned long)ntohl(
725 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
726 p
= (uint8_t *)&rte
->prefix
;
729 " family 0x%X type %d (MD5 data)",
733 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
734 p
[0], p
[1], p
[2], p
[3], p
[4],
735 p
[5], p
[6], p
[7], p
[8], p
[9],
736 p
[10], p
[11], p
[12], p
[13],
740 " family 0x%X type %d (Unknown auth type)",
746 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
748 &rte
->prefix
, netmask
, &rte
->nexthop
,
750 (route_tag_t
)ntohs(rte
->tag
),
751 (unsigned long)ntohl(rte
->metric
));
753 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
755 &rte
->prefix
, ntohs(rte
->family
),
756 (route_tag_t
)ntohs(rte
->tag
),
757 (unsigned long)ntohl(rte
->metric
));
762 /* Check if the destination address is valid (unicast; not net 0
763 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
764 check net 0 because we accept default route. */
765 static int rip_destination_check(struct in_addr addr
)
767 uint32_t destination
;
769 /* Convert to host byte order. */
770 destination
= ntohl(addr
.s_addr
);
772 if (IPV4_NET127(destination
))
775 /* Net 0 may match to the default route. */
776 if (IPV4_NET0(destination
) && destination
!= 0)
779 /* Unicast address must belong to class A, B, C. */
780 if (IN_CLASSA(destination
))
782 if (IN_CLASSB(destination
))
784 if (IN_CLASSC(destination
))
790 /* RIP version 2 authentication. */
791 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
792 struct interface
*ifp
)
794 struct rip_interface
*ri
;
795 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
798 /* reject passwords with zeros in the middle of the string */
799 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
800 if (auth_str
[i
] != '\0')
804 if (IS_RIP_DEBUG_EVENT
)
805 zlog_debug("RIPv2 simple password authentication from %pI4",
810 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
811 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
814 /* Simple password authentication. */
816 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
820 struct keychain
*keychain
;
823 keychain
= keychain_lookup(ri
->key_chain
);
824 if (keychain
== NULL
|| keychain
->key
== NULL
)
827 key
= key_match_for_accept(keychain
, auth_str
);
834 /* RIP version 2 authentication with MD5. */
835 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
836 int length
, struct interface
*ifp
)
838 struct rip_interface
*ri
;
839 struct rip_md5_info
*md5
;
840 struct rip_md5_data
*md5data
;
841 struct keychain
*keychain
;
843 #ifdef CRYPTO_OPENSSL
845 #elif CRYPTO_INTERNAL
848 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
850 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
852 if (IS_RIP_DEBUG_EVENT
)
853 zlog_debug("RIPv2 MD5 authentication from %pI4",
857 md5
= (struct rip_md5_info
*)&packet
->rte
;
859 /* Check auth type. */
860 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
863 /* If the authentication length is less than 16, then it must be wrong
865 * any interpretation of rfc2082. Some implementations also interpret
866 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
867 * RIP_AUTH_MD5_COMPAT_SIZE.
869 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
870 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
871 if (IS_RIP_DEBUG_EVENT
)
873 "RIPv2 MD5 authentication, strange authentication length field %d",
878 /* grab and verify check packet length */
879 packet_len
= ntohs(md5
->packet_len
);
881 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
882 if (IS_RIP_DEBUG_EVENT
)
884 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
885 md5
->packet_len
, length
);
889 /* retrieve authentication data */
890 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
893 keychain
= keychain_lookup(ri
->key_chain
);
894 if (keychain
== NULL
)
897 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
898 if (key
== NULL
|| key
->string
== NULL
)
901 memcpy(auth_str
, key
->string
,
902 MIN(sizeof(auth_str
), strlen(key
->string
)));
903 } else if (ri
->auth_str
)
904 memcpy(auth_str
, ri
->auth_str
,
905 MIN(sizeof(auth_str
), strlen(ri
->auth_str
)));
907 if (auth_str
[0] == 0)
910 /* MD5 digest authentication. */
911 #ifdef CRYPTO_OPENSSL
912 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
913 ctx
= EVP_MD_CTX_new();
914 EVP_DigestInit(ctx
, EVP_md5());
915 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
916 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
917 EVP_DigestFinal(ctx
, digest
, &md5_size
);
918 EVP_MD_CTX_free(ctx
);
919 #elif CRYPTO_INTERNAL
920 memset(&ctx
, 0, sizeof(ctx
));
922 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
923 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
924 MD5Final(digest
, &ctx
);
927 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
933 /* Pick correct auth string for sends, prepare auth_str buffer for use.
934 * (left justified and padded).
936 * presumes one of ri or key is valid, and that the auth strings they point
937 * to are nul terminated. If neither are present, auth_str will be fully
941 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
942 char *auth_str
, int len
)
946 memset(auth_str
, 0, len
);
947 if (key
&& key
->string
)
948 memcpy(auth_str
, key
->string
,
949 MIN((size_t)len
, strlen(key
->string
)));
950 else if (ri
->auth_str
)
951 memcpy(auth_str
, ri
->auth_str
,
952 MIN((size_t)len
, strlen(ri
->auth_str
)));
957 /* Write RIPv2 simple password authentication information
959 * auth_str is presumed to be 2 bytes and correctly prepared
960 * (left justified and zero padded).
962 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
964 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
966 stream_putw(s
, RIP_FAMILY_AUTH
);
967 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
968 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
973 /* write RIPv2 MD5 "authentication header"
974 * (uses the auth key data field)
976 * Digest offset field is set to 0.
978 * returns: offset of the digest offset field, which must be set when
979 * length to the auth-data MD5 digest is known.
981 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
985 static uint32_t seq
= 0;
987 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
989 /* MD5 authentication. */
990 stream_putw(s
, RIP_FAMILY_AUTH
);
991 stream_putw(s
, RIP_AUTH_MD5
);
993 /* MD5 AH digest offset field.
995 * Set to placeholder value here, to true value when RIP-2 Packet length
996 * is known. Actual value is set in .....().
998 doff
= stream_get_endp(s
);
1003 stream_putc(s
, key
->index
% 256);
1007 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1008 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1010 * to be configurable.
1012 stream_putc(s
, ri
->md5_auth_len
);
1014 /* Sequence Number (non-decreasing). */
1015 /* RFC2080: The value used in the sequence number is
1016 arbitrary, but two suggestions are the time of the
1017 message's creation or a simple message counter. */
1018 stream_putl(s
, ++seq
);
1020 /* Reserved field must be zero. */
1027 /* If authentication is in used, write the appropriate header
1028 * returns stream offset to which length must later be written
1029 * or 0 if this is not required
1031 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1032 struct key
*key
, char *auth_str
, int len
)
1034 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1036 switch (ri
->auth_type
) {
1037 case RIP_AUTH_SIMPLE_PASSWORD
:
1038 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1039 rip_auth_simple_write(s
, auth_str
, len
);
1042 return rip_auth_md5_ah_write(s
, ri
, key
);
1048 /* Write RIPv2 MD5 authentication data trailer */
1049 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1050 size_t doff
, char *auth_str
, int authlen
)
1053 #ifdef CRYPTO_OPENSSL
1055 #elif CRYPTO_INTERNAL
1058 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1060 /* Make it sure this interface is configured as MD5
1062 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1063 && (authlen
== RIP_AUTH_MD5_SIZE
));
1066 /* Get packet length. */
1067 len
= stream_get_endp(s
);
1069 /* Check packet length. */
1070 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1071 flog_err(EC_RIP_PACKET
,
1072 "%s: packet length %ld is less than minimum length.",
1077 /* Set the digest offset length in the header */
1078 stream_putw_at(s
, doff
, len
);
1080 /* Set authentication data. */
1081 stream_putw(s
, RIP_FAMILY_AUTH
);
1082 stream_putw(s
, RIP_AUTH_DATA
);
1084 /* Generate a digest for the RIP packet. */
1085 #ifdef CRYPTO_OPENSSL
1086 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1087 ctx
= EVP_MD_CTX_new();
1088 EVP_DigestInit(ctx
, EVP_md5());
1089 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1090 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1091 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1092 EVP_MD_CTX_free(ctx
);
1093 #elif CRYPTO_INTERNAL
1094 memset(&ctx
, 0, sizeof(ctx
));
1096 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1097 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1098 MD5Final(digest
, &ctx
);
1101 /* Copy the digest to the packet. */
1102 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1105 /* RIP routing information. */
1106 static void rip_response_process(struct rip_packet
*packet
, int size
,
1107 struct sockaddr_in
*from
,
1108 struct connected
*ifc
)
1110 struct rip_interface
*ri
= ifc
->ifp
->info
;
1111 struct rip
*rip
= ri
->rip
;
1114 struct prefix_ipv4 ifaddr
;
1115 struct prefix_ipv4 ifaddrclass
;
1118 memset(&ifaddr
, 0, sizeof(ifaddr
));
1119 /* We don't know yet. */
1122 /* The Response must be ignored if it is not from the RIP
1123 port. (RFC2453 - Sec. 3.9.2)*/
1124 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1125 zlog_info("response doesn't come from RIP port: %d",
1127 rip_peer_bad_packet(rip
, ri
, from
);
1131 /* The datagram's IPv4 source address should be checked to see
1132 whether the datagram is from a valid neighbor; the source of the
1133 datagram must be on a directly connected network (RFC2453 - Sec.
1135 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1139 "This datagram doesn't come from a valid neighbor: %pI4",
1141 rip_peer_bad_packet(rip
, ri
, from
);
1145 /* It is also worth checking to see whether the response is from one
1146 of the router's own addresses. */
1148 ; /* Alredy done in rip_read () */
1150 /* Update RIP peer. */
1151 rip_peer_update(rip
, ri
, from
, packet
->version
);
1153 /* Set RTE pointer. */
1156 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1157 /* RIPv2 authentication check. */
1158 /* If the Address Family Identifier of the first (and only the
1159 first) entry in the message is 0xFFFF, then the remainder of
1160 the entry contains the authentication. */
1161 /* If the packet gets here it means authentication enabled */
1162 /* Check is done in rip_read(). So, just skipping it */
1163 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1164 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1167 if (rte
->family
!= htons(AF_INET
)) {
1168 /* Address family check. RIP only supports AF_INET. */
1169 zlog_info("Unsupported family %d from %pI4",
1175 if (packet
->version
== RIPv1
&& rte
->tag
!= 0) {
1176 zlog_warn("RIPv1 reserved field is nonzero: %d",
1181 /* - is the destination address valid (e.g., unicast; not net 0
1183 if (!rip_destination_check(rte
->prefix
)) {
1185 "Network is net 0 or net 127 or it is not unicast network");
1186 rip_peer_bad_route(rip
, ri
, from
);
1190 /* Convert metric value to host byte order. */
1191 rte
->metric
= ntohl(rte
->metric
);
1193 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1194 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1195 zlog_info("Route's metric is not in the 1-16 range.");
1196 rip_peer_bad_route(rip
, ri
, from
);
1200 /* RIPv1 does not have nexthop value. */
1201 if (packet
->version
== RIPv1
1202 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1203 zlog_info("RIPv1 packet with nexthop value %pI4",
1205 rip_peer_bad_route(rip
, ri
, from
);
1209 /* That is, if the provided information is ignored, a possibly
1210 sub-optimal, but absolutely valid, route may be taken. If
1211 the received Next Hop is not directly reachable, it should be
1212 treated as 0.0.0.0. */
1213 if (packet
->version
== RIPv2
1214 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1217 /* Multicast address check. */
1218 addrval
= ntohl(rte
->nexthop
.s_addr
);
1219 if (IN_CLASSD(addrval
)) {
1221 "Nexthop %pI4 is multicast address, skip this rte",
1226 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1227 rip
->vrf
->vrf_id
)) {
1228 struct route_node
*rn
;
1229 struct rip_info
*rinfo
;
1231 rn
= route_node_match_ipv4(rip
->table
,
1237 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1240 if (IS_RIP_DEBUG_EVENT
)
1242 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1244 rte
->nexthop
= rinfo
->from
;
1246 if (IS_RIP_DEBUG_EVENT
)
1248 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1250 rte
->nexthop
.s_addr
=
1254 route_unlock_node(rn
);
1256 if (IS_RIP_DEBUG_EVENT
)
1258 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1260 rte
->nexthop
.s_addr
= INADDR_ANY
;
1265 /* For RIPv1, there won't be a valid netmask.
1266 * This is a best guess at the masks. If everyone was using old
1267 * Ciscos before the 'ip subnet zero' option, it would be almost
1270 * Cisco summarize ripv1 advertisements to the classful boundary
1271 * (/16 for class B's) except when the RIP packet does to inside
1272 * the classful network in question.
1274 if ((packet
->version
== RIPv1
1275 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1276 || (packet
->version
== RIPv2
1277 && (rte
->prefix
.s_addr
!= INADDR_ANY
1278 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1279 uint32_t destination
;
1281 if (subnetted
== -1) {
1282 memcpy(&ifaddr
, ifc
->address
, sizeof(ifaddr
));
1283 memcpy(&ifaddrclass
, &ifaddr
,
1284 sizeof(ifaddrclass
));
1285 apply_classful_mask_ipv4(&ifaddrclass
);
1287 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1291 destination
= ntohl(rte
->prefix
.s_addr
);
1293 if (IN_CLASSA(destination
))
1294 masklen2ip(8, &rte
->mask
);
1295 else if (IN_CLASSB(destination
))
1296 masklen2ip(16, &rte
->mask
);
1297 else if (IN_CLASSC(destination
))
1298 masklen2ip(24, &rte
->mask
);
1301 masklen2ip(ifaddrclass
.prefixlen
,
1302 (struct in_addr
*)&destination
);
1303 if ((subnetted
== 1)
1304 && ((rte
->prefix
.s_addr
& destination
)
1305 == ifaddrclass
.prefix
.s_addr
)) {
1306 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1307 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1308 != rte
->prefix
.s_addr
)
1309 masklen2ip(32, &rte
->mask
);
1310 if (IS_RIP_DEBUG_EVENT
)
1311 zlog_debug("Subnetted route %pI4",
1314 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1315 != rte
->prefix
.s_addr
)
1319 if (IS_RIP_DEBUG_EVENT
) {
1320 zlog_debug("Resultant route %pI4",
1322 zlog_debug("Resultant mask %pI4",
1327 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1328 ignore the entry. */
1329 if ((packet
->version
== RIPv2
)
1330 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1331 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1332 != rte
->prefix
.s_addr
)) {
1334 "RIPv2 address %pI4 is not mask /%d applied one",
1335 &rte
->prefix
, ip_masklen(rte
->mask
));
1336 rip_peer_bad_route(rip
, ri
, from
);
1340 /* Default route's netmask is ignored. */
1341 if (packet
->version
== RIPv2
1342 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1343 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1344 if (IS_RIP_DEBUG_EVENT
)
1346 "Default route with non-zero netmask. Set zero to netmask");
1347 rte
->mask
.s_addr
= INADDR_ANY
;
1350 /* Routing table updates. */
1351 rip_rte_process(rte
, from
, ifc
->ifp
);
1355 /* Make socket for RIP protocol. */
1356 int rip_create_socket(struct vrf
*vrf
)
1360 struct sockaddr_in addr
;
1361 const char *vrf_dev
= NULL
;
1363 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1364 addr
.sin_family
= AF_INET
;
1365 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1366 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1367 addr
.sin_len
= sizeof(struct sockaddr_in
);
1368 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1369 /* sending port must always be the RIP port */
1370 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1372 /* Make datagram socket. */
1373 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1374 vrf_dev
= vrf
->name
;
1375 frr_with_privs(&ripd_privs
) {
1376 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1379 flog_err_sys(EC_LIB_SOCKET
,
1380 "Cannot create UDP socket: %s",
1381 safe_strerror(errno
));
1386 sockopt_broadcast(sock
);
1387 sockopt_reuseaddr(sock
);
1388 sockopt_reuseport(sock
);
1389 setsockopt_ipv4_multicast_loop(sock
, 0);
1390 #ifdef IPTOS_PREC_INTERNETCONTROL
1391 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1393 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1395 frr_with_privs(&ripd_privs
) {
1396 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1398 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1399 __func__
, sock
, &addr
.sin_addr
,
1400 (int)ntohs(addr
.sin_port
),
1401 safe_strerror(errno
));
1411 /* RIP packet send to destination address, on interface denoted by
1412 * by connected argument. NULL to argument denotes destination should be
1413 * should be RIP multicast group
1415 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1416 struct connected
*ifc
)
1418 struct rip_interface
*ri
;
1421 struct sockaddr_in sin
;
1425 struct cmsghdr
*cmsgptr
;
1426 char adata
[256] = {};
1427 struct in_pktinfo
*pkt
;
1428 #endif /* GNU_LINUX */
1430 assert(ifc
!= NULL
);
1431 ri
= ifc
->ifp
->info
;
1434 if (IS_RIP_DEBUG_PACKET
) {
1435 #define ADDRESS_SIZE 20
1436 char dst
[ADDRESS_SIZE
];
1439 inet_ntop(AF_INET
, &to
->sin_addr
, dst
, sizeof(dst
));
1441 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1442 inet_ntop(AF_INET
, &sin
.sin_addr
, dst
, sizeof(dst
));
1445 zlog_debug("%s %pI4 > %s (%s)", __func__
,
1446 &ifc
->address
->u
.prefix4
, dst
, ifc
->ifp
->name
);
1449 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1451 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1452 * configured with multiple addresses on the same
1453 * subnet: the first address on the subnet is configured
1454 * "primary", and all subsequent addresses on that subnet
1455 * are treated as "secondary" addresses. In order to avoid
1456 * routing-table bloat on other rip listeners, we do not send
1457 * out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1458 * XXX Since Linux is the only system for which the
1459 * ZEBRA_IFA_SECONDARY flag is set, we would end up
1460 * sending a packet for a "secondary" source address on
1461 * non-linux systems.
1463 if (IS_RIP_DEBUG_PACKET
)
1464 zlog_debug("duplicate dropped");
1468 /* Make destination address. */
1469 memset(&sin
, 0, sizeof(sin
));
1470 sin
.sin_family
= AF_INET
;
1471 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1472 sin
.sin_len
= sizeof(struct sockaddr_in
);
1473 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1475 /* When destination is specified, use it's port and address. */
1477 sin
.sin_port
= to
->sin_port
;
1478 sin
.sin_addr
= to
->sin_addr
;
1480 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1481 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1483 rip_interface_multicast_set(rip
->sock
, ifc
);
1486 memset(&msg
, 0, sizeof(msg
));
1487 msg
.msg_name
= (void *)&sin
;
1488 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1495 msg
.msg_control
= (void *)adata
;
1496 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1498 cmsgptr
= (struct cmsghdr
*)adata
;
1499 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1500 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1501 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1502 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1503 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1504 pkt
->ipi_spec_dst
.s_addr
= ifc
->address
->u
.prefix4
.s_addr
;
1505 #endif /* GNU_LINUX */
1507 ret
= sendmsg(rip
->sock
, &msg
, 0);
1509 if (IS_RIP_DEBUG_EVENT
)
1510 zlog_debug("SEND to %pI4 port %d", &sin
.sin_addr
,
1511 ntohs(sin
.sin_port
));
1514 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1519 /* Add redistributed route to RIP table. */
1520 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1521 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1522 unsigned int metric
, unsigned char distance
,
1526 struct route_node
*rp
= NULL
;
1527 struct rip_info
*rinfo
= NULL
, newinfo
;
1528 struct list
*list
= NULL
;
1530 /* Redistribute route */
1531 ret
= rip_destination_check(p
->prefix
);
1535 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1537 memset(&newinfo
, 0, sizeof(newinfo
));
1538 newinfo
.type
= type
;
1539 newinfo
.sub_type
= sub_type
;
1541 newinfo
.external_metric
= metric
;
1542 newinfo
.distance
= distance
;
1543 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1548 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1549 rinfo
= listgetdata(listhead(list
));
1551 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1552 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1553 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1554 route_unlock_node(rp
);
1558 /* Manually configured RIP route check. */
1559 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1560 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1561 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1562 if (type
!= ZEBRA_ROUTE_RIP
1563 || ((sub_type
!= RIP_ROUTE_STATIC
)
1564 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1565 route_unlock_node(rp
);
1570 (void)rip_ecmp_replace(rip
, &newinfo
);
1571 route_unlock_node(rp
);
1573 (void)rip_ecmp_add(rip
, &newinfo
);
1575 if (IS_RIP_DEBUG_EVENT
) {
1576 zlog_debug("Redistribute new prefix %pFX", p
);
1579 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1582 /* Delete redistributed route from RIP table. */
1583 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1584 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1587 struct route_node
*rp
;
1588 struct rip_info
*rinfo
;
1590 ret
= rip_destination_check(p
->prefix
);
1594 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1596 struct list
*list
= rp
->info
;
1598 if (list
!= NULL
&& listcount(list
) != 0) {
1599 rinfo
= listgetdata(listhead(list
));
1600 if (rinfo
!= NULL
&& rinfo
->type
== type
1601 && rinfo
->sub_type
== sub_type
1602 && rinfo
->nh
.ifindex
== ifindex
) {
1603 /* Perform poisoned reverse. */
1604 rinfo
->metric
= RIP_METRIC_INFINITY
;
1605 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1606 rip_garbage_collect
,
1608 EVENT_OFF(rinfo
->t_timeout
);
1609 rinfo
->flags
|= RIP_RTF_CHANGED
;
1611 if (IS_RIP_DEBUG_EVENT
)
1613 "Poison %pFX on the interface %s with an infinity metric [delete]",
1619 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1622 route_unlock_node(rp
);
1626 /* Response to request called from rip_read ().*/
1627 static void rip_request_process(struct rip_packet
*packet
, int size
,
1628 struct sockaddr_in
*from
, struct connected
*ifc
)
1633 struct prefix_ipv4 p
;
1634 struct route_node
*rp
;
1635 struct rip_info
*rinfo
;
1636 struct rip_interface
*ri
;
1638 /* Does not reponse to the requests on the loopback interfaces */
1639 if (if_is_loopback(ifc
->ifp
))
1642 /* Check RIP process is enabled on this interface. */
1643 ri
= ifc
->ifp
->info
;
1648 /* When passive interface is specified, suppress responses */
1652 /* RIP peer update. */
1653 rip_peer_update(rip
, ri
, from
, packet
->version
);
1655 lim
= ((caddr_t
)packet
) + size
;
1658 /* The Request is processed entry by entry. If there are no
1659 entries, no response is given. */
1660 if (lim
== (caddr_t
)rte
)
1663 /* There is one special case. If there is exactly one entry in the
1664 request, and it has an address family identifier of zero and a
1665 metric of infinity (i.e., 16), then this is a request to send the
1666 entire routing table. */
1667 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1668 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1669 /* All route with split horizon */
1670 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1672 if (ntohs(rte
->family
) != AF_INET
)
1675 /* Examine the list of RTEs in the Request one by one. For each
1676 entry, look up the destination in the router's routing
1677 database and, if there is a route, put that route's metric in
1678 the metric field of the RTE. If there is no explicit route
1679 to the specified destination, put infinity in the metric
1680 field. Once all the entries have been filled in, change the
1681 command from Request to Response and send the datagram back
1682 to the requestor. */
1685 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1686 p
.prefix
= rte
->prefix
;
1687 p
.prefixlen
= ip_masklen(rte
->mask
);
1688 apply_mask_ipv4(&p
);
1690 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1692 rinfo
= listgetdata(
1693 listhead((struct list
*)rp
->info
));
1694 rte
->metric
= htonl(rinfo
->metric
);
1695 route_unlock_node(rp
);
1697 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1699 packet
->command
= RIP_RESPONSE
;
1701 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1703 rip
->counters
.queries
++;
1706 /* First entry point of RIP packet. */
1707 static void rip_read(struct event
*t
)
1709 struct rip
*rip
= EVENT_ARG(t
);
1713 union rip_buf rip_buf
;
1714 struct rip_packet
*packet
;
1715 struct sockaddr_in from
;
1719 struct interface
*ifp
= NULL
;
1720 struct connected
*ifc
;
1721 struct rip_interface
*ri
= NULL
;
1724 /* Fetch socket then register myself. */
1727 /* Add myself to tne next event */
1728 rip_event(rip
, RIP_READ
, sock
);
1730 /* RIPd manages only IPv4. */
1731 memset(&from
, 0, sizeof(from
));
1732 fromlen
= sizeof(struct sockaddr_in
);
1734 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1735 (struct sockaddr
*)&from
, &fromlen
);
1737 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1738 safe_strerror(errno
));
1742 /* Check is this packet comming from myself? */
1743 if (if_check_address(rip
, from
.sin_addr
)) {
1744 if (IS_RIP_DEBUG_PACKET
)
1745 zlog_debug("ignore packet comes from myself (VRF %s)",
1750 /* Which interface is this packet comes from. */
1751 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1758 /* RIP packet received */
1759 if (IS_RIP_DEBUG_EVENT
)
1760 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1761 &from
.sin_addr
, ntohs(from
.sin_port
),
1762 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1764 /* If this packet come from unknown interface, ignore it. */
1765 if (ifp
== NULL
|| ri
== NULL
) {
1767 "%s: cannot find interface for packet from %pI4 port %d (VRF %s)",
1768 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1774 p
.u
.prefix4
= from
.sin_addr
;
1775 p
.prefixlen
= IPV4_MAX_BITLEN
;
1777 ifc
= connected_lookup_prefix(ifp
, &p
);
1781 "%s: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1782 __func__
, &from
.sin_addr
, ntohs(from
.sin_port
),
1783 ifp
->name
, rip
->vrf_name
);
1787 /* Packet length check. */
1788 if (len
< RIP_PACKET_MINSIZ
) {
1789 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1791 rip_peer_bad_packet(rip
, ri
, &from
);
1794 if (len
> RIP_PACKET_MAXSIZ
) {
1795 zlog_warn("packet size %d is larger than max size %d", len
,
1797 rip_peer_bad_packet(rip
, ri
, &from
);
1801 /* Packet alignment check. */
1802 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1803 zlog_warn("packet size %d is wrong for RIP packet alignment",
1805 rip_peer_bad_packet(rip
, ri
, &from
);
1809 /* Set RTE number. */
1810 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1812 /* For easy to handle. */
1813 packet
= &rip_buf
.rip_packet
;
1815 /* RIP version check. */
1816 if (packet
->version
== 0) {
1817 zlog_info("version 0 with command %d received.",
1819 rip_peer_bad_packet(rip
, ri
, &from
);
1823 /* Dump RIP packet. */
1824 if (IS_RIP_DEBUG_RECV
)
1825 rip_packet_dump(packet
, len
, "RECV");
1827 /* RIP version adjust. This code should rethink now. RFC1058 says
1828 that "Version 1 implementations are to ignore this extra data and
1829 process only the fields specified in this document.". So RIPv3
1830 packet should be treated as RIPv1 ignoring must be zero field. */
1831 if (packet
->version
> RIPv2
)
1832 packet
->version
= RIPv2
;
1834 /* Is RIP running or is this RIP neighbor ?*/
1835 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1836 if (IS_RIP_DEBUG_EVENT
)
1837 zlog_debug("RIP is not enabled on interface %s.",
1839 rip_peer_bad_packet(rip
, ri
, &from
);
1843 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1844 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1846 if (vrecv
== RI_RIP_VERSION_NONE
1847 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1848 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1849 if (IS_RIP_DEBUG_PACKET
)
1851 " packet's v%d doesn't fit to if version spec",
1853 rip_peer_bad_packet(rip
, ri
, &from
);
1857 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1858 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1859 accepted; authenticated RIP-2 messages shall be discarded. */
1860 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1861 && (packet
->version
== RIPv2
)
1862 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1863 if (IS_RIP_DEBUG_EVENT
)
1865 "packet RIPv%d is dropped because authentication disabled",
1867 ripd_notif_send_auth_type_failure(ifp
->name
);
1868 rip_peer_bad_packet(rip
, ri
, &from
);
1873 If the router is configured to authenticate RIP-2 messages, then
1874 RIP-1 messages and RIP-2 messages which pass authentication
1875 testing shall be accepted; unauthenticated and failed
1876 authentication RIP-2 messages shall be discarded. For maximum
1877 security, RIP-1 messages should be ignored when authentication is
1878 in use (see section 4.1); otherwise, the routing information from
1879 authenticated messages will be propagated by RIP-1 routers in an
1880 unauthenticated manner.
1882 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1883 * always reply regardless of authentication settings, because:
1885 * - if there other authorised routers on-link, the REQUESTor can
1886 * passively obtain the routing updates anyway
1887 * - if there are no other authorised routers on-link, RIP can
1888 * easily be disabled for the link to prevent giving out information
1889 * on state of this routers RIP routing table..
1891 * I.e. if RIPv1 has any place anymore these days, it's as a very
1892 * simple way to distribute routing information (e.g. to embedded
1893 * hosts / appliances) and the ability to give out RIPv1
1894 * routing-information freely, while still requiring RIPv2
1895 * authentication for any RESPONSEs might be vaguely useful.
1897 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1898 /* Discard RIPv1 messages other than REQUESTs */
1899 if (packet
->command
!= RIP_REQUEST
) {
1900 if (IS_RIP_DEBUG_PACKET
)
1902 "RIPv1 dropped because authentication enabled");
1903 ripd_notif_send_auth_type_failure(ifp
->name
);
1904 rip_peer_bad_packet(rip
, ri
, &from
);
1907 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1908 const char *auth_desc
;
1911 /* There definitely is no authentication in the packet.
1913 if (IS_RIP_DEBUG_PACKET
)
1915 "RIPv2 authentication failed: no auth RTE in packet");
1916 ripd_notif_send_auth_type_failure(ifp
->name
);
1917 rip_peer_bad_packet(rip
, ri
, &from
);
1921 /* First RTE must be an Authentication Family RTE */
1922 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1923 if (IS_RIP_DEBUG_PACKET
)
1925 "RIPv2 dropped because authentication enabled");
1926 ripd_notif_send_auth_type_failure(ifp
->name
);
1927 rip_peer_bad_packet(rip
, ri
, &from
);
1931 /* Check RIPv2 authentication. */
1932 switch (ntohs(packet
->rte
->tag
)) {
1933 case RIP_AUTH_SIMPLE_PASSWORD
:
1934 auth_desc
= "simple";
1935 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1940 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1941 /* Reset RIP packet length to trim MD5 data. */
1947 auth_desc
= "unknown type";
1948 if (IS_RIP_DEBUG_PACKET
)
1950 "RIPv2 Unknown authentication type %d",
1951 ntohs(packet
->rte
->tag
));
1955 if (IS_RIP_DEBUG_PACKET
)
1956 zlog_debug("RIPv2 %s authentication success",
1959 if (IS_RIP_DEBUG_PACKET
)
1960 zlog_debug("RIPv2 %s authentication failure",
1962 ripd_notif_send_auth_failure(ifp
->name
);
1963 rip_peer_bad_packet(rip
, ri
, &from
);
1968 /* Process each command. */
1969 switch (packet
->command
) {
1971 rip_response_process(packet
, len
, &from
, ifc
);
1975 rip_request_process(packet
, len
, &from
, ifc
);
1980 "Obsolete command %s received, please sent it to routed",
1981 lookup_msg(rip_msg
, packet
->command
, NULL
));
1982 rip_peer_bad_packet(rip
, ri
, &from
);
1984 case RIP_POLL_ENTRY
:
1985 zlog_info("Obsolete command %s received",
1986 lookup_msg(rip_msg
, packet
->command
, NULL
));
1987 rip_peer_bad_packet(rip
, ri
, &from
);
1990 zlog_info("Unknown RIP command %d received", packet
->command
);
1991 rip_peer_bad_packet(rip
, ri
, &from
);
1996 /* Write routing table entry to the stream and return next index of
1997 the routing table entry in the stream. */
1998 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
1999 uint8_t version
, struct rip_info
*rinfo
)
2001 struct in_addr mask
;
2003 /* Write routing table entry. */
2004 if (version
== RIPv1
) {
2005 stream_putw(s
, AF_INET
);
2007 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2008 stream_put_ipv4(s
, 0);
2009 stream_put_ipv4(s
, 0);
2010 stream_putl(s
, rinfo
->metric_out
);
2012 masklen2ip(p
->prefixlen
, &mask
);
2014 stream_putw(s
, AF_INET
);
2015 stream_putw(s
, rinfo
->tag_out
);
2016 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2017 stream_put_ipv4(s
, mask
.s_addr
);
2018 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2019 stream_putl(s
, rinfo
->metric_out
);
2025 /* Send update to the ifp or spcified neighbor. */
2026 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2027 int route_type
, uint8_t version
)
2032 struct route_node
*rp
;
2033 struct rip_info
*rinfo
;
2034 struct rip_interface
*ri
;
2035 struct prefix_ipv4
*p
;
2036 struct prefix_ipv4 classfull
;
2037 struct prefix_ipv4 ifaddrclass
;
2038 struct key
*key
= NULL
;
2039 /* this might need to made dynamic if RIP ever supported auth methods
2040 with larger key string sizes */
2041 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2042 size_t doff
= 0; /* offset of digest offset field */
2046 struct list
*list
= NULL
;
2047 struct listnode
*listnode
= NULL
;
2049 /* Logging output event. */
2050 if (IS_RIP_DEBUG_EVENT
) {
2052 zlog_debug("update routes to neighbor %pI4",
2055 zlog_debug("update routes on interface %s ifindex %d",
2056 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2059 /* Get RIP interface. */
2060 ri
= ifc
->ifp
->info
;
2063 /* Set output stream. */
2066 /* Reset stream and RTE counter. */
2068 rtemax
= RIP_MAX_RTE
;
2070 /* If output interface is in simple password authentication mode, we
2071 need space for authentication data. */
2072 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2075 /* If output interface is in MD5 authentication mode, we need space
2076 for authentication header and data. */
2077 if (ri
->auth_type
== RIP_AUTH_MD5
)
2080 /* If output interface is in simple password authentication mode
2081 and string or keychain is specified we need space for auth. data */
2082 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2083 if (ri
->key_chain
) {
2084 struct keychain
*keychain
;
2086 keychain
= keychain_lookup(ri
->key_chain
);
2088 key
= key_lookup_for_send(keychain
);
2090 /* to be passed to auth functions later */
2091 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2092 if (strlen(auth_str
) == 0)
2096 if (version
== RIPv1
) {
2097 memcpy(&ifaddrclass
, ifc
->address
, sizeof(ifaddrclass
));
2098 apply_classful_mask_ipv4(&ifaddrclass
);
2100 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2104 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2110 if (listcount(list
) == 0)
2113 rinfo
= listgetdata(listhead(list
));
2115 * For RIPv1, if we are subnetted, output subnets in our
2116 * network that have the same mask as the output "interface".
2117 * For other networks, only the classfull version is output.
2119 if (version
== RIPv1
) {
2120 p
= (struct prefix_ipv4
*)&rp
->p
;
2122 if (IS_RIP_DEBUG_PACKET
)
2124 "RIPv1 mask check, %pFX considered for output",
2128 prefix_match((struct prefix
*)&ifaddrclass
,
2130 if ((ifc
->address
->prefixlen
!=
2132 (rp
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2135 memcpy(&classfull
, &rp
->p
,
2136 sizeof(struct prefix_ipv4
));
2137 apply_classful_mask_ipv4(&classfull
);
2138 if (rp
->p
.u
.prefix4
.s_addr
!= INADDR_ANY
&&
2139 classfull
.prefixlen
!= rp
->p
.prefixlen
)
2142 if (IS_RIP_DEBUG_PACKET
)
2144 "RIPv1 mask check, %pFX made it through",
2147 p
= (struct prefix_ipv4
*)&rp
->p
;
2149 /* Apply output filters. */
2150 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2154 /* Changed route only output. */
2155 if (route_type
== rip_changed_route
&&
2156 (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2159 /* Split horizon. */
2160 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2162 * We perform split horizon for RIP and connected
2163 * route. For rip routes, we want to suppress the
2164 * route if we would end up sending the route back on
2165 * the interface that we learned it from, with a
2166 * higher metric. For connected routes, we suppress
2167 * the route if the prefix is a subset of the source
2168 * address that we are going to use for the packet
2169 * (in order to handle the case when multiple subnets
2170 * are configured on the same interface).
2173 struct rip_info
*tmp_rinfo
= NULL
;
2174 struct connected
*tmp_ifc
= NULL
;
2176 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2177 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2178 tmp_rinfo
->nh
.ifindex
==
2179 ifc
->ifp
->ifindex
) {
2184 if (!suppress
&& rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2185 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2187 if (prefix_match((struct prefix
*)p
,
2188 tmp_ifc
->address
)) {
2198 /* Preparation for route-map. */
2199 rinfo
->metric_set
= 0;
2200 rinfo
->nexthop_out
.s_addr
= 0;
2201 rinfo
->metric_out
= rinfo
->metric
;
2202 rinfo
->tag_out
= rinfo
->tag
;
2203 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2205 /* In order to avoid some local loops, if the RIP route has
2206 * a nexthop via this interface, keep the nexthop, otherwise
2207 * set it to 0. The nexthop should not be propagated beyond
2208 * the local broadcast/multicast area in order to avoid an
2209 * IGP multi-level recursive look-up. see (4.4)
2211 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2212 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2214 /* Interface route-map */
2215 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2216 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_OUT
],
2217 (struct prefix
*)p
, rinfo
);
2219 if (ret
== RMAP_DENYMATCH
) {
2220 if (IS_RIP_DEBUG_PACKET
)
2222 "RIP %pFX is filtered by route-map out",
2228 /* Apply redistribute route map - continue, if deny */
2229 if (rip
->redist
[rinfo
->type
].route_map
.name
&&
2230 rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2231 ret
= route_map_apply(
2232 rip
->redist
[rinfo
->type
].route_map
.map
,
2233 (struct prefix
*)p
, rinfo
);
2235 if (ret
== RMAP_DENYMATCH
) {
2236 if (IS_RIP_DEBUG_PACKET
)
2238 "%pFX is filtered by route-map",
2244 /* When route-map does not set metric. */
2245 if (!rinfo
->metric_set
) {
2246 /* If redistribute metric is set. */
2247 if (rip
->redist
[rinfo
->type
].metric_config
&&
2248 rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2250 rip
->redist
[rinfo
->type
].metric
;
2252 /* If the route is not connected or localy
2253 * generated one, use default-metric value
2255 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&&
2256 rinfo
->type
!= ZEBRA_ROUTE_CONNECT
&&
2257 rinfo
->metric
!= RIP_METRIC_INFINITY
)
2258 rinfo
->metric_out
= rip
->default_metric
;
2262 /* Apply offset-list */
2263 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2264 rip_offset_list_apply_out(p
, ifc
->ifp
,
2265 &rinfo
->metric_out
);
2267 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2268 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2270 /* Perform split-horizon with poisoned reverse
2271 * for RIP and connected routes.
2273 if (ri
->split_horizon
== RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2275 * We perform split horizon for RIP and connected
2276 * route. For rip routes, we want to suppress the
2277 * route if we would end up sending the route back
2278 * on the interface that we learned it from, with a
2279 * higher metric. For connected routes, we suppress
2280 * the route if the prefix is a subset of the source
2281 * address that we are going to use for the packet
2282 * (in order to handle the case when multiple
2283 * subnets are configured on the same interface).
2285 struct rip_info
*tmp_rinfo
= NULL
;
2286 struct connected
*tmp_ifc
= NULL
;
2288 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2289 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2290 tmp_rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2291 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2293 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
&&
2294 rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2295 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2297 if (prefix_match((struct prefix
*)p
,
2298 tmp_ifc
->address
)) {
2300 RIP_METRIC_INFINITY
;
2306 /* Prepare preamble, auth headers, if needs be */
2308 stream_putc(s
, RIP_RESPONSE
);
2309 stream_putc(s
, version
);
2312 /* auth header for !v1 && !no_auth */
2313 if ((ri
->auth_type
!= RIP_NO_AUTH
) &&
2315 doff
= rip_auth_header_write(
2316 s
, ri
, key
, auth_str
,
2317 RIP_AUTH_SIMPLE_SIZE
);
2320 /* Write RTE to the stream. */
2321 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2322 if (num
== rtemax
) {
2323 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2324 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2325 RIP_AUTH_SIMPLE_SIZE
);
2327 ret
= rip_send_packet(STREAM_DATA(s
),
2328 stream_get_endp(s
), to
, ifc
);
2330 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2332 (struct rip_packet
*)STREAM_DATA(s
),
2333 stream_get_endp(s
), "SEND");
2339 /* Flush unwritten RTE. */
2341 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2342 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2343 RIP_AUTH_SIMPLE_SIZE
);
2345 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2348 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2349 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2350 stream_get_endp(s
), "SEND");
2354 /* Statistics updates. */
2358 /* Send RIP packet to the interface. */
2359 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2362 struct interface
*ifp
= ifc
->ifp
;
2363 struct rip_interface
*ri
= ifp
->info
;
2364 struct sockaddr_in to
;
2366 /* When RIP version is 2 and multicast enable interface. */
2367 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2368 if (IS_RIP_DEBUG_EVENT
)
2369 zlog_debug("multicast announce on %s ", ifp
->name
);
2371 rip_output_process(ifc
, NULL
, route_type
, version
);
2375 /* If we can't send multicast packet, send it with unicast. */
2376 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2377 if (ifc
->address
->family
== AF_INET
) {
2378 /* Destination address and port setting. */
2379 memset(&to
, 0, sizeof(to
));
2380 if (ifc
->destination
)
2381 /* use specified broadcast or peer destination
2383 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2384 else if (ifc
->address
->prefixlen
< IPV4_MAX_BITLEN
)
2385 /* calculate the appropriate broadcast address
2387 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2388 ifc
->address
->u
.prefix4
.s_addr
,
2389 ifc
->address
->prefixlen
);
2391 /* do not know where to send the packet */
2393 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2395 if (IS_RIP_DEBUG_EVENT
)
2396 zlog_debug("%s announce to %pI4 on %s",
2397 CONNECTED_PEER(ifc
) ? "unicast"
2399 &to
.sin_addr
, ifp
->name
);
2401 rip_output_process(ifc
, &to
, route_type
, version
);
2406 /* Update send to all interface and neighbor. */
2407 static void rip_update_process(struct rip
*rip
, int route_type
)
2409 struct listnode
*ifnode
, *ifnnode
;
2410 struct connected
*connected
;
2411 struct interface
*ifp
;
2412 struct rip_interface
*ri
;
2413 struct route_node
*rp
;
2414 struct sockaddr_in to
;
2417 /* Send RIP update to each interface. */
2418 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2419 if (if_is_loopback(ifp
))
2422 if (!if_is_operative(ifp
))
2425 /* Fetch RIP interface information. */
2428 /* When passive interface is specified, suppress announce to the
2437 * If there is no version configuration in the
2438 * interface, use rip's version setting.
2440 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? rip
->version_send
2443 if (IS_RIP_DEBUG_EVENT
)
2444 zlog_debug("SEND UPDATE to %s ifindex %d", ifp
->name
,
2447 /* send update on each connected network */
2448 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2450 if (connected
->address
->family
== AF_INET
) {
2452 rip_update_interface(connected
, RIPv1
,
2454 if ((vsend
& RIPv2
) && if_is_multicast(ifp
))
2455 rip_update_interface(connected
, RIPv2
,
2461 /* RIP send updates to each neighbor. */
2462 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
)) {
2463 if (rp
->info
== NULL
)
2468 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2472 "Neighbor %pI4 doesn't have connected interface!",
2477 /* Set destination address and port */
2478 memset(&to
, 0, sizeof(struct sockaddr_in
));
2479 to
.sin_addr
= p
->u
.prefix4
;
2480 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2482 /* RIP version is rip's configuration. */
2483 rip_output_process(connected
, &to
, route_type
,
2488 /* RIP's periodical timer. */
2489 static void rip_update(struct event
*t
)
2491 struct rip
*rip
= EVENT_ARG(t
);
2493 if (IS_RIP_DEBUG_EVENT
)
2494 zlog_debug("update timer fire!");
2496 /* Process update output. */
2497 rip_update_process(rip
, rip_all_route
);
2499 /* Triggered updates may be suppressed if a regular update is due by
2500 the time the triggered update would be sent. */
2501 EVENT_OFF(rip
->t_triggered_interval
);
2504 /* Register myself. */
2505 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2508 /* Walk down the RIP routing table then clear changed flag. */
2509 static void rip_clear_changed_flag(struct rip
*rip
)
2511 struct route_node
*rp
;
2512 struct rip_info
*rinfo
= NULL
;
2513 struct list
*list
= NULL
;
2514 struct listnode
*listnode
= NULL
;
2516 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2522 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2523 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2524 /* This flag can be set only on the first entry. */
2530 /* Triggered update interval timer. */
2531 static void rip_triggered_interval(struct event
*t
)
2533 struct rip
*rip
= EVENT_ARG(t
);
2537 rip_triggered_update(t
);
2541 /* Execute triggered update. */
2542 static void rip_triggered_update(struct event
*t
)
2544 struct rip
*rip
= EVENT_ARG(t
);
2547 /* Cancel interval timer. */
2548 EVENT_OFF(rip
->t_triggered_interval
);
2551 /* Logging triggered update. */
2552 if (IS_RIP_DEBUG_EVENT
)
2553 zlog_debug("triggered update!");
2555 /* Split Horizon processing is done when generating triggered
2556 updates as well as normal updates (see section 2.6). */
2557 rip_update_process(rip
, rip_changed_route
);
2559 /* Once all of the triggered updates have been generated, the route
2560 change flags should be cleared. */
2561 rip_clear_changed_flag(rip
);
2563 /* After a triggered update is sent, a timer should be set for a
2564 random interval between 1 and 5 seconds. If other changes that
2565 would trigger updates occur before the timer expires, a single
2566 update is triggered when the timer expires. */
2567 interval
= (frr_weak_random() % 5) + 1;
2569 event_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2570 &rip
->t_triggered_interval
);
2573 /* Withdraw redistributed route. */
2574 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2576 struct route_node
*rp
;
2577 struct rip_info
*rinfo
= NULL
;
2578 struct list
*list
= NULL
;
2580 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2586 rinfo
= listgetdata(listhead(list
));
2588 if (rinfo
->type
!= type
)
2591 if (rinfo
->sub_type
== RIP_ROUTE_INTERFACE
)
2594 /* Perform poisoned reverse. */
2595 rinfo
->metric
= RIP_METRIC_INFINITY
;
2596 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
2598 EVENT_OFF(rinfo
->t_timeout
);
2599 rinfo
->flags
|= RIP_RTF_CHANGED
;
2601 if (IS_RIP_DEBUG_EVENT
) {
2602 struct prefix_ipv4
*p
= (struct prefix_ipv4
*)&rp
->p
;
2605 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2607 ifindex2ifname(rinfo
->nh
.ifindex
,
2611 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2615 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2619 vrf
= vrf_lookup_by_id(vrf_id
);
2626 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2630 rip
.vrf_name
= (char *)vrf_name
;
2632 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2635 /* Create new RIP instance and set it to global variable. */
2636 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2640 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2641 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2643 /* Set initial value. */
2644 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2645 rip
->default_metric
=
2646 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2648 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2649 rip
->passive_default
=
2650 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2651 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2653 rip
->timeout_time
= yang_get_default_uint32(
2654 "%s/timers/holddown-interval", RIP_INSTANCE
);
2655 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2658 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2660 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2662 /* Initialize RIP data structures. */
2663 rip
->table
= route_table_init();
2664 route_table_set_info(rip
->table
, rip
);
2665 rip
->neighbor
= route_table_init();
2666 rip
->peer_list
= list_new();
2667 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2668 rip
->peer_list
->del
= rip_peer_list_del
;
2669 rip
->distance_table
= route_table_init();
2670 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2671 rip
->enable_interface
= vector_init(1);
2672 rip
->enable_network
= route_table_init();
2673 rip
->passive_nondefault
= vector_init(1);
2674 rip
->offset_list_master
= list_new();
2675 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2676 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2678 /* Distribute list install. */
2679 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2680 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2681 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2683 /* if rmap install. */
2684 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2685 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2686 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2688 /* Make output stream. */
2689 rip
->obuf
= stream_new(1500);
2691 /* Enable the routing instance if possible. */
2692 if (vrf
&& vrf_is_enabled(vrf
))
2693 rip_instance_enable(rip
, vrf
, socket
);
2699 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2704 /* Sned RIP request to the destination. */
2705 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2706 uint8_t version
, struct connected
*connected
)
2709 struct rip_packet rip_packet
;
2710 struct listnode
*node
, *nnode
;
2712 memset(&rip_packet
, 0, sizeof(rip_packet
));
2714 rip_packet
.command
= RIP_REQUEST
;
2715 rip_packet
.version
= version
;
2716 rte
= rip_packet
.rte
;
2717 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2721 * connected is only sent for ripv1 case, or when
2722 * interface does not support multicast. Caller loops
2723 * over each connected address for this case.
2725 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2727 != sizeof(rip_packet
))
2730 return sizeof(rip_packet
);
2733 /* send request on each connected network */
2734 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2735 struct prefix_ipv4
*p
;
2737 p
= (struct prefix_ipv4
*)connected
->address
;
2739 if (p
->family
!= AF_INET
)
2742 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2744 != sizeof(rip_packet
))
2747 return sizeof(rip_packet
);
2750 static int rip_update_jitter(unsigned long time
)
2752 #define JITTER_BOUND 4
2753 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2754 Given that, we cannot let time be less than JITTER_BOUND seconds.
2755 The RIPv2 RFC says jitter should be small compared to
2756 update_time. We consider 1/JITTER_BOUND to be small.
2759 int jitter_input
= time
;
2762 if (jitter_input
< JITTER_BOUND
)
2763 jitter_input
= JITTER_BOUND
;
2765 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2768 return jitter
/ JITTER_BOUND
;
2771 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2777 event_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2779 case RIP_UPDATE_EVENT
:
2780 EVENT_OFF(rip
->t_update
);
2781 jitter
= rip_update_jitter(rip
->update_time
);
2782 event_add_timer(master
, rip_update
, rip
,
2783 sock
? 2 : rip
->update_time
+ jitter
,
2786 case RIP_TRIGGERED_UPDATE
:
2787 if (rip
->t_triggered_interval
)
2790 event_add_event(master
, rip_triggered_update
, rip
, 0,
2791 &rip
->t_triggered_update
);
2798 struct rip_distance
*rip_distance_new(void)
2800 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2803 void rip_distance_free(struct rip_distance
*rdistance
)
2805 if (rdistance
->access_list
)
2806 free(rdistance
->access_list
);
2807 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2810 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2811 struct route_node
*node
)
2813 struct rip_distance
*rdistance
;
2815 rdistance
= node
->info
;
2817 rip_distance_free(rdistance
);
2820 /* Apply RIP information to distance method. */
2821 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2823 struct route_node
*rn
;
2824 struct prefix_ipv4 p
;
2825 struct rip_distance
*rdistance
;
2826 struct access_list
*alist
;
2828 memset(&p
, 0, sizeof(p
));
2830 p
.prefix
= rinfo
->from
;
2831 p
.prefixlen
= IPV4_MAX_BITLEN
;
2833 /* Check source address. */
2834 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2836 rdistance
= rn
->info
;
2837 route_unlock_node(rn
);
2839 if (rdistance
->access_list
) {
2840 alist
= access_list_lookup(AFI_IP
,
2841 rdistance
->access_list
);
2844 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2848 return rdistance
->distance
;
2851 return rip
->distance
;
2854 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2856 struct route_node
*rn
;
2857 struct rip_distance
*rdistance
;
2861 vty_out(vty
, " Distance: (default is %u)\n",
2862 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2864 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
)) {
2865 rdistance
= rn
->info
;
2867 if (rdistance
== NULL
)
2871 vty_out(vty
, " Address Distance List\n");
2874 snprintfrr(buf
, sizeof(buf
), "%pFX", &rn
->p
);
2875 vty_out(vty
, " %-20s %4d %s\n", buf
, rdistance
->distance
,
2876 rdistance
->access_list
? rdistance
->access_list
: "");
2880 /* Update ECMP routes to zebra when ECMP is disabled. */
2881 void rip_ecmp_disable(struct rip
*rip
)
2883 struct route_node
*rp
;
2884 struct rip_info
*rinfo
, *tmp_rinfo
;
2886 struct listnode
*node
, *nextnode
;
2888 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2893 if (listcount(list
) == 0)
2896 rinfo
= listgetdata(listhead(list
));
2897 if (!rip_route_rte(rinfo
))
2900 /* Drop all other entries, except the first one. */
2901 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
2902 if (tmp_rinfo
== rinfo
)
2905 EVENT_OFF(tmp_rinfo
->t_timeout
);
2906 EVENT_OFF(tmp_rinfo
->t_garbage_collect
);
2907 list_delete_node(list
, node
);
2908 rip_info_free(tmp_rinfo
);
2912 rip_zebra_ipv4_add(rip
, rp
);
2914 /* Set the route change flag. */
2915 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2917 /* Signal the output process to trigger an update. */
2918 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2922 /* Print out routes update time. */
2923 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2928 char timebuf
[TIME_BUF
];
2929 struct event
*thread
;
2931 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2932 clock
= event_timer_remain_second(thread
);
2933 gmtime_r(&clock
, &tm
);
2934 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2935 vty_out(vty
, "%5s", timebuf
);
2936 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2937 clock
= event_timer_remain_second(thread
);
2938 gmtime_r(&clock
, &tm
);
2939 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2940 vty_out(vty
, "%5s", timebuf
);
2944 static const char *rip_route_type_print(int sub_type
)
2949 case RIP_ROUTE_STATIC
:
2951 case RIP_ROUTE_DEFAULT
:
2953 case RIP_ROUTE_REDISTRIBUTE
:
2955 case RIP_ROUTE_INTERFACE
:
2964 "show ip rip [vrf NAME]",
2971 struct route_node
*np
;
2972 struct rip_info
*rinfo
= NULL
;
2973 struct list
*list
= NULL
;
2974 struct listnode
*listnode
= NULL
;
2975 const char *vrf_name
;
2978 if (argv_find(argv
, argc
, "vrf", &idx
))
2979 vrf_name
= argv
[idx
+ 1]->arg
;
2981 vrf_name
= VRF_DEFAULT_NAME
;
2983 rip
= rip_lookup_by_vrf_name(vrf_name
);
2985 vty_out(vty
, "%% RIP instance not found\n");
2988 if (!rip
->enabled
) {
2989 vty_out(vty
, "%% RIP instance is disabled\n");
2994 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
2996 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
2997 " (i) - interface\n\n"
2998 " Network Next Hop Metric From Tag Time\n");
3000 for (np
= route_top(rip
->table
); np
; np
= route_next(np
)) {
3006 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3009 len
= vty_out(vty
, "%c(%s) %pFX",
3010 /* np->lock, For debugging. */
3011 zebra_route_char(rinfo
->type
),
3012 rip_route_type_print(rinfo
->sub_type
),
3018 vty_out(vty
, "%*s", len
, " ");
3020 switch (rinfo
->nh
.type
) {
3021 case NEXTHOP_TYPE_IPV4
:
3022 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3023 vty_out(vty
, "%-20pI4 %2d ",
3024 &rinfo
->nh
.gate
.ipv4
, rinfo
->metric
);
3026 case NEXTHOP_TYPE_IFINDEX
:
3027 vty_out(vty
, "0.0.0.0 %2d ",
3030 case NEXTHOP_TYPE_BLACKHOLE
:
3031 vty_out(vty
, "blackhole %2d ",
3034 case NEXTHOP_TYPE_IPV6
:
3035 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3036 vty_out(vty
, "V6 Address Hidden %2d ",
3041 /* Route which exist in kernel routing table. */
3042 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
) &&
3043 (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3044 vty_out(vty
, "%-15pI4 ", &rinfo
->from
);
3045 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3046 (route_tag_t
)rinfo
->tag
);
3047 rip_vty_out_uptime(vty
, rinfo
);
3048 } else if (rinfo
->metric
== RIP_METRIC_INFINITY
) {
3049 vty_out(vty
, "self ");
3050 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3051 (route_tag_t
)rinfo
->tag
);
3052 rip_vty_out_uptime(vty
, rinfo
);
3054 if (rinfo
->external_metric
) {
3056 vty
, "self (%s:%d)",
3057 zebra_route_string(rinfo
->type
),
3058 rinfo
->external_metric
);
3061 vty_out(vty
, "%*s", len
, " ");
3063 vty_out(vty
, "self ");
3064 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3065 (route_tag_t
)rinfo
->tag
);
3074 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3075 DEFUN (show_ip_rip_status
,
3076 show_ip_rip_status_cmd
,
3077 "show ip rip [vrf NAME] status",
3082 "IP routing protocol process parameters and statistics\n")
3085 struct interface
*ifp
;
3086 struct rip_interface
*ri
;
3087 extern const struct message ri_version_msg
[];
3088 const char *send_version
;
3089 const char *receive_version
;
3090 const char *vrf_name
;
3093 if (argv_find(argv
, argc
, "vrf", &idx
))
3094 vrf_name
= argv
[idx
+ 1]->arg
;
3096 vrf_name
= VRF_DEFAULT_NAME
;
3098 rip
= rip_lookup_by_vrf_name(vrf_name
);
3100 vty_out(vty
, "%% RIP instance not found\n");
3103 if (!rip
->enabled
) {
3104 vty_out(vty
, "%% RIP instance is disabled\n");
3108 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3109 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3111 vty_out(vty
, " next due in %lu seconds\n",
3112 event_timer_remain_second(rip
->t_update
));
3113 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3114 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3116 /* Filtering status show. */
3117 config_show_distribute(vty
, rip
->distribute_ctx
);
3119 /* Default metric information. */
3120 vty_out(vty
, " Default redistribution metric is %u\n",
3121 rip
->default_metric
);
3123 /* Redistribute information. */
3124 vty_out(vty
, " Redistributing:");
3125 rip_show_redistribute_config(vty
, rip
);
3128 vty_out(vty
, " Default version control: send version %s,",
3129 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3130 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3131 vty_out(vty
, " receive any version \n");
3133 vty_out(vty
, " receive version %s \n",
3134 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3136 vty_out(vty
, " Interface Send Recv Key-chain\n");
3138 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3144 if (ri
->enable_network
|| ri
->enable_interface
) {
3145 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3147 lookup_msg(ri_version_msg
,
3148 rip
->version_send
, NULL
);
3150 send_version
= lookup_msg(ri_version_msg
,
3153 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3155 lookup_msg(ri_version_msg
,
3156 rip
->version_recv
, NULL
);
3158 receive_version
= lookup_msg(
3159 ri_version_msg
, ri
->ri_receive
, NULL
);
3161 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3162 send_version
, receive_version
,
3163 ri
->key_chain
? ri
->key_chain
: "");
3167 vty_out(vty
, " Routing for Networks:\n");
3168 rip_show_network_config(vty
, rip
);
3170 int found_passive
= 0;
3171 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3174 if ((ri
->enable_network
|| ri
->enable_interface
) &&
3176 if (!found_passive
) {
3177 vty_out(vty
, " Passive Interface(s):\n");
3180 vty_out(vty
, " %s\n", ifp
->name
);
3184 vty_out(vty
, " Routing Information Sources:\n");
3186 " Gateway BadPackets BadRoutes Distance Last Update\n");
3187 rip_peer_display(vty
, rip
);
3189 rip_distance_show(vty
, rip
);
3194 /* RIP configuration write function. */
3195 static int config_write_rip(struct vty
*vty
)
3200 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3201 char xpath
[XPATH_MAXLEN
];
3202 struct lyd_node
*dnode
;
3204 snprintf(xpath
, sizeof(xpath
),
3205 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3207 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3210 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3212 /* Distribute configuration. */
3213 config_write_distribute(vty
, rip
->distribute_ctx
);
3215 vty_out(vty
, "exit\n");
3223 static int config_write_rip(struct vty
*vty
);
3224 /* RIP node structure. */
3225 static struct cmd_node rip_node
= {
3228 .parent_node
= CONFIG_NODE
,
3229 .prompt
= "%s(config-router)# ",
3230 .config_write
= config_write_rip
,
3233 /* Distribute-list update functions. */
3234 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3235 struct distribute
*dist
)
3237 struct interface
*ifp
;
3238 struct rip_interface
*ri
;
3239 struct access_list
*alist
;
3240 struct prefix_list
*plist
;
3242 if (!ctx
->vrf
|| !dist
->ifname
)
3245 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3251 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3252 alist
= access_list_lookup(AFI_IP
,
3253 dist
->list
[DISTRIBUTE_V4_IN
]);
3255 ri
->list
[RIP_FILTER_IN
] = alist
;
3257 ri
->list
[RIP_FILTER_IN
] = NULL
;
3259 ri
->list
[RIP_FILTER_IN
] = NULL
;
3261 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3262 alist
= access_list_lookup(AFI_IP
,
3263 dist
->list
[DISTRIBUTE_V4_OUT
]);
3265 ri
->list
[RIP_FILTER_OUT
] = alist
;
3267 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3269 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3271 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3272 plist
= prefix_list_lookup(AFI_IP
,
3273 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3275 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3277 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3279 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3281 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3282 plist
= prefix_list_lookup(AFI_IP
,
3283 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3285 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3287 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3289 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3292 void rip_distribute_update_interface(struct interface
*ifp
)
3294 struct rip_interface
*ri
= ifp
->info
;
3295 struct rip
*rip
= ri
->rip
;
3296 struct distribute
*dist
;
3300 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3302 rip_distribute_update(rip
->distribute_ctx
, dist
);
3305 /* Update all interface's distribute list. */
3307 static void rip_distribute_update_all(struct prefix_list
*notused
)
3309 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3310 struct interface
*ifp
;
3312 FOR_ALL_INTERFACES (vrf
, ifp
)
3313 rip_distribute_update_interface(ifp
);
3316 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3318 rip_distribute_update_all(NULL
);
3321 /* Delete all added rip route. */
3322 void rip_clean(struct rip
*rip
)
3324 rip_interfaces_clean(rip
);
3327 rip_instance_disable(rip
);
3329 stream_free(rip
->obuf
);
3331 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3332 if (rip
->redist
[i
].route_map
.name
)
3333 free(rip
->redist
[i
].route_map
.name
);
3335 route_table_finish(rip
->table
);
3336 route_table_finish(rip
->neighbor
);
3337 list_delete(&rip
->peer_list
);
3338 distribute_list_delete(&rip
->distribute_ctx
);
3339 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3341 rip_clean_network(rip
);
3342 rip_passive_nondefault_clean(rip
);
3343 vector_free(rip
->enable_interface
);
3344 route_table_finish(rip
->enable_network
);
3345 vector_free(rip
->passive_nondefault
);
3346 list_delete(&rip
->offset_list_master
);
3347 route_table_finish(rip
->distance_table
);
3349 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3350 XFREE(MTYPE_RIP_BFD_PROFILE
, rip
->default_bfd_profile
);
3351 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3352 XFREE(MTYPE_RIP
, rip
);
3355 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3356 struct if_rmap
*if_rmap
)
3358 struct interface
*ifp
= NULL
;
3359 struct rip_interface
*ri
;
3360 struct route_map
*rmap
;
3361 struct vrf
*vrf
= NULL
;
3364 vrf
= vrf_lookup_by_name(ctx
->name
);
3366 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3371 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3372 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3374 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3376 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3378 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3380 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3381 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3383 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3385 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3387 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3390 void rip_if_rmap_update_interface(struct interface
*ifp
)
3392 struct rip_interface
*ri
= ifp
->info
;
3393 struct rip
*rip
= ri
->rip
;
3394 struct if_rmap
*if_rmap
;
3395 struct if_rmap_ctx
*ctx
;
3399 ctx
= rip
->if_rmap_ctx
;
3402 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3404 rip_if_rmap_update(ctx
, if_rmap
);
3407 static void rip_routemap_update_redistribute(struct rip
*rip
)
3409 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3410 if (rip
->redist
[i
].route_map
.name
) {
3411 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3412 rip
->redist
[i
].route_map
.name
);
3413 route_map_counter_increment(
3414 rip
->redist
[i
].route_map
.map
);
3420 static void rip_routemap_update(const char *notused
)
3422 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3424 struct interface
*ifp
;
3426 FOR_ALL_INTERFACES (vrf
, ifp
)
3427 rip_if_rmap_update_interface(ifp
);
3431 rip_routemap_update_redistribute(rip
);
3434 /* Link RIP instance to VRF. */
3435 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3437 struct interface
*ifp
;
3440 rip
->distribute_ctx
->vrf
= vrf
;
3443 FOR_ALL_INTERFACES (vrf
, ifp
)
3444 rip_interface_sync(ifp
);
3447 /* Unlink RIP instance from VRF. */
3448 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3450 struct interface
*ifp
;
3453 rip
->distribute_ctx
->vrf
= NULL
;
3456 FOR_ALL_INTERFACES (vrf
, ifp
)
3457 rip_interface_sync(ifp
);
3460 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3464 rip_vrf_link(rip
, vrf
);
3465 rip
->enabled
= true;
3467 /* Resend all redistribute requests. */
3468 rip_redistribute_enable(rip
);
3470 /* Create read and timer thread. */
3471 rip_event(rip
, RIP_READ
, rip
->sock
);
3472 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3474 rip_zebra_vrf_register(vrf
);
3477 static void rip_instance_disable(struct rip
*rip
)
3479 struct vrf
*vrf
= rip
->vrf
;
3480 struct route_node
*rp
;
3482 /* Clear RIP routes */
3483 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3484 struct rip_info
*rinfo
;
3486 struct listnode
*listnode
;
3488 if ((list
= rp
->info
) == NULL
)
3491 rinfo
= listgetdata(listhead(list
));
3492 if (rip_route_rte(rinfo
))
3493 rip_zebra_ipv4_delete(rip
, rp
);
3495 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3496 EVENT_OFF(rinfo
->t_timeout
);
3497 EVENT_OFF(rinfo
->t_garbage_collect
);
3498 rip_info_free(rinfo
);
3502 route_unlock_node(rp
);
3505 /* Flush all redistribute requests. */
3506 rip_redistribute_disable(rip
);
3508 /* Cancel RIP related timers. */
3509 EVENT_OFF(rip
->t_update
);
3510 EVENT_OFF(rip
->t_triggered_update
);
3511 EVENT_OFF(rip
->t_triggered_interval
);
3513 /* Cancel read thread. */
3514 EVENT_OFF(rip
->t_read
);
3516 /* Close RIP socket. */
3520 /* Clear existing peers. */
3521 list_delete_all_node(rip
->peer_list
);
3523 rip_zebra_vrf_deregister(vrf
);
3525 rip_vrf_unlink(rip
, vrf
);
3526 rip
->enabled
= false;
3529 static int rip_vrf_new(struct vrf
*vrf
)
3531 if (IS_RIP_DEBUG_EVENT
)
3532 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3538 static int rip_vrf_delete(struct vrf
*vrf
)
3542 if (IS_RIP_DEBUG_EVENT
)
3543 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3546 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3555 static int rip_vrf_enable(struct vrf
*vrf
)
3560 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3561 if (!rip
|| rip
->enabled
)
3564 if (IS_RIP_DEBUG_EVENT
)
3565 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3568 /* Activate the VRF RIP instance. */
3569 if (!rip
->enabled
) {
3570 socket
= rip_create_socket(vrf
);
3574 rip_instance_enable(rip
, vrf
, socket
);
3580 static int rip_vrf_disable(struct vrf
*vrf
)
3584 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3585 if (!rip
|| !rip
->enabled
)
3588 if (IS_RIP_DEBUG_EVENT
)
3589 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3592 /* Deactivate the VRF RIP instance. */
3594 rip_instance_disable(rip
);
3599 void rip_vrf_init(void)
3601 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
);
3606 void rip_vrf_terminate(void)
3611 /* Allocate new rip structure and set default value. */
3614 /* Install top nodes. */
3615 install_node(&rip_node
);
3617 /* Install rip commands. */
3618 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3619 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3621 install_default(RIP_NODE
);
3623 /* Debug related init. */
3626 /* Access list install. */
3628 access_list_add_hook(rip_distribute_update_all_wrapper
);
3629 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3631 /* Prefix list initialize.*/
3633 prefix_list_add_hook(rip_distribute_update_all
);
3634 prefix_list_delete_hook(rip_distribute_update_all
);
3637 rip_route_map_init();
3639 route_map_add_hook(rip_routemap_update
);
3640 route_map_delete_hook(rip_routemap_update
);
3642 if_rmap_init(RIP_NODE
);