1 /* RIP version 1 and 2.
2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h"
39 #include "distribute.h"
40 #ifdef CRYPTO_INTERNAL
45 #include "lib_errors.h"
46 #include "northbound_cli.h"
48 #include "lib/printfrr.h"
50 #include "ripd/ripd.h"
51 #include "ripd/rip_nb.h"
52 #include "ripd/rip_debug.h"
53 #include "ripd/rip_errors.h"
54 #include "ripd/rip_interface.h"
56 /* UDP receive buffer size */
57 #define RIP_UDP_RCV_BUF 41600
59 DEFINE_MGROUP(RIPD
, "ripd");
60 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure");
61 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name");
62 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info");
63 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance");
66 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
68 static void rip_triggered_update(struct thread
*);
69 static int rip_update_jitter(unsigned long);
70 static void rip_distance_table_node_cleanup(struct route_table
*table
,
71 struct route_node
*node
);
72 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
73 static void rip_instance_disable(struct rip
*rip
);
75 static void rip_distribute_update(struct distribute_ctx
*ctx
,
76 struct distribute
*dist
);
78 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
79 struct if_rmap
*if_rmap
);
81 /* RIP output routes type. */
82 enum { rip_all_route
, rip_changed_route
};
84 /* RIP command strings. */
85 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
86 {RIP_RESPONSE
, "RESPONSE"},
87 {RIP_TRACEON
, "TRACEON"},
88 {RIP_TRACEOFF
, "TRACEOFF"},
90 {RIP_POLL_ENTRY
, "POLL ENTRY"},
93 /* Generate rb-tree of RIP instances. */
94 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
96 return strcmp(a
->vrf_name
, b
->vrf_name
);
98 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
100 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
102 /* Utility function to set broadcast option to the socket. */
103 static int sockopt_broadcast(int sock
)
108 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
111 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
117 int rip_route_rte(struct rip_info
*rinfo
)
119 return (rinfo
->type
== ZEBRA_ROUTE_RIP
120 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
123 static struct rip_info
*rip_info_new(void)
125 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
128 void rip_info_free(struct rip_info
*rinfo
)
130 XFREE(MTYPE_RIP_INFO
, rinfo
);
133 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
135 return route_table_get_info(rinfo
->rp
->table
);
138 /* RIP route garbage collect timer. */
139 static void rip_garbage_collect(struct thread
*t
)
141 struct rip_info
*rinfo
;
142 struct route_node
*rp
;
144 rinfo
= THREAD_ARG(t
);
146 /* Off timeout timer. */
147 RIP_TIMER_OFF(rinfo
->t_timeout
);
149 /* Get route_node pointer. */
152 /* Unlock route_node. */
153 listnode_delete(rp
->info
, rinfo
);
154 if (list_isempty((struct list
*)rp
->info
)) {
155 list_delete((struct list
**)&rp
->info
);
156 route_unlock_node(rp
);
159 /* Free RIP routing information. */
160 rip_info_free(rinfo
);
163 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
165 /* Add new route to the ECMP list.
166 * RETURN: the new entry added in the list, or NULL if it is not the first
167 * entry and ECMP is not allowed.
169 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
171 struct route_node
*rp
= rinfo_new
->rp
;
172 struct rip_info
*rinfo
= NULL
;
173 struct list
*list
= NULL
;
175 if (rp
->info
== NULL
)
176 rp
->info
= list_new();
177 list
= (struct list
*)rp
->info
;
179 /* If ECMP is not allowed and some entry already exists in the list,
181 if (listcount(list
) && !rip
->ecmp
)
184 rinfo
= rip_info_new();
185 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
186 listnode_add(list
, rinfo
);
188 if (rip_route_rte(rinfo
)) {
189 rip_timeout_update(rip
, rinfo
);
190 rip_zebra_ipv4_add(rip
, rp
);
193 /* Set the route change flag on the first entry. */
194 rinfo
= listgetdata(listhead(list
));
195 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
197 /* Signal the output process to trigger an update (see section 2.5). */
198 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
203 /* Replace the ECMP list with the new route.
204 * RETURN: the new entry added in the list
206 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
208 struct route_node
*rp
= rinfo_new
->rp
;
209 struct list
*list
= (struct list
*)rp
->info
;
210 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
211 struct listnode
*node
= NULL
, *nextnode
= NULL
;
213 if (list
== NULL
|| listcount(list
) == 0)
214 return rip_ecmp_add(rip
, rinfo_new
);
216 /* Get the first entry */
217 rinfo
= listgetdata(listhead(list
));
219 /* Learnt route replaced by a local one. Delete it from zebra. */
220 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
221 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
222 rip_zebra_ipv4_delete(rip
, rp
);
224 /* Re-use the first entry, and delete the others. */
225 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
226 if (tmp_rinfo
== rinfo
)
229 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
230 RIP_TIMER_OFF(tmp_rinfo
->t_garbage_collect
);
231 list_delete_node(list
, node
);
232 rip_info_free(tmp_rinfo
);
235 RIP_TIMER_OFF(rinfo
->t_timeout
);
236 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
237 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
239 if (rip_route_rte(rinfo
)) {
240 rip_timeout_update(rip
, rinfo
);
241 /* The ADD message implies an update. */
242 rip_zebra_ipv4_add(rip
, rp
);
245 /* Set the route change flag. */
246 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
248 /* Signal the output process to trigger an update (see section 2.5). */
249 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
254 /* Delete one route from the ECMP list.
256 * null - the entry is freed, and other entries exist in the list
257 * the entry - the entry is the last one in the list; its metric is set
258 * to INFINITY, and the garbage collector is started for it
260 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
262 struct route_node
*rp
= rinfo
->rp
;
263 struct list
*list
= (struct list
*)rp
->info
;
265 RIP_TIMER_OFF(rinfo
->t_timeout
);
267 if (listcount(list
) > 1) {
268 /* Some other ECMP entries still exist. Just delete this entry.
270 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
271 listnode_delete(list
, rinfo
);
272 if (rip_route_rte(rinfo
)
273 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
274 /* The ADD message implies the update. */
275 rip_zebra_ipv4_add(rip
, rp
);
276 rip_info_free(rinfo
);
279 assert(rinfo
== listgetdata(listhead(list
)));
281 /* This is the only entry left in the list. We must keep it in
282 * the list for garbage collection time, with INFINITY metric.
285 rinfo
->metric
= RIP_METRIC_INFINITY
;
286 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
289 if (rip_route_rte(rinfo
)
290 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
291 rip_zebra_ipv4_delete(rip
, rp
);
294 /* Set the route change flag on the first entry. */
295 rinfo
= listgetdata(listhead(list
));
296 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
298 /* Signal the output process to trigger an update (see section 2.5). */
299 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
304 /* Timeout RIP routes. */
305 static void rip_timeout(struct thread
*t
)
307 struct rip_info
*rinfo
= THREAD_ARG(t
);
308 struct rip
*rip
= rip_info_get_instance(rinfo
);
310 rip_ecmp_delete(rip
, rinfo
);
313 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
315 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
316 RIP_TIMER_OFF(rinfo
->t_timeout
);
317 thread_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
322 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
323 struct rip_interface
*ri
)
325 struct distribute
*dist
;
326 struct access_list
*alist
;
327 struct prefix_list
*plist
;
328 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
330 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
332 /* Input distribute-list filtering. */
333 if (ri
->list
[rip_distribute
] &&
334 access_list_apply(ri
->list
[rip_distribute
], (struct prefix
*)p
) ==
336 if (IS_RIP_DEBUG_PACKET
)
337 zlog_debug("%pFX filtered by distribute %s", p
, inout
);
341 if (ri
->prefix
[rip_distribute
] &&
342 prefix_list_apply(ri
->prefix
[rip_distribute
], (struct prefix
*)p
) ==
344 if (IS_RIP_DEBUG_PACKET
)
345 zlog_debug("%pFX filtered by prefix-list %s", p
, inout
);
349 /* All interface filter check. */
350 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
354 if (dist
->list
[distribute
]) {
355 alist
= access_list_lookup(AFI_IP
, dist
->list
[distribute
]);
358 if (access_list_apply(alist
, (struct prefix
*)p
) ==
360 if (IS_RIP_DEBUG_PACKET
)
362 "%pFX filtered by distribute %s",
368 if (dist
->prefix
[distribute
]) {
369 plist
= prefix_list_lookup(AFI_IP
, dist
->prefix
[distribute
]);
372 if (prefix_list_apply(plist
, (struct prefix
*)p
) ==
374 if (IS_RIP_DEBUG_PACKET
)
376 "%pFX filtered by prefix-list %s",
386 /* Check nexthop address validity. */
387 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
389 struct interface
*ifp
;
390 struct listnode
*cnode
;
391 struct connected
*ifc
;
394 /* If nexthop address matches local configured address then it is
397 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
398 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
401 if (p
->family
== AF_INET
402 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
409 /* RIP add route to routing table. */
410 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
411 struct interface
*ifp
)
415 struct prefix_ipv4 p
;
416 struct route_node
*rp
;
417 struct rip_info
*rinfo
= NULL
, newinfo
;
418 struct rip_interface
*ri
;
419 struct in_addr
*nexthop
;
421 unsigned char old_dist
, new_dist
;
422 struct list
*list
= NULL
;
423 struct listnode
*node
= NULL
;
425 /* Make prefix structure. */
426 memset(&p
, 0, sizeof(struct prefix_ipv4
));
428 p
.prefix
= rte
->prefix
;
429 p
.prefixlen
= ip_masklen(rte
->mask
);
431 /* Make sure mask is applied. */
437 /* Apply input filters. */
438 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
442 memset(&newinfo
, 0, sizeof(newinfo
));
443 newinfo
.type
= ZEBRA_ROUTE_RIP
;
444 newinfo
.sub_type
= RIP_ROUTE_RTE
;
445 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
446 newinfo
.from
= from
->sin_addr
;
447 newinfo
.nh
.ifindex
= ifp
->ifindex
;
448 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
449 newinfo
.metric
= rte
->metric
;
450 newinfo
.metric_out
= rte
->metric
; /* XXX */
451 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
453 /* Modify entry according to the interface routemap. */
454 if (ri
->routemap
[RIP_FILTER_IN
]) {
455 /* The object should be of the type of rip_info */
456 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
457 (struct prefix
*)&p
, &newinfo
);
459 if (ret
== RMAP_DENYMATCH
) {
460 if (IS_RIP_DEBUG_PACKET
)
462 "RIP %pFX is filtered by route-map in",
467 /* Get back the object */
468 rte
->nexthop
= newinfo
.nexthop_out
;
469 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
470 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
474 /* Once the entry has been validated, update the metric by
475 adding the cost of the network on which the message
476 arrived. If the result is greater than infinity, use infinity
477 (RFC2453 Sec. 3.9.2) */
478 /* Zebra ripd can handle offset-list in. */
479 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
481 /* If offset-list does not modify the metric use interface's
484 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
486 if (rte
->metric
> RIP_METRIC_INFINITY
)
487 rte
->metric
= RIP_METRIC_INFINITY
;
489 /* Set nexthop pointer. */
490 if (rte
->nexthop
.s_addr
== INADDR_ANY
)
491 nexthop
= &from
->sin_addr
;
493 nexthop
= &rte
->nexthop
;
495 /* Check if nexthop address is myself, then do nothing. */
496 if (rip_nexthop_check(rip
, nexthop
) < 0) {
497 if (IS_RIP_DEBUG_PACKET
)
498 zlog_debug("Nexthop address %pI4 is myself",
503 /* Get index for the prefix. */
504 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
507 newinfo
.nh
.gate
.ipv4
= *nexthop
;
508 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
509 newinfo
.metric
= rte
->metric
;
510 newinfo
.tag
= ntohs(rte
->tag
);
511 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
513 new_dist
= newinfo
.distance
? newinfo
.distance
514 : ZEBRA_RIP_DISTANCE_DEFAULT
;
516 /* Check to see whether there is already RIP route on the table. */
517 if ((list
= rp
->info
) != NULL
)
518 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
519 /* Need to compare with redistributed entry or local
521 if (!rip_route_rte(rinfo
))
524 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
525 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
528 if (listnextnode(node
))
531 /* Not found in the list */
533 if (rte
->metric
> rinfo
->metric
) {
534 /* New route has a greater metric.
536 route_unlock_node(rp
);
540 if (rte
->metric
< rinfo
->metric
)
541 /* New route has a smaller metric.
542 * Replace the ECMP list
543 * with the new one in below. */
546 /* Metrics are same. We compare the distances.
548 old_dist
= rinfo
->distance
? rinfo
->distance
549 : ZEBRA_RIP_DISTANCE_DEFAULT
;
551 if (new_dist
> old_dist
) {
552 /* New route has a greater distance.
554 route_unlock_node(rp
);
558 if (new_dist
< old_dist
)
559 /* New route has a smaller distance.
560 * Replace the ECMP list
561 * with the new one in below. */
564 /* Metrics and distances are both same. Keep
566 * the new route is added in the ECMP list in
571 /* Local static route. */
572 if (rinfo
->type
== ZEBRA_ROUTE_RIP
573 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
574 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
575 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
576 route_unlock_node(rp
);
580 /* Redistributed route check. */
581 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
582 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
583 old_dist
= rinfo
->distance
;
584 /* Only routes directly connected to an interface
586 * may have a valid NULL distance */
587 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
590 : ZEBRA_RIP_DISTANCE_DEFAULT
;
591 /* If imported route does not have STRICT precedence,
592 mark it as a ghost */
593 if (new_dist
<= old_dist
594 && rte
->metric
!= RIP_METRIC_INFINITY
)
595 rip_ecmp_replace(rip
, &newinfo
);
597 route_unlock_node(rp
);
604 route_unlock_node(rp
);
606 /* Now, check to see whether there is already an explicit route
607 for the destination prefix. If there is no such route, add
608 this route to the routing table, unless the metric is
609 infinity (there is no point in adding a route which
611 if (rte
->metric
!= RIP_METRIC_INFINITY
)
612 rip_ecmp_add(rip
, &newinfo
);
614 /* Route is there but we are not sure the route is RIP or not.
617 /* If there is an existing route, compare the next hop address
618 to the address of the router from which the datagram came.
619 If this datagram is from the same router as the existing
620 route, reinitialize the timeout. */
621 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
622 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
624 old_dist
= rinfo
->distance
? rinfo
->distance
625 : ZEBRA_RIP_DISTANCE_DEFAULT
;
627 /* Next, compare the metrics. If the datagram is from the same
628 router as the existing route, and the new metric is different
629 than the old one; or, if the new metric is lower than the old
630 one, or if the tag has been changed; or if there is a route
631 with a lower administrave distance; or an update of the
632 distance on the actual route; do the following actions: */
633 if ((same
&& rinfo
->metric
!= rte
->metric
)
634 || (rte
->metric
< rinfo
->metric
)
635 || ((same
) && (rinfo
->metric
== rte
->metric
)
636 && (newinfo
.tag
!= rinfo
->tag
))
637 || (old_dist
> new_dist
)
638 || ((old_dist
!= new_dist
) && same
)) {
639 if (listcount(list
) == 1) {
640 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
641 rip_ecmp_replace(rip
, &newinfo
);
643 rip_ecmp_delete(rip
, rinfo
);
645 if (newinfo
.metric
< rinfo
->metric
)
646 rip_ecmp_replace(rip
, &newinfo
);
647 else if (newinfo
.metric
> rinfo
->metric
)
648 rip_ecmp_delete(rip
, rinfo
);
649 else if (new_dist
< old_dist
)
650 rip_ecmp_replace(rip
, &newinfo
);
651 else if (new_dist
> old_dist
)
652 rip_ecmp_delete(rip
, rinfo
);
654 int update
= CHECK_FLAG(rinfo
->flags
,
659 assert(newinfo
.metric
660 != RIP_METRIC_INFINITY
);
662 RIP_TIMER_OFF(rinfo
->t_timeout
);
663 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
664 memcpy(rinfo
, &newinfo
,
665 sizeof(struct rip_info
));
666 rip_timeout_update(rip
, rinfo
);
669 rip_zebra_ipv4_add(rip
, rp
);
671 /* - Set the route change flag on the
673 rinfo
= listgetdata(listhead(list
));
674 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
675 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
678 } else /* same & no change */
679 rip_timeout_update(rip
, rinfo
);
681 /* Unlock tempolary lock of the route. */
682 route_unlock_node(rp
);
686 /* Dump RIP packet */
687 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
692 const char *command_str
;
696 /* Set command string. */
697 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
698 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
700 command_str
= "unknown";
702 /* Dump packet header. */
703 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
704 packet
->version
, size
);
706 /* Dump each routing table entry. */
709 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
710 if (packet
->version
== RIPv2
) {
711 netmask
= ip_masklen(rte
->mask
);
713 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
715 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
716 p
= (uint8_t *)&rte
->prefix
;
719 " family 0x%X type %d auth string: %s",
722 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
723 struct rip_md5_info
*md5
;
725 md5
= (struct rip_md5_info
*)&packet
729 " family 0x%X type %d (MD5 authentication)",
733 " RIP-2 packet len %d Key ID %d Auth Data len %d",
734 ntohs(md5
->packet_len
),
735 md5
->keyid
, md5
->auth_len
);
736 zlog_debug(" Sequence Number %ld",
737 (unsigned long)ntohl(
739 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
740 p
= (uint8_t *)&rte
->prefix
;
743 " family 0x%X type %d (MD5 data)",
747 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
748 p
[0], p
[1], p
[2], p
[3], p
[4],
749 p
[5], p
[6], p
[7], p
[8], p
[9],
750 p
[10], p
[11], p
[12], p
[13],
754 " family 0x%X type %d (Unknown auth type)",
760 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
762 &rte
->prefix
, netmask
, &rte
->nexthop
,
764 (route_tag_t
)ntohs(rte
->tag
),
765 (unsigned long)ntohl(rte
->metric
));
767 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
769 &rte
->prefix
, ntohs(rte
->family
),
770 (route_tag_t
)ntohs(rte
->tag
),
771 (unsigned long)ntohl(rte
->metric
));
776 /* Check if the destination address is valid (unicast; not net 0
777 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
778 check net 0 because we accept default route. */
779 static int rip_destination_check(struct in_addr addr
)
781 uint32_t destination
;
783 /* Convert to host byte order. */
784 destination
= ntohl(addr
.s_addr
);
786 if (IPV4_NET127(destination
))
789 /* Net 0 may match to the default route. */
790 if (IPV4_NET0(destination
) && destination
!= 0)
793 /* Unicast address must belong to class A, B, C. */
794 if (IN_CLASSA(destination
))
796 if (IN_CLASSB(destination
))
798 if (IN_CLASSC(destination
))
804 /* RIP version 2 authentication. */
805 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
806 struct interface
*ifp
)
808 struct rip_interface
*ri
;
809 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
812 /* reject passwords with zeros in the middle of the string */
813 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
814 if (auth_str
[i
] != '\0')
818 if (IS_RIP_DEBUG_EVENT
)
819 zlog_debug("RIPv2 simple password authentication from %pI4",
824 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
825 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
828 /* Simple password authentication. */
830 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
834 struct keychain
*keychain
;
837 keychain
= keychain_lookup(ri
->key_chain
);
838 if (keychain
== NULL
|| keychain
->key
== NULL
)
841 key
= key_match_for_accept(keychain
, auth_str
);
848 /* RIP version 2 authentication with MD5. */
849 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
850 int length
, struct interface
*ifp
)
852 struct rip_interface
*ri
;
853 struct rip_md5_info
*md5
;
854 struct rip_md5_data
*md5data
;
855 struct keychain
*keychain
;
857 #ifdef CRYPTO_OPENSSL
859 #elif CRYPTO_INTERNAL
862 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
864 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
866 if (IS_RIP_DEBUG_EVENT
)
867 zlog_debug("RIPv2 MD5 authentication from %pI4",
871 md5
= (struct rip_md5_info
*)&packet
->rte
;
873 /* Check auth type. */
874 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
877 /* If the authentication length is less than 16, then it must be wrong
879 * any interpretation of rfc2082. Some implementations also interpret
880 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
881 * RIP_AUTH_MD5_COMPAT_SIZE.
883 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
884 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
885 if (IS_RIP_DEBUG_EVENT
)
887 "RIPv2 MD5 authentication, strange authentication length field %d",
892 /* grab and verify check packet length */
893 packet_len
= ntohs(md5
->packet_len
);
895 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
896 if (IS_RIP_DEBUG_EVENT
)
898 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
899 md5
->packet_len
, length
);
903 /* retrieve authentication data */
904 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
907 keychain
= keychain_lookup(ri
->key_chain
);
908 if (keychain
== NULL
)
911 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
912 if (key
== NULL
|| key
->string
== NULL
)
915 memcpy(auth_str
, key
->string
,
916 MIN(sizeof(auth_str
), strlen(key
->string
)));
917 } else if (ri
->auth_str
)
918 memcpy(auth_str
, ri
->auth_str
,
919 MIN(sizeof(auth_str
), strlen(ri
->auth_str
)));
921 if (auth_str
[0] == 0)
924 /* MD5 digest authentication. */
925 #ifdef CRYPTO_OPENSSL
926 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
927 ctx
= EVP_MD_CTX_new();
928 EVP_DigestInit(ctx
, EVP_md5());
929 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
930 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
931 EVP_DigestFinal(ctx
, digest
, &md5_size
);
932 EVP_MD_CTX_free(ctx
);
933 #elif CRYPTO_INTERNAL
934 memset(&ctx
, 0, sizeof(ctx
));
936 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
937 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
938 MD5Final(digest
, &ctx
);
941 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
947 /* Pick correct auth string for sends, prepare auth_str buffer for use.
948 * (left justified and padded).
950 * presumes one of ri or key is valid, and that the auth strings they point
951 * to are nul terminated. If neither are present, auth_str will be fully
955 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
956 char *auth_str
, int len
)
960 memset(auth_str
, 0, len
);
961 if (key
&& key
->string
)
962 memcpy(auth_str
, key
->string
,
963 MIN((size_t)len
, strlen(key
->string
)));
964 else if (ri
->auth_str
)
965 memcpy(auth_str
, ri
->auth_str
,
966 MIN((size_t)len
, strlen(ri
->auth_str
)));
971 /* Write RIPv2 simple password authentication information
973 * auth_str is presumed to be 2 bytes and correctly prepared
974 * (left justified and zero padded).
976 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
978 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
980 stream_putw(s
, RIP_FAMILY_AUTH
);
981 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
982 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
987 /* write RIPv2 MD5 "authentication header"
988 * (uses the auth key data field)
990 * Digest offset field is set to 0.
992 * returns: offset of the digest offset field, which must be set when
993 * length to the auth-data MD5 digest is known.
995 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1000 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1002 /* MD5 authentication. */
1003 stream_putw(s
, RIP_FAMILY_AUTH
);
1004 stream_putw(s
, RIP_AUTH_MD5
);
1006 /* MD5 AH digest offset field.
1008 * Set to placeholder value here, to true value when RIP-2 Packet length
1009 * is known. Actual value is set in .....().
1011 doff
= stream_get_endp(s
);
1016 stream_putc(s
, key
->index
% 256);
1020 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1021 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1023 * to be configurable.
1025 stream_putc(s
, ri
->md5_auth_len
);
1027 /* Sequence Number (non-decreasing). */
1028 /* RFC2080: The value used in the sequence number is
1029 arbitrary, but two suggestions are the time of the
1030 message's creation or a simple message counter. */
1031 stream_putl(s
, time(NULL
));
1033 /* Reserved field must be zero. */
1040 /* If authentication is in used, write the appropriate header
1041 * returns stream offset to which length must later be written
1042 * or 0 if this is not required
1044 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1045 struct key
*key
, char *auth_str
, int len
)
1047 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1049 switch (ri
->auth_type
) {
1050 case RIP_AUTH_SIMPLE_PASSWORD
:
1051 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1052 rip_auth_simple_write(s
, auth_str
, len
);
1055 return rip_auth_md5_ah_write(s
, ri
, key
);
1061 /* Write RIPv2 MD5 authentication data trailer */
1062 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1063 size_t doff
, char *auth_str
, int authlen
)
1066 #ifdef CRYPTO_OPENSSL
1068 #elif CRYPTO_INTERNAL
1071 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1073 /* Make it sure this interface is configured as MD5
1075 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1076 && (authlen
== RIP_AUTH_MD5_SIZE
));
1079 /* Get packet length. */
1080 len
= stream_get_endp(s
);
1082 /* Check packet length. */
1083 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1086 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1091 /* Set the digest offset length in the header */
1092 stream_putw_at(s
, doff
, len
);
1094 /* Set authentication data. */
1095 stream_putw(s
, RIP_FAMILY_AUTH
);
1096 stream_putw(s
, RIP_AUTH_DATA
);
1098 /* Generate a digest for the RIP packet. */
1099 #ifdef CRYPTO_OPENSSL
1100 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1101 ctx
= EVP_MD_CTX_new();
1102 EVP_DigestInit(ctx
, EVP_md5());
1103 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1104 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1105 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1106 EVP_MD_CTX_free(ctx
);
1107 #elif CRYPTO_INTERNAL
1108 memset(&ctx
, 0, sizeof(ctx
));
1110 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1111 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1112 MD5Final(digest
, &ctx
);
1115 /* Copy the digest to the packet. */
1116 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1119 /* RIP routing information. */
1120 static void rip_response_process(struct rip_packet
*packet
, int size
,
1121 struct sockaddr_in
*from
,
1122 struct connected
*ifc
)
1124 struct rip_interface
*ri
= ifc
->ifp
->info
;
1125 struct rip
*rip
= ri
->rip
;
1128 struct prefix_ipv4 ifaddr
;
1129 struct prefix_ipv4 ifaddrclass
;
1132 memset(&ifaddr
, 0, sizeof(ifaddr
));
1133 /* We don't know yet. */
1136 /* The Response must be ignored if it is not from the RIP
1137 port. (RFC2453 - Sec. 3.9.2)*/
1138 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1139 zlog_info("response doesn't come from RIP port: %d",
1141 rip_peer_bad_packet(rip
, from
);
1145 /* The datagram's IPv4 source address should be checked to see
1146 whether the datagram is from a valid neighbor; the source of the
1147 datagram must be on a directly connected network (RFC2453 - Sec.
1149 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1153 "This datagram doesn't come from a valid neighbor: %pI4",
1155 rip_peer_bad_packet(rip
, from
);
1159 /* It is also worth checking to see whether the response is from one
1160 of the router's own addresses. */
1162 ; /* Alredy done in rip_read () */
1164 /* Update RIP peer. */
1165 rip_peer_update(rip
, from
, packet
->version
);
1167 /* Set RTE pointer. */
1170 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1171 /* RIPv2 authentication check. */
1172 /* If the Address Family Identifier of the first (and only the
1173 first) entry in the message is 0xFFFF, then the remainder of
1174 the entry contains the authentication. */
1175 /* If the packet gets here it means authentication enabled */
1176 /* Check is done in rip_read(). So, just skipping it */
1177 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1178 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1181 if (rte
->family
!= htons(AF_INET
)) {
1182 /* Address family check. RIP only supports AF_INET. */
1183 zlog_info("Unsupported family %d from %pI4",
1189 /* - is the destination address valid (e.g., unicast; not net 0
1191 if (!rip_destination_check(rte
->prefix
)) {
1193 "Network is net 0 or net 127 or it is not unicast network");
1194 rip_peer_bad_route(rip
, from
);
1198 /* Convert metric value to host byte order. */
1199 rte
->metric
= ntohl(rte
->metric
);
1201 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1202 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1203 zlog_info("Route's metric is not in the 1-16 range.");
1204 rip_peer_bad_route(rip
, from
);
1208 /* RIPv1 does not have nexthop value. */
1209 if (packet
->version
== RIPv1
1210 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1211 zlog_info("RIPv1 packet with nexthop value %pI4",
1213 rip_peer_bad_route(rip
, from
);
1217 /* That is, if the provided information is ignored, a possibly
1218 sub-optimal, but absolutely valid, route may be taken. If
1219 the received Next Hop is not directly reachable, it should be
1220 treated as 0.0.0.0. */
1221 if (packet
->version
== RIPv2
1222 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1225 /* Multicast address check. */
1226 addrval
= ntohl(rte
->nexthop
.s_addr
);
1227 if (IN_CLASSD(addrval
)) {
1229 "Nexthop %pI4 is multicast address, skip this rte",
1234 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1235 rip
->vrf
->vrf_id
)) {
1236 struct route_node
*rn
;
1237 struct rip_info
*rinfo
;
1239 rn
= route_node_match_ipv4(rip
->table
,
1245 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1248 if (IS_RIP_DEBUG_EVENT
)
1250 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1252 rte
->nexthop
= rinfo
->from
;
1254 if (IS_RIP_DEBUG_EVENT
)
1256 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1258 rte
->nexthop
.s_addr
=
1262 route_unlock_node(rn
);
1264 if (IS_RIP_DEBUG_EVENT
)
1266 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1268 rte
->nexthop
.s_addr
= INADDR_ANY
;
1273 /* For RIPv1, there won't be a valid netmask.
1274 * This is a best guess at the masks. If everyone was using old
1275 * Ciscos before the 'ip subnet zero' option, it would be almost
1278 * Cisco summarize ripv1 advertisements to the classful boundary
1279 * (/16 for class B's) except when the RIP packet does to inside
1280 * the classful network in question.
1282 if ((packet
->version
== RIPv1
1283 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1284 || (packet
->version
== RIPv2
1285 && (rte
->prefix
.s_addr
!= INADDR_ANY
1286 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1287 uint32_t destination
;
1289 if (subnetted
== -1) {
1290 memcpy(&ifaddr
, ifc
->address
, sizeof(ifaddr
));
1291 memcpy(&ifaddrclass
, &ifaddr
,
1292 sizeof(ifaddrclass
));
1293 apply_classful_mask_ipv4(&ifaddrclass
);
1295 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1299 destination
= ntohl(rte
->prefix
.s_addr
);
1301 if (IN_CLASSA(destination
))
1302 masklen2ip(8, &rte
->mask
);
1303 else if (IN_CLASSB(destination
))
1304 masklen2ip(16, &rte
->mask
);
1305 else if (IN_CLASSC(destination
))
1306 masklen2ip(24, &rte
->mask
);
1309 masklen2ip(ifaddrclass
.prefixlen
,
1310 (struct in_addr
*)&destination
);
1311 if ((subnetted
== 1)
1312 && ((rte
->prefix
.s_addr
& destination
)
1313 == ifaddrclass
.prefix
.s_addr
)) {
1314 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1315 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1316 != rte
->prefix
.s_addr
)
1317 masklen2ip(32, &rte
->mask
);
1318 if (IS_RIP_DEBUG_EVENT
)
1319 zlog_debug("Subnetted route %pI4",
1322 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1323 != rte
->prefix
.s_addr
)
1327 if (IS_RIP_DEBUG_EVENT
) {
1328 zlog_debug("Resultant route %pI4",
1330 zlog_debug("Resultant mask %pI4",
1335 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1336 ignore the entry. */
1337 if ((packet
->version
== RIPv2
)
1338 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1339 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1340 != rte
->prefix
.s_addr
)) {
1342 "RIPv2 address %pI4 is not mask /%d applied one",
1343 &rte
->prefix
, ip_masklen(rte
->mask
));
1344 rip_peer_bad_route(rip
, from
);
1348 /* Default route's netmask is ignored. */
1349 if (packet
->version
== RIPv2
1350 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1351 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1352 if (IS_RIP_DEBUG_EVENT
)
1354 "Default route with non-zero netmask. Set zero to netmask");
1355 rte
->mask
.s_addr
= INADDR_ANY
;
1358 /* Routing table updates. */
1359 rip_rte_process(rte
, from
, ifc
->ifp
);
1363 /* Make socket for RIP protocol. */
1364 int rip_create_socket(struct vrf
*vrf
)
1368 struct sockaddr_in addr
;
1369 const char *vrf_dev
= NULL
;
1371 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1372 addr
.sin_family
= AF_INET
;
1373 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1374 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1375 addr
.sin_len
= sizeof(struct sockaddr_in
);
1376 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1377 /* sending port must always be the RIP port */
1378 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1380 /* Make datagram socket. */
1381 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1382 vrf_dev
= vrf
->name
;
1383 frr_with_privs(&ripd_privs
) {
1384 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1387 flog_err_sys(EC_LIB_SOCKET
,
1388 "Cannot create UDP socket: %s",
1389 safe_strerror(errno
));
1394 sockopt_broadcast(sock
);
1395 sockopt_reuseaddr(sock
);
1396 sockopt_reuseport(sock
);
1397 setsockopt_ipv4_multicast_loop(sock
, 0);
1398 #ifdef IPTOS_PREC_INTERNETCONTROL
1399 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1401 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1403 frr_with_privs(&ripd_privs
) {
1404 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1406 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1407 __func__
, sock
, &addr
.sin_addr
,
1408 (int)ntohs(addr
.sin_port
),
1409 safe_strerror(errno
));
1419 /* RIP packet send to destination address, on interface denoted by
1420 * by connected argument. NULL to argument denotes destination should be
1421 * should be RIP multicast group
1423 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1424 struct connected
*ifc
)
1426 struct rip_interface
*ri
;
1429 struct sockaddr_in sin
;
1433 struct cmsghdr
*cmsgptr
;
1434 char adata
[256] = {};
1435 struct in_pktinfo
*pkt
;
1436 #endif /* GNU_LINUX */
1438 assert(ifc
!= NULL
);
1439 ri
= ifc
->ifp
->info
;
1442 if (IS_RIP_DEBUG_PACKET
) {
1443 #define ADDRESS_SIZE 20
1444 char dst
[ADDRESS_SIZE
];
1447 inet_ntop(AF_INET
, &to
->sin_addr
, dst
, sizeof(dst
));
1449 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1450 inet_ntop(AF_INET
, &sin
.sin_addr
, dst
, sizeof(dst
));
1453 zlog_debug("rip_send_packet %pI4 > %s (%s)",
1454 &ifc
->address
->u
.prefix4
, dst
,
1458 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1460 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1461 * configured with multiple addresses on the same
1462 * subnet: the first address on the subnet is configured
1463 * "primary", and all subsequent addresses on that subnet
1464 * are treated as "secondary" addresses. In order to avoid
1465 * routing-table bloat on other rip listeners, we do not send
1466 * out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1467 * XXX Since Linux is the only system for which the
1468 * ZEBRA_IFA_SECONDARY flag is set, we would end up
1469 * sending a packet for a "secondary" source address on
1470 * non-linux systems.
1472 if (IS_RIP_DEBUG_PACKET
)
1473 zlog_debug("duplicate dropped");
1477 /* Make destination address. */
1478 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1479 sin
.sin_family
= AF_INET
;
1480 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1481 sin
.sin_len
= sizeof(struct sockaddr_in
);
1482 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1484 /* When destination is specified, use it's port and address. */
1486 sin
.sin_port
= to
->sin_port
;
1487 sin
.sin_addr
= to
->sin_addr
;
1489 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1490 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1492 rip_interface_multicast_set(rip
->sock
, ifc
);
1495 memset(&msg
, 0, sizeof(msg
));
1496 msg
.msg_name
= (void *)&sin
;
1497 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1504 msg
.msg_control
= (void *)adata
;
1505 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1507 cmsgptr
= (struct cmsghdr
*)adata
;
1508 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1509 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1510 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1511 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1512 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1513 pkt
->ipi_spec_dst
.s_addr
= ifc
->address
->u
.prefix4
.s_addr
;
1514 #endif /* GNU_LINUX */
1516 ret
= sendmsg(rip
->sock
, &msg
, 0);
1518 if (IS_RIP_DEBUG_EVENT
)
1519 zlog_debug("SEND to %pI4%d", &sin
.sin_addr
,
1520 ntohs(sin
.sin_port
));
1523 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1528 /* Add redistributed route to RIP table. */
1529 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1530 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1531 unsigned int metric
, unsigned char distance
,
1535 struct route_node
*rp
= NULL
;
1536 struct rip_info
*rinfo
= NULL
, newinfo
;
1537 struct list
*list
= NULL
;
1539 /* Redistribute route */
1540 ret
= rip_destination_check(p
->prefix
);
1544 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1546 memset(&newinfo
, 0, sizeof(struct rip_info
));
1547 newinfo
.type
= type
;
1548 newinfo
.sub_type
= sub_type
;
1550 newinfo
.external_metric
= metric
;
1551 newinfo
.distance
= distance
;
1552 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1557 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1558 rinfo
= listgetdata(listhead(list
));
1560 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1561 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1562 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1563 route_unlock_node(rp
);
1567 /* Manually configured RIP route check. */
1568 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1569 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1570 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1571 if (type
!= ZEBRA_ROUTE_RIP
1572 || ((sub_type
!= RIP_ROUTE_STATIC
)
1573 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1574 route_unlock_node(rp
);
1579 (void)rip_ecmp_replace(rip
, &newinfo
);
1580 route_unlock_node(rp
);
1582 (void)rip_ecmp_add(rip
, &newinfo
);
1584 if (IS_RIP_DEBUG_EVENT
) {
1585 zlog_debug("Redistribute new prefix %pFX", p
);
1588 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1591 /* Delete redistributed route from RIP table. */
1592 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1593 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1596 struct route_node
*rp
;
1597 struct rip_info
*rinfo
;
1599 ret
= rip_destination_check(p
->prefix
);
1603 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1605 struct list
*list
= rp
->info
;
1607 if (list
!= NULL
&& listcount(list
) != 0) {
1608 rinfo
= listgetdata(listhead(list
));
1609 if (rinfo
!= NULL
&& rinfo
->type
== type
1610 && rinfo
->sub_type
== sub_type
1611 && rinfo
->nh
.ifindex
== ifindex
) {
1612 /* Perform poisoned reverse. */
1613 rinfo
->metric
= RIP_METRIC_INFINITY
;
1614 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1615 rip_garbage_collect
,
1617 RIP_TIMER_OFF(rinfo
->t_timeout
);
1618 rinfo
->flags
|= RIP_RTF_CHANGED
;
1620 if (IS_RIP_DEBUG_EVENT
)
1622 "Poison %pFX on the interface %s with an infinity metric [delete]",
1628 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1631 route_unlock_node(rp
);
1635 /* Response to request called from rip_read ().*/
1636 static void rip_request_process(struct rip_packet
*packet
, int size
,
1637 struct sockaddr_in
*from
, struct connected
*ifc
)
1642 struct prefix_ipv4 p
;
1643 struct route_node
*rp
;
1644 struct rip_info
*rinfo
;
1645 struct rip_interface
*ri
;
1647 /* Does not reponse to the requests on the loopback interfaces */
1648 if (if_is_loopback(ifc
->ifp
))
1651 /* Check RIP process is enabled on this interface. */
1652 ri
= ifc
->ifp
->info
;
1657 /* When passive interface is specified, suppress responses */
1661 /* RIP peer update. */
1662 rip_peer_update(rip
, from
, packet
->version
);
1664 lim
= ((caddr_t
)packet
) + size
;
1667 /* The Request is processed entry by entry. If there are no
1668 entries, no response is given. */
1669 if (lim
== (caddr_t
)rte
)
1672 /* There is one special case. If there is exactly one entry in the
1673 request, and it has an address family identifier of zero and a
1674 metric of infinity (i.e., 16), then this is a request to send the
1675 entire routing table. */
1676 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1677 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1678 /* All route with split horizon */
1679 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1681 if (ntohs(rte
->family
) != AF_INET
)
1684 /* Examine the list of RTEs in the Request one by one. For each
1685 entry, look up the destination in the router's routing
1686 database and, if there is a route, put that route's metric in
1687 the metric field of the RTE. If there is no explicit route
1688 to the specified destination, put infinity in the metric
1689 field. Once all the entries have been filled in, change the
1690 command from Request to Response and send the datagram back
1691 to the requestor. */
1694 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1695 p
.prefix
= rte
->prefix
;
1696 p
.prefixlen
= ip_masklen(rte
->mask
);
1697 apply_mask_ipv4(&p
);
1699 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1701 rinfo
= listgetdata(
1702 listhead((struct list
*)rp
->info
));
1703 rte
->metric
= htonl(rinfo
->metric
);
1704 route_unlock_node(rp
);
1706 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1708 packet
->command
= RIP_RESPONSE
;
1710 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1712 rip
->counters
.queries
++;
1715 /* First entry point of RIP packet. */
1716 static void rip_read(struct thread
*t
)
1718 struct rip
*rip
= THREAD_ARG(t
);
1722 union rip_buf rip_buf
;
1723 struct rip_packet
*packet
;
1724 struct sockaddr_in from
;
1728 struct interface
*ifp
= NULL
;
1729 struct connected
*ifc
;
1730 struct rip_interface
*ri
;
1733 /* Fetch socket then register myself. */
1734 sock
= THREAD_FD(t
);
1736 /* Add myself to tne next event */
1737 rip_event(rip
, RIP_READ
, sock
);
1739 /* RIPd manages only IPv4. */
1740 memset(&from
, 0, sizeof(struct sockaddr_in
));
1741 fromlen
= sizeof(struct sockaddr_in
);
1743 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1744 (struct sockaddr
*)&from
, &fromlen
);
1746 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1747 safe_strerror(errno
));
1751 /* Check is this packet comming from myself? */
1752 if (if_check_address(rip
, from
.sin_addr
)) {
1753 if (IS_RIP_DEBUG_PACKET
)
1754 zlog_debug("ignore packet comes from myself (VRF %s)",
1759 /* Which interface is this packet comes from. */
1760 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1765 /* RIP packet received */
1766 if (IS_RIP_DEBUG_EVENT
)
1767 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1768 &from
.sin_addr
, ntohs(from
.sin_port
),
1769 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1771 /* If this packet come from unknown interface, ignore it. */
1774 "rip_read: cannot find interface for packet from %pI4 port %d (VRF %s)",
1775 &from
.sin_addr
, ntohs(from
.sin_port
),
1781 p
.u
.prefix4
= from
.sin_addr
;
1782 p
.prefixlen
= IPV4_MAX_BITLEN
;
1784 ifc
= connected_lookup_prefix(ifp
, &p
);
1788 "rip_read: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1789 &from
.sin_addr
, ntohs(from
.sin_port
),
1790 ifp
->name
, rip
->vrf_name
);
1794 /* Packet length check. */
1795 if (len
< RIP_PACKET_MINSIZ
) {
1796 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1798 rip_peer_bad_packet(rip
, &from
);
1801 if (len
> RIP_PACKET_MAXSIZ
) {
1802 zlog_warn("packet size %d is larger than max size %d", len
,
1804 rip_peer_bad_packet(rip
, &from
);
1808 /* Packet alignment check. */
1809 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1810 zlog_warn("packet size %d is wrong for RIP packet alignment",
1812 rip_peer_bad_packet(rip
, &from
);
1816 /* Set RTE number. */
1817 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1819 /* For easy to handle. */
1820 packet
= &rip_buf
.rip_packet
;
1822 /* RIP version check. */
1823 if (packet
->version
== 0) {
1824 zlog_info("version 0 with command %d received.",
1826 rip_peer_bad_packet(rip
, &from
);
1830 /* Dump RIP packet. */
1831 if (IS_RIP_DEBUG_RECV
)
1832 rip_packet_dump(packet
, len
, "RECV");
1834 /* RIP version adjust. This code should rethink now. RFC1058 says
1835 that "Version 1 implementations are to ignore this extra data and
1836 process only the fields specified in this document.". So RIPv3
1837 packet should be treated as RIPv1 ignoring must be zero field. */
1838 if (packet
->version
> RIPv2
)
1839 packet
->version
= RIPv2
;
1841 /* Is RIP running or is this RIP neighbor ?*/
1843 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1844 if (IS_RIP_DEBUG_EVENT
)
1845 zlog_debug("RIP is not enabled on interface %s.",
1847 rip_peer_bad_packet(rip
, &from
);
1851 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1852 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1854 if (vrecv
== RI_RIP_VERSION_NONE
1855 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1856 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1857 if (IS_RIP_DEBUG_PACKET
)
1859 " packet's v%d doesn't fit to if version spec",
1861 rip_peer_bad_packet(rip
, &from
);
1865 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1866 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1867 accepted; authenticated RIP-2 messages shall be discarded. */
1868 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1869 && (packet
->version
== RIPv2
)
1870 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1871 if (IS_RIP_DEBUG_EVENT
)
1873 "packet RIPv%d is dropped because authentication disabled",
1875 ripd_notif_send_auth_type_failure(ifp
->name
);
1876 rip_peer_bad_packet(rip
, &from
);
1881 If the router is configured to authenticate RIP-2 messages, then
1882 RIP-1 messages and RIP-2 messages which pass authentication
1883 testing shall be accepted; unauthenticated and failed
1884 authentication RIP-2 messages shall be discarded. For maximum
1885 security, RIP-1 messages should be ignored when authentication is
1886 in use (see section 4.1); otherwise, the routing information from
1887 authenticated messages will be propagated by RIP-1 routers in an
1888 unauthenticated manner.
1890 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1891 * always reply regardless of authentication settings, because:
1893 * - if there other authorised routers on-link, the REQUESTor can
1894 * passively obtain the routing updates anyway
1895 * - if there are no other authorised routers on-link, RIP can
1896 * easily be disabled for the link to prevent giving out information
1897 * on state of this routers RIP routing table..
1899 * I.e. if RIPv1 has any place anymore these days, it's as a very
1900 * simple way to distribute routing information (e.g. to embedded
1901 * hosts / appliances) and the ability to give out RIPv1
1902 * routing-information freely, while still requiring RIPv2
1903 * authentication for any RESPONSEs might be vaguely useful.
1905 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1906 /* Discard RIPv1 messages other than REQUESTs */
1907 if (packet
->command
!= RIP_REQUEST
) {
1908 if (IS_RIP_DEBUG_PACKET
)
1910 "RIPv1 dropped because authentication enabled");
1911 ripd_notif_send_auth_type_failure(ifp
->name
);
1912 rip_peer_bad_packet(rip
, &from
);
1915 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1916 const char *auth_desc
;
1919 /* There definitely is no authentication in the packet.
1921 if (IS_RIP_DEBUG_PACKET
)
1923 "RIPv2 authentication failed: no auth RTE in packet");
1924 ripd_notif_send_auth_type_failure(ifp
->name
);
1925 rip_peer_bad_packet(rip
, &from
);
1929 /* First RTE must be an Authentication Family RTE */
1930 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1931 if (IS_RIP_DEBUG_PACKET
)
1933 "RIPv2 dropped because authentication enabled");
1934 ripd_notif_send_auth_type_failure(ifp
->name
);
1935 rip_peer_bad_packet(rip
, &from
);
1939 /* Check RIPv2 authentication. */
1940 switch (ntohs(packet
->rte
->tag
)) {
1941 case RIP_AUTH_SIMPLE_PASSWORD
:
1942 auth_desc
= "simple";
1943 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1948 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1949 /* Reset RIP packet length to trim MD5 data. */
1955 auth_desc
= "unknown type";
1956 if (IS_RIP_DEBUG_PACKET
)
1958 "RIPv2 Unknown authentication type %d",
1959 ntohs(packet
->rte
->tag
));
1963 if (IS_RIP_DEBUG_PACKET
)
1964 zlog_debug("RIPv2 %s authentication success",
1967 if (IS_RIP_DEBUG_PACKET
)
1968 zlog_debug("RIPv2 %s authentication failure",
1970 ripd_notif_send_auth_failure(ifp
->name
);
1971 rip_peer_bad_packet(rip
, &from
);
1976 /* Process each command. */
1977 switch (packet
->command
) {
1979 rip_response_process(packet
, len
, &from
, ifc
);
1983 rip_request_process(packet
, len
, &from
, ifc
);
1988 "Obsolete command %s received, please sent it to routed",
1989 lookup_msg(rip_msg
, packet
->command
, NULL
));
1990 rip_peer_bad_packet(rip
, &from
);
1992 case RIP_POLL_ENTRY
:
1993 zlog_info("Obsolete command %s received",
1994 lookup_msg(rip_msg
, packet
->command
, NULL
));
1995 rip_peer_bad_packet(rip
, &from
);
1998 zlog_info("Unknown RIP command %d received", packet
->command
);
1999 rip_peer_bad_packet(rip
, &from
);
2004 /* Write routing table entry to the stream and return next index of
2005 the routing table entry in the stream. */
2006 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2007 uint8_t version
, struct rip_info
*rinfo
)
2009 struct in_addr mask
;
2011 /* Write routing table entry. */
2012 if (version
== RIPv1
) {
2013 stream_putw(s
, AF_INET
);
2015 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2016 stream_put_ipv4(s
, 0);
2017 stream_put_ipv4(s
, 0);
2018 stream_putl(s
, rinfo
->metric_out
);
2020 masklen2ip(p
->prefixlen
, &mask
);
2022 stream_putw(s
, AF_INET
);
2023 stream_putw(s
, rinfo
->tag_out
);
2024 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2025 stream_put_ipv4(s
, mask
.s_addr
);
2026 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2027 stream_putl(s
, rinfo
->metric_out
);
2033 /* Send update to the ifp or spcified neighbor. */
2034 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2035 int route_type
, uint8_t version
)
2040 struct route_node
*rp
;
2041 struct rip_info
*rinfo
;
2042 struct rip_interface
*ri
;
2043 struct prefix_ipv4
*p
;
2044 struct prefix_ipv4 classfull
;
2045 struct prefix_ipv4 ifaddrclass
;
2046 struct key
*key
= NULL
;
2047 /* this might need to made dynamic if RIP ever supported auth methods
2048 with larger key string sizes */
2049 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2050 size_t doff
= 0; /* offset of digest offset field */
2054 struct list
*list
= NULL
;
2055 struct listnode
*listnode
= NULL
;
2057 /* Logging output event. */
2058 if (IS_RIP_DEBUG_EVENT
) {
2060 zlog_debug("update routes to neighbor %pI4",
2063 zlog_debug("update routes on interface %s ifindex %d",
2064 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2067 /* Get RIP interface. */
2068 ri
= ifc
->ifp
->info
;
2071 /* Set output stream. */
2074 /* Reset stream and RTE counter. */
2076 rtemax
= RIP_MAX_RTE
;
2078 /* If output interface is in simple password authentication mode, we
2079 need space for authentication data. */
2080 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2083 /* If output interface is in MD5 authentication mode, we need space
2084 for authentication header and data. */
2085 if (ri
->auth_type
== RIP_AUTH_MD5
)
2088 /* If output interface is in simple password authentication mode
2089 and string or keychain is specified we need space for auth. data */
2090 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2091 if (ri
->key_chain
) {
2092 struct keychain
*keychain
;
2094 keychain
= keychain_lookup(ri
->key_chain
);
2096 key
= key_lookup_for_send(keychain
);
2098 /* to be passed to auth functions later */
2099 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2100 if (strlen(auth_str
) == 0)
2104 if (version
== RIPv1
) {
2105 memcpy(&ifaddrclass
, ifc
->address
, sizeof(ifaddrclass
));
2106 apply_classful_mask_ipv4(&ifaddrclass
);
2108 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2112 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2118 if (listcount(list
) == 0)
2121 rinfo
= listgetdata(listhead(list
));
2123 * For RIPv1, if we are subnetted, output subnets in our
2124 * network that have the same mask as the output "interface".
2125 * For other networks, only the classfull version is output.
2127 if (version
== RIPv1
) {
2128 p
= (struct prefix_ipv4
*)&rp
->p
;
2130 if (IS_RIP_DEBUG_PACKET
)
2132 "RIPv1 mask check, %pFX considered for output",
2136 prefix_match((struct prefix
*)&ifaddrclass
,
2138 if ((ifc
->address
->prefixlen
!=
2140 (rp
->p
.prefixlen
!= IPV4_MAX_BITLEN
))
2143 memcpy(&classfull
, &rp
->p
,
2144 sizeof(struct prefix_ipv4
));
2145 apply_classful_mask_ipv4(&classfull
);
2146 if (rp
->p
.u
.prefix4
.s_addr
!= INADDR_ANY
&&
2147 classfull
.prefixlen
!= rp
->p
.prefixlen
)
2150 if (IS_RIP_DEBUG_PACKET
)
2152 "RIPv1 mask check, %pFX made it through",
2155 p
= (struct prefix_ipv4
*)&rp
->p
;
2157 /* Apply output filters. */
2158 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2162 /* Changed route only output. */
2163 if (route_type
== rip_changed_route
&&
2164 (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2167 /* Split horizon. */
2168 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2170 * We perform split horizon for RIP and connected
2171 * route. For rip routes, we want to suppress the
2172 * route if we would end up sending the route back on
2173 * the interface that we learned it from, with a
2174 * higher metric. For connected routes, we suppress
2175 * the route if the prefix is a subset of the source
2176 * address that we are going to use for the packet
2177 * (in order to handle the case when multiple subnets
2178 * are configured on the same interface).
2181 struct rip_info
*tmp_rinfo
= NULL
;
2182 struct connected
*tmp_ifc
= NULL
;
2184 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2185 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2186 tmp_rinfo
->nh
.ifindex
==
2187 ifc
->ifp
->ifindex
) {
2192 if (!suppress
&& rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2193 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2195 if (prefix_match((struct prefix
*)p
,
2196 tmp_ifc
->address
)) {
2206 /* Preparation for route-map. */
2207 rinfo
->metric_set
= 0;
2208 rinfo
->nexthop_out
.s_addr
= 0;
2209 rinfo
->metric_out
= rinfo
->metric
;
2210 rinfo
->tag_out
= rinfo
->tag
;
2211 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2213 /* In order to avoid some local loops, if the RIP route has
2214 * a nexthop via this interface, keep the nexthop, otherwise
2215 * set it to 0. The nexthop should not be propagated beyond
2216 * the local broadcast/multicast area in order to avoid an
2217 * IGP multi-level recursive look-up. see (4.4)
2219 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2220 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2222 /* Interface route-map */
2223 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2224 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_OUT
],
2225 (struct prefix
*)p
, rinfo
);
2227 if (ret
== RMAP_DENYMATCH
) {
2228 if (IS_RIP_DEBUG_PACKET
)
2230 "RIP %pFX is filtered by route-map out",
2236 /* Apply redistribute route map - continue, if deny */
2237 if (rip
->redist
[rinfo
->type
].route_map
.name
&&
2238 rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2239 ret
= route_map_apply(
2240 rip
->redist
[rinfo
->type
].route_map
.map
,
2241 (struct prefix
*)p
, rinfo
);
2243 if (ret
== RMAP_DENYMATCH
) {
2244 if (IS_RIP_DEBUG_PACKET
)
2246 "%pFX is filtered by route-map",
2252 /* When route-map does not set metric. */
2253 if (!rinfo
->metric_set
) {
2254 /* If redistribute metric is set. */
2255 if (rip
->redist
[rinfo
->type
].metric_config
&&
2256 rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2258 rip
->redist
[rinfo
->type
].metric
;
2260 /* If the route is not connected or localy
2261 * generated one, use default-metric value
2263 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&&
2264 rinfo
->type
!= ZEBRA_ROUTE_CONNECT
&&
2265 rinfo
->metric
!= RIP_METRIC_INFINITY
)
2266 rinfo
->metric_out
= rip
->default_metric
;
2270 /* Apply offset-list */
2271 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2272 rip_offset_list_apply_out(p
, ifc
->ifp
,
2273 &rinfo
->metric_out
);
2275 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2276 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2278 /* Perform split-horizon with poisoned reverse
2279 * for RIP and connected routes.
2281 if (ri
->split_horizon
== RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2283 * We perform split horizon for RIP and connected
2284 * route. For rip routes, we want to suppress the
2285 * route if we would end up sending the route back
2286 * on the interface that we learned it from, with a
2287 * higher metric. For connected routes, we suppress
2288 * the route if the prefix is a subset of the source
2289 * address that we are going to use for the packet
2290 * (in order to handle the case when multiple
2291 * subnets are configured on the same interface).
2293 struct rip_info
*tmp_rinfo
= NULL
;
2294 struct connected
*tmp_ifc
= NULL
;
2296 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, tmp_rinfo
))
2297 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
&&
2298 tmp_rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2299 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2301 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
&&
2302 rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2303 for (ALL_LIST_ELEMENTS_RO(ifc
->ifp
->connected
,
2305 if (prefix_match((struct prefix
*)p
,
2306 tmp_ifc
->address
)) {
2308 RIP_METRIC_INFINITY
;
2314 /* Prepare preamble, auth headers, if needs be */
2316 stream_putc(s
, RIP_RESPONSE
);
2317 stream_putc(s
, version
);
2320 /* auth header for !v1 && !no_auth */
2321 if ((ri
->auth_type
!= RIP_NO_AUTH
) &&
2323 doff
= rip_auth_header_write(
2324 s
, ri
, key
, auth_str
,
2325 RIP_AUTH_SIMPLE_SIZE
);
2328 /* Write RTE to the stream. */
2329 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2330 if (num
== rtemax
) {
2331 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2332 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2333 RIP_AUTH_SIMPLE_SIZE
);
2335 ret
= rip_send_packet(STREAM_DATA(s
),
2336 stream_get_endp(s
), to
, ifc
);
2338 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2340 (struct rip_packet
*)STREAM_DATA(s
),
2341 stream_get_endp(s
), "SEND");
2347 /* Flush unwritten RTE. */
2349 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2350 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2351 RIP_AUTH_SIMPLE_SIZE
);
2353 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2356 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2357 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2358 stream_get_endp(s
), "SEND");
2362 /* Statistics updates. */
2366 /* Send RIP packet to the interface. */
2367 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2370 struct interface
*ifp
= ifc
->ifp
;
2371 struct rip_interface
*ri
= ifp
->info
;
2372 struct sockaddr_in to
;
2374 /* When RIP version is 2 and multicast enable interface. */
2375 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2376 if (IS_RIP_DEBUG_EVENT
)
2377 zlog_debug("multicast announce on %s ", ifp
->name
);
2379 rip_output_process(ifc
, NULL
, route_type
, version
);
2383 /* If we can't send multicast packet, send it with unicast. */
2384 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2385 if (ifc
->address
->family
== AF_INET
) {
2386 /* Destination address and port setting. */
2387 memset(&to
, 0, sizeof(struct sockaddr_in
));
2388 if (ifc
->destination
)
2389 /* use specified broadcast or peer destination
2391 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2392 else if (ifc
->address
->prefixlen
< IPV4_MAX_BITLEN
)
2393 /* calculate the appropriate broadcast address
2395 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2396 ifc
->address
->u
.prefix4
.s_addr
,
2397 ifc
->address
->prefixlen
);
2399 /* do not know where to send the packet */
2401 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2403 if (IS_RIP_DEBUG_EVENT
)
2404 zlog_debug("%s announce to %pI4 on %s",
2405 CONNECTED_PEER(ifc
) ? "unicast"
2407 &to
.sin_addr
, ifp
->name
);
2409 rip_output_process(ifc
, &to
, route_type
, version
);
2414 /* Update send to all interface and neighbor. */
2415 static void rip_update_process(struct rip
*rip
, int route_type
)
2417 struct listnode
*ifnode
, *ifnnode
;
2418 struct connected
*connected
;
2419 struct interface
*ifp
;
2420 struct rip_interface
*ri
;
2421 struct route_node
*rp
;
2422 struct sockaddr_in to
;
2425 /* Send RIP update to each interface. */
2426 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2427 if (if_is_loopback(ifp
))
2430 if (!if_is_operative(ifp
))
2433 /* Fetch RIP interface information. */
2436 /* When passive interface is specified, suppress announce to the
2445 * If there is no version configuration in the
2446 * interface, use rip's version setting.
2448 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
) ? rip
->version_send
2451 if (IS_RIP_DEBUG_EVENT
)
2452 zlog_debug("SEND UPDATE to %s ifindex %d", ifp
->name
,
2455 /* send update on each connected network */
2456 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2458 if (connected
->address
->family
== AF_INET
) {
2460 rip_update_interface(connected
, RIPv1
,
2462 if ((vsend
& RIPv2
) && if_is_multicast(ifp
))
2463 rip_update_interface(connected
, RIPv2
,
2469 /* RIP send updates to each neighbor. */
2470 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
)) {
2471 if (rp
->info
== NULL
)
2476 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2480 "Neighbor %pI4 doesn't have connected interface!",
2485 /* Set destination address and port */
2486 memset(&to
, 0, sizeof(struct sockaddr_in
));
2487 to
.sin_addr
= p
->u
.prefix4
;
2488 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2490 /* RIP version is rip's configuration. */
2491 rip_output_process(connected
, &to
, route_type
,
2496 /* RIP's periodical timer. */
2497 static void rip_update(struct thread
*t
)
2499 struct rip
*rip
= THREAD_ARG(t
);
2501 if (IS_RIP_DEBUG_EVENT
)
2502 zlog_debug("update timer fire!");
2504 /* Process update output. */
2505 rip_update_process(rip
, rip_all_route
);
2507 /* Triggered updates may be suppressed if a regular update is due by
2508 the time the triggered update would be sent. */
2509 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2512 /* Register myself. */
2513 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2516 /* Walk down the RIP routing table then clear changed flag. */
2517 static void rip_clear_changed_flag(struct rip
*rip
)
2519 struct route_node
*rp
;
2520 struct rip_info
*rinfo
= NULL
;
2521 struct list
*list
= NULL
;
2522 struct listnode
*listnode
= NULL
;
2524 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2530 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2531 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2532 /* This flag can be set only on the first entry. */
2538 /* Triggered update interval timer. */
2539 static void rip_triggered_interval(struct thread
*t
)
2541 struct rip
*rip
= THREAD_ARG(t
);
2545 rip_triggered_update(t
);
2549 /* Execute triggered update. */
2550 static void rip_triggered_update(struct thread
*t
)
2552 struct rip
*rip
= THREAD_ARG(t
);
2555 /* Cancel interval timer. */
2556 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2559 /* Logging triggered update. */
2560 if (IS_RIP_DEBUG_EVENT
)
2561 zlog_debug("triggered update!");
2563 /* Split Horizon processing is done when generating triggered
2564 updates as well as normal updates (see section 2.6). */
2565 rip_update_process(rip
, rip_changed_route
);
2567 /* Once all of the triggered updates have been generated, the route
2568 change flags should be cleared. */
2569 rip_clear_changed_flag(rip
);
2571 /* After a triggered update is sent, a timer should be set for a
2572 random interval between 1 and 5 seconds. If other changes that
2573 would trigger updates occur before the timer expires, a single
2574 update is triggered when the timer expires. */
2575 interval
= (frr_weak_random() % 5) + 1;
2577 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2578 &rip
->t_triggered_interval
);
2581 /* Withdraw redistributed route. */
2582 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2584 struct route_node
*rp
;
2585 struct rip_info
*rinfo
= NULL
;
2586 struct list
*list
= NULL
;
2588 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2594 rinfo
= listgetdata(listhead(list
));
2596 if (rinfo
->type
!= type
)
2599 if (rinfo
->sub_type
== RIP_ROUTE_INTERFACE
)
2602 /* Perform poisoned reverse. */
2603 rinfo
->metric
= RIP_METRIC_INFINITY
;
2604 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
2606 RIP_TIMER_OFF(rinfo
->t_timeout
);
2607 rinfo
->flags
|= RIP_RTF_CHANGED
;
2609 if (IS_RIP_DEBUG_EVENT
) {
2610 struct prefix_ipv4
*p
= (struct prefix_ipv4
*)&rp
->p
;
2613 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2615 ifindex2ifname(rinfo
->nh
.ifindex
,
2619 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2623 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2627 vrf
= vrf_lookup_by_id(vrf_id
);
2634 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2638 rip
.vrf_name
= (char *)vrf_name
;
2640 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2643 /* Create new RIP instance and set it to global variable. */
2644 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2648 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2649 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2651 /* Set initial value. */
2652 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2653 rip
->default_metric
=
2654 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2656 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2657 rip
->passive_default
=
2658 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2659 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2661 rip
->timeout_time
= yang_get_default_uint32(
2662 "%s/timers/holddown-interval", RIP_INSTANCE
);
2663 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2666 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2668 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2670 /* Initialize RIP data structures. */
2671 rip
->table
= route_table_init();
2672 route_table_set_info(rip
->table
, rip
);
2673 rip
->neighbor
= route_table_init();
2674 rip
->peer_list
= list_new();
2675 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2676 rip
->peer_list
->del
= rip_peer_list_del
;
2677 rip
->distance_table
= route_table_init();
2678 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2679 rip
->enable_interface
= vector_init(1);
2680 rip
->enable_network
= route_table_init();
2681 rip
->passive_nondefault
= vector_init(1);
2682 rip
->offset_list_master
= list_new();
2683 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2684 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2686 /* Distribute list install. */
2687 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2688 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2689 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2691 /* if rmap install. */
2692 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2693 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2694 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2696 /* Make output stream. */
2697 rip
->obuf
= stream_new(1500);
2699 /* Enable the routing instance if possible. */
2700 if (vrf
&& vrf_is_enabled(vrf
))
2701 rip_instance_enable(rip
, vrf
, socket
);
2707 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2712 /* Sned RIP request to the destination. */
2713 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2714 uint8_t version
, struct connected
*connected
)
2717 struct rip_packet rip_packet
;
2718 struct listnode
*node
, *nnode
;
2720 memset(&rip_packet
, 0, sizeof(rip_packet
));
2722 rip_packet
.command
= RIP_REQUEST
;
2723 rip_packet
.version
= version
;
2724 rte
= rip_packet
.rte
;
2725 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2729 * connected is only sent for ripv1 case, or when
2730 * interface does not support multicast. Caller loops
2731 * over each connected address for this case.
2733 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2735 != sizeof(rip_packet
))
2738 return sizeof(rip_packet
);
2741 /* send request on each connected network */
2742 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2743 struct prefix_ipv4
*p
;
2745 p
= (struct prefix_ipv4
*)connected
->address
;
2747 if (p
->family
!= AF_INET
)
2750 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2752 != sizeof(rip_packet
))
2755 return sizeof(rip_packet
);
2758 static int rip_update_jitter(unsigned long time
)
2760 #define JITTER_BOUND 4
2761 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2762 Given that, we cannot let time be less than JITTER_BOUND seconds.
2763 The RIPv2 RFC says jitter should be small compared to
2764 update_time. We consider 1/JITTER_BOUND to be small.
2767 int jitter_input
= time
;
2770 if (jitter_input
< JITTER_BOUND
)
2771 jitter_input
= JITTER_BOUND
;
2773 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2776 return jitter
/ JITTER_BOUND
;
2779 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2785 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2787 case RIP_UPDATE_EVENT
:
2788 RIP_TIMER_OFF(rip
->t_update
);
2789 jitter
= rip_update_jitter(rip
->update_time
);
2790 thread_add_timer(master
, rip_update
, rip
,
2791 sock
? 2 : rip
->update_time
+ jitter
,
2794 case RIP_TRIGGERED_UPDATE
:
2795 if (rip
->t_triggered_interval
)
2798 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2799 &rip
->t_triggered_update
);
2806 struct rip_distance
*rip_distance_new(void)
2808 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2811 void rip_distance_free(struct rip_distance
*rdistance
)
2813 if (rdistance
->access_list
)
2814 free(rdistance
->access_list
);
2815 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2818 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2819 struct route_node
*node
)
2821 struct rip_distance
*rdistance
;
2823 rdistance
= node
->info
;
2825 rip_distance_free(rdistance
);
2828 /* Apply RIP information to distance method. */
2829 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2831 struct route_node
*rn
;
2832 struct prefix_ipv4 p
;
2833 struct rip_distance
*rdistance
;
2834 struct access_list
*alist
;
2836 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2838 p
.prefix
= rinfo
->from
;
2839 p
.prefixlen
= IPV4_MAX_BITLEN
;
2841 /* Check source address. */
2842 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2844 rdistance
= rn
->info
;
2845 route_unlock_node(rn
);
2847 if (rdistance
->access_list
) {
2848 alist
= access_list_lookup(AFI_IP
,
2849 rdistance
->access_list
);
2852 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2856 return rdistance
->distance
;
2858 return rdistance
->distance
;
2862 return rip
->distance
;
2867 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2869 struct route_node
*rn
;
2870 struct rip_distance
*rdistance
;
2874 vty_out(vty
, " Distance: (default is %u)\n",
2875 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2877 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
)) {
2878 rdistance
= rn
->info
;
2880 if (rdistance
== NULL
)
2884 vty_out(vty
, " Address Distance List\n");
2887 snprintfrr(buf
, sizeof(buf
), "%pFX", &rn
->p
);
2888 vty_out(vty
, " %-20s %4d %s\n", buf
, rdistance
->distance
,
2889 rdistance
->access_list
? rdistance
->access_list
: "");
2893 /* Update ECMP routes to zebra when ECMP is disabled. */
2894 void rip_ecmp_disable(struct rip
*rip
)
2896 struct route_node
*rp
;
2897 struct rip_info
*rinfo
, *tmp_rinfo
;
2899 struct listnode
*node
, *nextnode
;
2901 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
2906 if (listcount(list
) == 0)
2909 rinfo
= listgetdata(listhead(list
));
2910 if (!rip_route_rte(rinfo
))
2913 /* Drop all other entries, except the first one. */
2914 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
)) {
2915 if (tmp_rinfo
== rinfo
)
2918 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
2919 RIP_TIMER_OFF(tmp_rinfo
->t_garbage_collect
);
2920 list_delete_node(list
, node
);
2921 rip_info_free(tmp_rinfo
);
2925 rip_zebra_ipv4_add(rip
, rp
);
2927 /* Set the route change flag. */
2928 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2930 /* Signal the output process to trigger an update. */
2931 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2935 /* Print out routes update time. */
2936 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2941 char timebuf
[TIME_BUF
];
2942 struct thread
*thread
;
2944 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2945 clock
= thread_timer_remain_second(thread
);
2946 gmtime_r(&clock
, &tm
);
2947 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2948 vty_out(vty
, "%5s", timebuf
);
2949 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2950 clock
= thread_timer_remain_second(thread
);
2951 gmtime_r(&clock
, &tm
);
2952 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2953 vty_out(vty
, "%5s", timebuf
);
2957 static const char *rip_route_type_print(int sub_type
)
2962 case RIP_ROUTE_STATIC
:
2964 case RIP_ROUTE_DEFAULT
:
2966 case RIP_ROUTE_REDISTRIBUTE
:
2968 case RIP_ROUTE_INTERFACE
:
2977 "show ip rip [vrf NAME]",
2984 struct route_node
*np
;
2985 struct rip_info
*rinfo
= NULL
;
2986 struct list
*list
= NULL
;
2987 struct listnode
*listnode
= NULL
;
2988 const char *vrf_name
;
2991 if (argv_find(argv
, argc
, "vrf", &idx
))
2992 vrf_name
= argv
[idx
+ 1]->arg
;
2994 vrf_name
= VRF_DEFAULT_NAME
;
2996 rip
= rip_lookup_by_vrf_name(vrf_name
);
2998 vty_out(vty
, "%% RIP instance not found\n");
3001 if (!rip
->enabled
) {
3002 vty_out(vty
, "%% RIP instance is disabled\n");
3007 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3009 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3010 " (i) - interface\n\n"
3011 " Network Next Hop Metric From Tag Time\n");
3013 for (np
= route_top(rip
->table
); np
; np
= route_next(np
)) {
3019 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3022 len
= vty_out(vty
, "%c(%s) %pFX",
3023 /* np->lock, For debugging. */
3024 zebra_route_char(rinfo
->type
),
3025 rip_route_type_print(rinfo
->sub_type
),
3031 vty_out(vty
, "%*s", len
, " ");
3033 switch (rinfo
->nh
.type
) {
3034 case NEXTHOP_TYPE_IPV4
:
3035 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3036 vty_out(vty
, "%-20pI4 %2d ",
3037 &rinfo
->nh
.gate
.ipv4
, rinfo
->metric
);
3039 case NEXTHOP_TYPE_IFINDEX
:
3040 vty_out(vty
, "0.0.0.0 %2d ",
3043 case NEXTHOP_TYPE_BLACKHOLE
:
3044 vty_out(vty
, "blackhole %2d ",
3047 case NEXTHOP_TYPE_IPV6
:
3048 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3049 vty_out(vty
, "V6 Address Hidden %2d ",
3054 /* Route which exist in kernel routing table. */
3055 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
) &&
3056 (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3057 vty_out(vty
, "%-15pI4 ", &rinfo
->from
);
3058 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3059 (route_tag_t
)rinfo
->tag
);
3060 rip_vty_out_uptime(vty
, rinfo
);
3061 } else if (rinfo
->metric
== RIP_METRIC_INFINITY
) {
3062 vty_out(vty
, "self ");
3063 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3064 (route_tag_t
)rinfo
->tag
);
3065 rip_vty_out_uptime(vty
, rinfo
);
3067 if (rinfo
->external_metric
) {
3069 vty
, "self (%s:%d)",
3070 zebra_route_string(rinfo
->type
),
3071 rinfo
->external_metric
);
3074 vty_out(vty
, "%*s", len
, " ");
3076 vty_out(vty
, "self ");
3077 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3078 (route_tag_t
)rinfo
->tag
);
3087 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3088 DEFUN (show_ip_rip_status
,
3089 show_ip_rip_status_cmd
,
3090 "show ip rip [vrf NAME] status",
3095 "IP routing protocol process parameters and statistics\n")
3098 struct interface
*ifp
;
3099 struct rip_interface
*ri
;
3100 extern const struct message ri_version_msg
[];
3101 const char *send_version
;
3102 const char *receive_version
;
3103 const char *vrf_name
;
3106 if (argv_find(argv
, argc
, "vrf", &idx
))
3107 vrf_name
= argv
[idx
+ 1]->arg
;
3109 vrf_name
= VRF_DEFAULT_NAME
;
3111 rip
= rip_lookup_by_vrf_name(vrf_name
);
3113 vty_out(vty
, "%% RIP instance not found\n");
3116 if (!rip
->enabled
) {
3117 vty_out(vty
, "%% RIP instance is disabled\n");
3121 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3122 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3124 vty_out(vty
, " next due in %lu seconds\n",
3125 thread_timer_remain_second(rip
->t_update
));
3126 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3127 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3129 /* Filtering status show. */
3130 config_show_distribute(vty
, rip
->distribute_ctx
);
3132 /* Default metric information. */
3133 vty_out(vty
, " Default redistribution metric is %u\n",
3134 rip
->default_metric
);
3136 /* Redistribute information. */
3137 vty_out(vty
, " Redistributing:");
3138 rip_show_redistribute_config(vty
, rip
);
3141 vty_out(vty
, " Default version control: send version %s,",
3142 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3143 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3144 vty_out(vty
, " receive any version \n");
3146 vty_out(vty
, " receive version %s \n",
3147 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3149 vty_out(vty
, " Interface Send Recv Key-chain\n");
3151 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3157 if (ri
->enable_network
|| ri
->enable_interface
) {
3158 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3160 lookup_msg(ri_version_msg
,
3161 rip
->version_send
, NULL
);
3163 send_version
= lookup_msg(ri_version_msg
,
3166 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3168 lookup_msg(ri_version_msg
,
3169 rip
->version_recv
, NULL
);
3171 receive_version
= lookup_msg(
3172 ri_version_msg
, ri
->ri_receive
, NULL
);
3174 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3175 send_version
, receive_version
,
3176 ri
->key_chain
? ri
->key_chain
: "");
3180 vty_out(vty
, " Routing for Networks:\n");
3181 rip_show_network_config(vty
, rip
);
3183 int found_passive
= 0;
3184 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3187 if ((ri
->enable_network
|| ri
->enable_interface
) &&
3189 if (!found_passive
) {
3190 vty_out(vty
, " Passive Interface(s):\n");
3193 vty_out(vty
, " %s\n", ifp
->name
);
3197 vty_out(vty
, " Routing Information Sources:\n");
3199 " Gateway BadPackets BadRoutes Distance Last Update\n");
3200 rip_peer_display(vty
, rip
);
3202 rip_distance_show(vty
, rip
);
3207 /* RIP configuration write function. */
3208 static int config_write_rip(struct vty
*vty
)
3213 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3214 char xpath
[XPATH_MAXLEN
];
3215 struct lyd_node
*dnode
;
3217 snprintf(xpath
, sizeof(xpath
),
3218 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3220 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3223 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3225 /* Distribute configuration. */
3226 config_write_distribute(vty
, rip
->distribute_ctx
);
3228 /* Interface routemap configuration */
3229 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3231 vty_out(vty
, "exit\n");
3239 static int config_write_rip(struct vty
*vty
);
3240 /* RIP node structure. */
3241 static struct cmd_node rip_node
= {
3244 .parent_node
= CONFIG_NODE
,
3245 .prompt
= "%s(config-router)# ",
3246 .config_write
= config_write_rip
,
3249 /* Distribute-list update functions. */
3250 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3251 struct distribute
*dist
)
3253 struct interface
*ifp
;
3254 struct rip_interface
*ri
;
3255 struct access_list
*alist
;
3256 struct prefix_list
*plist
;
3258 if (!ctx
->vrf
|| !dist
->ifname
)
3261 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3267 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3268 alist
= access_list_lookup(AFI_IP
,
3269 dist
->list
[DISTRIBUTE_V4_IN
]);
3271 ri
->list
[RIP_FILTER_IN
] = alist
;
3273 ri
->list
[RIP_FILTER_IN
] = NULL
;
3275 ri
->list
[RIP_FILTER_IN
] = NULL
;
3277 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3278 alist
= access_list_lookup(AFI_IP
,
3279 dist
->list
[DISTRIBUTE_V4_OUT
]);
3281 ri
->list
[RIP_FILTER_OUT
] = alist
;
3283 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3285 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3287 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3288 plist
= prefix_list_lookup(AFI_IP
,
3289 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3291 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3293 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3295 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3297 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3298 plist
= prefix_list_lookup(AFI_IP
,
3299 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3301 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3303 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3305 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3308 void rip_distribute_update_interface(struct interface
*ifp
)
3310 struct rip_interface
*ri
= ifp
->info
;
3311 struct rip
*rip
= ri
->rip
;
3312 struct distribute
*dist
;
3316 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3318 rip_distribute_update(rip
->distribute_ctx
, dist
);
3321 /* Update all interface's distribute list. */
3323 static void rip_distribute_update_all(struct prefix_list
*notused
)
3325 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3326 struct interface
*ifp
;
3328 FOR_ALL_INTERFACES (vrf
, ifp
)
3329 rip_distribute_update_interface(ifp
);
3332 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3334 rip_distribute_update_all(NULL
);
3337 /* Delete all added rip route. */
3338 void rip_clean(struct rip
*rip
)
3340 rip_interfaces_clean(rip
);
3343 rip_instance_disable(rip
);
3345 stream_free(rip
->obuf
);
3347 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3348 if (rip
->redist
[i
].route_map
.name
)
3349 free(rip
->redist
[i
].route_map
.name
);
3351 route_table_finish(rip
->table
);
3352 route_table_finish(rip
->neighbor
);
3353 list_delete(&rip
->peer_list
);
3354 distribute_list_delete(&rip
->distribute_ctx
);
3355 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3357 rip_clean_network(rip
);
3358 rip_passive_nondefault_clean(rip
);
3359 vector_free(rip
->enable_interface
);
3360 route_table_finish(rip
->enable_network
);
3361 vector_free(rip
->passive_nondefault
);
3362 list_delete(&rip
->offset_list_master
);
3363 route_table_finish(rip
->distance_table
);
3365 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3366 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3367 XFREE(MTYPE_RIP
, rip
);
3370 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3371 struct if_rmap
*if_rmap
)
3373 struct interface
*ifp
= NULL
;
3374 struct rip_interface
*ri
;
3375 struct route_map
*rmap
;
3376 struct vrf
*vrf
= NULL
;
3379 vrf
= vrf_lookup_by_name(ctx
->name
);
3381 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3386 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3387 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3389 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3391 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3393 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3395 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3396 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3398 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3400 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3402 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3405 void rip_if_rmap_update_interface(struct interface
*ifp
)
3407 struct rip_interface
*ri
= ifp
->info
;
3408 struct rip
*rip
= ri
->rip
;
3409 struct if_rmap
*if_rmap
;
3410 struct if_rmap_ctx
*ctx
;
3414 ctx
= rip
->if_rmap_ctx
;
3417 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3419 rip_if_rmap_update(ctx
, if_rmap
);
3422 static void rip_routemap_update_redistribute(struct rip
*rip
)
3424 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3425 if (rip
->redist
[i
].route_map
.name
) {
3426 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3427 rip
->redist
[i
].route_map
.name
);
3428 route_map_counter_increment(
3429 rip
->redist
[i
].route_map
.map
);
3435 static void rip_routemap_update(const char *notused
)
3437 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3439 struct interface
*ifp
;
3441 FOR_ALL_INTERFACES (vrf
, ifp
)
3442 rip_if_rmap_update_interface(ifp
);
3446 rip_routemap_update_redistribute(rip
);
3449 /* Link RIP instance to VRF. */
3450 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3452 struct interface
*ifp
;
3455 rip
->distribute_ctx
->vrf
= vrf
;
3458 FOR_ALL_INTERFACES (vrf
, ifp
)
3459 rip_interface_sync(ifp
);
3462 /* Unlink RIP instance from VRF. */
3463 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3465 struct interface
*ifp
;
3468 rip
->distribute_ctx
->vrf
= NULL
;
3471 FOR_ALL_INTERFACES (vrf
, ifp
)
3472 rip_interface_sync(ifp
);
3475 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3479 rip_vrf_link(rip
, vrf
);
3480 rip
->enabled
= true;
3482 /* Resend all redistribute requests. */
3483 rip_redistribute_enable(rip
);
3485 /* Create read and timer thread. */
3486 rip_event(rip
, RIP_READ
, rip
->sock
);
3487 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3489 rip_zebra_vrf_register(vrf
);
3492 static void rip_instance_disable(struct rip
*rip
)
3494 struct vrf
*vrf
= rip
->vrf
;
3495 struct route_node
*rp
;
3497 /* Clear RIP routes */
3498 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3499 struct rip_info
*rinfo
;
3501 struct listnode
*listnode
;
3503 if ((list
= rp
->info
) == NULL
)
3506 rinfo
= listgetdata(listhead(list
));
3507 if (rip_route_rte(rinfo
))
3508 rip_zebra_ipv4_delete(rip
, rp
);
3510 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3511 RIP_TIMER_OFF(rinfo
->t_timeout
);
3512 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3513 rip_info_free(rinfo
);
3517 route_unlock_node(rp
);
3520 /* Flush all redistribute requests. */
3521 rip_redistribute_disable(rip
);
3523 /* Cancel RIP related timers. */
3524 RIP_TIMER_OFF(rip
->t_update
);
3525 RIP_TIMER_OFF(rip
->t_triggered_update
);
3526 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3528 /* Cancel read thread. */
3529 thread_cancel(&rip
->t_read
);
3531 /* Close RIP socket. */
3535 /* Clear existing peers. */
3536 list_delete_all_node(rip
->peer_list
);
3538 rip_zebra_vrf_deregister(vrf
);
3540 rip_vrf_unlink(rip
, vrf
);
3541 rip
->enabled
= false;
3544 static int rip_vrf_new(struct vrf
*vrf
)
3546 if (IS_RIP_DEBUG_EVENT
)
3547 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3553 static int rip_vrf_delete(struct vrf
*vrf
)
3555 if (IS_RIP_DEBUG_EVENT
)
3556 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3562 static int rip_vrf_enable(struct vrf
*vrf
)
3567 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3568 if (!rip
|| rip
->enabled
)
3571 if (IS_RIP_DEBUG_EVENT
)
3572 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3575 /* Activate the VRF RIP instance. */
3576 if (!rip
->enabled
) {
3577 socket
= rip_create_socket(vrf
);
3581 rip_instance_enable(rip
, vrf
, socket
);
3587 static int rip_vrf_disable(struct vrf
*vrf
)
3591 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3592 if (!rip
|| !rip
->enabled
)
3595 if (IS_RIP_DEBUG_EVENT
)
3596 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3599 /* Deactivate the VRF RIP instance. */
3601 rip_instance_disable(rip
);
3606 void rip_vrf_init(void)
3608 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
);
3613 void rip_vrf_terminate(void)
3618 /* Allocate new rip structure and set default value. */
3621 /* Install top nodes. */
3622 install_node(&rip_node
);
3624 /* Install rip commands. */
3625 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3626 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3628 install_default(RIP_NODE
);
3630 /* Debug related init. */
3633 /* Access list install. */
3635 access_list_add_hook(rip_distribute_update_all_wrapper
);
3636 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3638 /* Prefix list initialize.*/
3640 prefix_list_add_hook(rip_distribute_update_all
);
3641 prefix_list_delete_hook(rip_distribute_update_all
);
3644 rip_route_map_init();
3646 route_map_add_hook(rip_routemap_update
);
3647 route_map_delete_hook(rip_routemap_update
);
3649 if_rmap_init(RIP_NODE
);