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"
49 #include "ripd/ripd.h"
50 #include "ripd/rip_nb.h"
51 #include "ripd/rip_debug.h"
52 #include "ripd/rip_errors.h"
53 #include "ripd/rip_interface.h"
55 /* UDP receive buffer size */
56 #define RIP_UDP_RCV_BUF 41600
58 DEFINE_MGROUP(RIPD
, "ripd")
59 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure")
60 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name")
61 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info")
62 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance")
65 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
67 static int rip_triggered_update(struct thread
*);
68 static int rip_update_jitter(unsigned long);
69 static void rip_distance_table_node_cleanup(struct route_table
*table
,
70 struct route_node
*node
);
71 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
72 static void rip_instance_disable(struct rip
*rip
);
74 static void rip_distribute_update(struct distribute_ctx
*ctx
,
75 struct distribute
*dist
);
77 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
78 struct if_rmap
*if_rmap
);
80 /* RIP output routes type. */
81 enum { rip_all_route
, rip_changed_route
};
83 /* RIP command strings. */
84 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
85 {RIP_RESPONSE
, "RESPONSE"},
86 {RIP_TRACEON
, "TRACEON"},
87 {RIP_TRACEOFF
, "TRACEOFF"},
89 {RIP_POLL_ENTRY
, "POLL ENTRY"},
92 /* Generate rb-tree of RIP instances. */
93 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
95 return strcmp(a
->vrf_name
, b
->vrf_name
);
97 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
99 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
101 /* Utility function to set boradcast option to the socket. */
102 static int sockopt_broadcast(int sock
)
107 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
110 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
116 int rip_route_rte(struct rip_info
*rinfo
)
118 return (rinfo
->type
== ZEBRA_ROUTE_RIP
119 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
122 static struct rip_info
*rip_info_new(void)
124 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
127 void rip_info_free(struct rip_info
*rinfo
)
129 XFREE(MTYPE_RIP_INFO
, rinfo
);
132 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
134 return route_table_get_info(rinfo
->rp
->table
);
137 /* RIP route garbage collect timer. */
138 static int rip_garbage_collect(struct thread
*t
)
140 struct rip_info
*rinfo
;
141 struct route_node
*rp
;
143 rinfo
= THREAD_ARG(t
);
144 rinfo
->t_garbage_collect
= NULL
;
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
);
165 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
167 /* Add new route to the ECMP list.
168 * RETURN: the new entry added in the list, or NULL if it is not the first
169 * entry and ECMP is not allowed.
171 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
173 struct route_node
*rp
= rinfo_new
->rp
;
174 struct rip_info
*rinfo
= NULL
;
175 struct list
*list
= NULL
;
177 if (rp
->info
== NULL
)
178 rp
->info
= list_new();
179 list
= (struct list
*)rp
->info
;
181 /* If ECMP is not allowed and some entry already exists in the list,
183 if (listcount(list
) && !rip
->ecmp
)
186 rinfo
= rip_info_new();
187 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
188 listnode_add(list
, rinfo
);
190 if (rip_route_rte(rinfo
)) {
191 rip_timeout_update(rip
, rinfo
);
192 rip_zebra_ipv4_add(rip
, rp
);
195 /* Set the route change flag on the first entry. */
196 rinfo
= listgetdata(listhead(list
));
197 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
199 /* Signal the output process to trigger an update (see section 2.5). */
200 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
205 /* Replace the ECMP list with the new route.
206 * RETURN: the new entry added in the list
208 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
210 struct route_node
*rp
= rinfo_new
->rp
;
211 struct list
*list
= (struct list
*)rp
->info
;
212 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
213 struct listnode
*node
= NULL
, *nextnode
= NULL
;
215 if (list
== NULL
|| listcount(list
) == 0)
216 return rip_ecmp_add(rip
, rinfo_new
);
218 /* Get the first entry */
219 rinfo
= listgetdata(listhead(list
));
221 /* Learnt route replaced by a local one. Delete it from zebra. */
222 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
223 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
224 rip_zebra_ipv4_delete(rip
, rp
);
226 /* Re-use the first entry, and delete the others. */
227 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
228 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 int 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
);
315 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
317 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
318 RIP_TIMER_OFF(rinfo
->t_timeout
);
319 thread_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
324 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
325 struct rip_interface
*ri
)
327 struct distribute
*dist
;
328 struct access_list
*alist
;
329 struct prefix_list
*plist
;
330 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
332 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
334 /* Input distribute-list filtering. */
335 if (ri
->list
[rip_distribute
]) {
336 if (access_list_apply(ri
->list
[rip_distribute
],
339 if (IS_RIP_DEBUG_PACKET
)
340 zlog_debug("%s/%d filtered by distribute %s",
341 inet_ntoa(p
->prefix
), p
->prefixlen
,
346 if (ri
->prefix
[rip_distribute
]) {
347 if (prefix_list_apply(ri
->prefix
[rip_distribute
],
350 if (IS_RIP_DEBUG_PACKET
)
351 zlog_debug("%s/%d filtered by prefix-list %s",
352 inet_ntoa(p
->prefix
), p
->prefixlen
,
358 /* All interface filter check. */
359 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
361 if (dist
->list
[distribute
]) {
362 alist
= access_list_lookup(AFI_IP
,
363 dist
->list
[distribute
]);
366 if (access_list_apply(alist
, (struct prefix
*)p
)
368 if (IS_RIP_DEBUG_PACKET
)
370 "%s/%d filtered by distribute %s",
371 inet_ntoa(p
->prefix
),
372 p
->prefixlen
, inout
);
377 if (dist
->prefix
[distribute
]) {
378 plist
= prefix_list_lookup(AFI_IP
,
379 dist
->prefix
[distribute
]);
382 if (prefix_list_apply(plist
, (struct prefix
*)p
)
384 if (IS_RIP_DEBUG_PACKET
)
386 "%s/%d filtered by prefix-list %s",
387 inet_ntoa(p
->prefix
),
388 p
->prefixlen
, inout
);
397 /* Check nexthop address validity. */
398 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
400 struct interface
*ifp
;
401 struct listnode
*cnode
;
402 struct connected
*ifc
;
405 /* If nexthop address matches local configured address then it is
408 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
409 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
412 if (p
->family
== AF_INET
413 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
420 /* RIP add route to routing table. */
421 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
422 struct interface
*ifp
)
426 struct prefix_ipv4 p
;
427 struct route_node
*rp
;
428 struct rip_info
*rinfo
= NULL
, newinfo
;
429 struct rip_interface
*ri
;
430 struct in_addr
*nexthop
;
432 unsigned char old_dist
, new_dist
;
433 struct list
*list
= NULL
;
434 struct listnode
*node
= NULL
;
436 /* Make prefix structure. */
437 memset(&p
, 0, sizeof(struct prefix_ipv4
));
439 p
.prefix
= rte
->prefix
;
440 p
.prefixlen
= ip_masklen(rte
->mask
);
442 /* Make sure mask is applied. */
448 /* Apply input filters. */
449 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
453 memset(&newinfo
, 0, sizeof(newinfo
));
454 newinfo
.type
= ZEBRA_ROUTE_RIP
;
455 newinfo
.sub_type
= RIP_ROUTE_RTE
;
456 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
457 newinfo
.from
= from
->sin_addr
;
458 newinfo
.nh
.ifindex
= ifp
->ifindex
;
459 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
460 newinfo
.metric
= rte
->metric
;
461 newinfo
.metric_out
= rte
->metric
; /* XXX */
462 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
464 /* Modify entry according to the interface routemap. */
465 if (ri
->routemap
[RIP_FILTER_IN
]) {
466 /* The object should be of the type of rip_info */
467 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
468 (struct prefix
*)&p
, RMAP_RIP
, &newinfo
);
470 if (ret
== RMAP_DENYMATCH
) {
471 if (IS_RIP_DEBUG_PACKET
)
473 "RIP %s/%d is filtered by route-map in",
474 inet_ntoa(p
.prefix
), p
.prefixlen
);
478 /* Get back the object */
479 rte
->nexthop
= newinfo
.nexthop_out
;
480 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
481 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
485 /* Once the entry has been validated, update the metric by
486 adding the cost of the network on wich the message
487 arrived. If the result is greater than infinity, use infinity
488 (RFC2453 Sec. 3.9.2) */
489 /* Zebra ripd can handle offset-list in. */
490 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
492 /* If offset-list does not modify the metric use interface's
495 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
497 if (rte
->metric
> RIP_METRIC_INFINITY
)
498 rte
->metric
= RIP_METRIC_INFINITY
;
500 /* Set nexthop pointer. */
501 if (rte
->nexthop
.s_addr
== 0)
502 nexthop
= &from
->sin_addr
;
504 nexthop
= &rte
->nexthop
;
506 /* Check if nexthop address is myself, then do nothing. */
507 if (rip_nexthop_check(rip
, nexthop
) < 0) {
508 if (IS_RIP_DEBUG_PACKET
)
509 zlog_debug("Nexthop address %s is myself",
510 inet_ntoa(*nexthop
));
514 /* Get index for the prefix. */
515 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
518 newinfo
.nh
.gate
.ipv4
= *nexthop
;
519 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
520 newinfo
.metric
= rte
->metric
;
521 newinfo
.tag
= ntohs(rte
->tag
);
522 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
524 new_dist
= newinfo
.distance
? newinfo
.distance
525 : ZEBRA_RIP_DISTANCE_DEFAULT
;
527 /* Check to see whether there is already RIP route on the table. */
528 if ((list
= rp
->info
) != NULL
)
529 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
530 /* Need to compare with redistributed entry or local
532 if (!rip_route_rte(rinfo
))
535 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
536 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
539 if (!listnextnode(node
)) {
540 /* Not found in the list */
542 if (rte
->metric
> rinfo
->metric
) {
543 /* New route has a greater metric.
545 route_unlock_node(rp
);
549 if (rte
->metric
< rinfo
->metric
)
550 /* New route has a smaller metric.
551 * Replace the ECMP list
552 * with the new one in below. */
555 /* Metrics are same. We compare the distances.
557 old_dist
= rinfo
->distance
559 : ZEBRA_RIP_DISTANCE_DEFAULT
;
561 if (new_dist
> old_dist
) {
562 /* New route has a greater distance.
564 route_unlock_node(rp
);
568 if (new_dist
< old_dist
)
569 /* New route has a smaller distance.
570 * Replace the ECMP list
571 * with the new one in below. */
574 /* Metrics and distances are both same. Keep
576 * the new route is added in the ECMP list in
582 /* Local static route. */
583 if (rinfo
->type
== ZEBRA_ROUTE_RIP
584 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
585 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
586 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
587 route_unlock_node(rp
);
591 /* Redistributed route check. */
592 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
593 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
594 old_dist
= rinfo
->distance
;
595 /* Only routes directly connected to an interface
597 * may have a valid NULL distance */
598 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= 0)
601 : ZEBRA_RIP_DISTANCE_DEFAULT
;
602 /* If imported route does not have STRICT precedence,
603 mark it as a ghost */
604 if (new_dist
<= old_dist
605 && rte
->metric
!= RIP_METRIC_INFINITY
)
606 rip_ecmp_replace(rip
, &newinfo
);
608 route_unlock_node(rp
);
615 route_unlock_node(rp
);
617 /* Now, check to see whether there is already an explicit route
618 for the destination prefix. If there is no such route, add
619 this route to the routing table, unless the metric is
620 infinity (there is no point in adding a route which
622 if (rte
->metric
!= RIP_METRIC_INFINITY
)
623 rip_ecmp_add(rip
, &newinfo
);
625 /* Route is there but we are not sure the route is RIP or not.
628 /* If there is an existing route, compare the next hop address
629 to the address of the router from which the datagram came.
630 If this datagram is from the same router as the existing
631 route, reinitialize the timeout. */
632 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
633 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
635 old_dist
= rinfo
->distance
? rinfo
->distance
636 : ZEBRA_RIP_DISTANCE_DEFAULT
;
638 /* Next, compare the metrics. If the datagram is from the same
639 router as the existing route, and the new metric is different
640 than the old one; or, if the new metric is lower than the old
641 one, or if the tag has been changed; or if there is a route
642 with a lower administrave distance; or an update of the
643 distance on the actual route; do the following actions: */
644 if ((same
&& rinfo
->metric
!= rte
->metric
)
645 || (rte
->metric
< rinfo
->metric
)
646 || ((same
) && (rinfo
->metric
== rte
->metric
)
647 && (newinfo
.tag
!= rinfo
->tag
))
648 || (old_dist
> new_dist
)
649 || ((old_dist
!= new_dist
) && same
)) {
650 if (listcount(list
) == 1) {
651 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
652 rip_ecmp_replace(rip
, &newinfo
);
654 rip_ecmp_delete(rip
, rinfo
);
656 if (newinfo
.metric
< rinfo
->metric
)
657 rip_ecmp_replace(rip
, &newinfo
);
658 else if (newinfo
.metric
> rinfo
->metric
)
659 rip_ecmp_delete(rip
, rinfo
);
660 else if (new_dist
< old_dist
)
661 rip_ecmp_replace(rip
, &newinfo
);
662 else if (new_dist
> old_dist
)
663 rip_ecmp_delete(rip
, rinfo
);
665 int update
= CHECK_FLAG(rinfo
->flags
,
670 assert(newinfo
.metric
671 != RIP_METRIC_INFINITY
);
673 RIP_TIMER_OFF(rinfo
->t_timeout
);
674 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
675 memcpy(rinfo
, &newinfo
,
676 sizeof(struct rip_info
));
677 rip_timeout_update(rip
, rinfo
);
680 rip_zebra_ipv4_add(rip
, rp
);
682 /* - Set the route change flag on the
684 rinfo
= listgetdata(listhead(list
));
685 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
686 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
689 } else /* same & no change */
690 rip_timeout_update(rip
, rinfo
);
692 /* Unlock tempolary lock of the route. */
693 route_unlock_node(rp
);
697 /* Dump RIP packet */
698 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
703 const char *command_str
;
704 char pbuf
[BUFSIZ
], nbuf
[BUFSIZ
];
708 /* Set command string. */
709 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
710 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
712 command_str
= "unknown";
714 /* Dump packet header. */
715 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
716 packet
->version
, size
);
718 /* Dump each routing table entry. */
721 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
722 if (packet
->version
== RIPv2
) {
723 netmask
= ip_masklen(rte
->mask
);
725 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
727 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
728 p
= (uint8_t *)&rte
->prefix
;
731 " family 0x%X type %d auth string: %s",
734 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
735 struct rip_md5_info
*md5
;
737 md5
= (struct rip_md5_info
*)&packet
741 " family 0x%X type %d (MD5 authentication)",
745 " RIP-2 packet len %d Key ID %d"
747 ntohs(md5
->packet_len
),
748 md5
->keyid
, md5
->auth_len
);
749 zlog_debug(" Sequence Number %ld",
750 (unsigned long)ntohl(
752 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
753 p
= (uint8_t *)&rte
->prefix
;
756 " family 0x%X type %d (MD5 data)",
760 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
761 "%02X%02X%02X%02X%02X%02X%02X%02X",
762 p
[0], p
[1], p
[2], p
[3], p
[4],
763 p
[5], p
[6], p
[7], p
[8], p
[9],
764 p
[10], p
[11], p
[12], p
[13],
768 " family 0x%X type %d (Unknown auth type)",
774 " %s/%d -> %s family %d tag %" ROUTE_TAG_PRI
776 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
,
779 inet_ntop(AF_INET
, &rte
->nexthop
, nbuf
,
782 (route_tag_t
)ntohs(rte
->tag
),
783 (unsigned long)ntohl(rte
->metric
));
786 " %s family %d tag %" ROUTE_TAG_PRI
788 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
, BUFSIZ
),
790 (route_tag_t
)ntohs(rte
->tag
),
791 (unsigned long)ntohl(rte
->metric
));
796 /* Check if the destination address is valid (unicast; not net 0
797 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
798 check net 0 because we accept default route. */
799 static int rip_destination_check(struct in_addr addr
)
801 uint32_t destination
;
803 /* Convert to host byte order. */
804 destination
= ntohl(addr
.s_addr
);
806 if (IPV4_NET127(destination
))
809 /* Net 0 may match to the default route. */
810 if (IPV4_NET0(destination
) && destination
!= 0)
813 /* Unicast address must belong to class A, B, C. */
814 if (IN_CLASSA(destination
))
816 if (IN_CLASSB(destination
))
818 if (IN_CLASSC(destination
))
824 /* RIP version 2 authentication. */
825 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
826 struct interface
*ifp
)
828 struct rip_interface
*ri
;
829 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
832 /* reject passwords with zeros in the middle of the string */
833 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
834 if (auth_str
[i
] != '\0')
838 if (IS_RIP_DEBUG_EVENT
)
839 zlog_debug("RIPv2 simple password authentication from %s",
840 inet_ntoa(from
->sin_addr
));
844 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
845 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
848 /* Simple password authentication. */
850 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
854 struct keychain
*keychain
;
857 keychain
= keychain_lookup(ri
->key_chain
);
858 if (keychain
== NULL
|| keychain
->key
== NULL
)
861 key
= key_match_for_accept(keychain
, auth_str
);
868 /* RIP version 2 authentication with MD5. */
869 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
870 int length
, struct interface
*ifp
)
872 struct rip_interface
*ri
;
873 struct rip_md5_info
*md5
;
874 struct rip_md5_data
*md5data
;
875 struct keychain
*keychain
;
877 #ifdef CRYPTO_OPENSSL
879 #elif CRYPTO_INTERNAL
882 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
884 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
886 if (IS_RIP_DEBUG_EVENT
)
887 zlog_debug("RIPv2 MD5 authentication from %s",
888 inet_ntoa(from
->sin_addr
));
891 md5
= (struct rip_md5_info
*)&packet
->rte
;
893 /* Check auth type. */
894 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
897 /* If the authentication length is less than 16, then it must be wrong
899 * any interpretation of rfc2082. Some implementations also interpret
900 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
901 * RIP_AUTH_MD5_COMPAT_SIZE.
903 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
904 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
905 if (IS_RIP_DEBUG_EVENT
)
907 "RIPv2 MD5 authentication, strange authentication "
913 /* grab and verify check packet length */
914 packet_len
= ntohs(md5
->packet_len
);
916 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
917 if (IS_RIP_DEBUG_EVENT
)
919 "RIPv2 MD5 authentication, packet length field %d "
920 "greater than received length %d!",
921 md5
->packet_len
, length
);
925 /* retrieve authentication data */
926 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
929 keychain
= keychain_lookup(ri
->key_chain
);
930 if (keychain
== NULL
)
933 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
934 if (key
== NULL
|| key
->string
== NULL
)
937 strlcpy(auth_str
, key
->string
, sizeof(auth_str
));
938 } else if (ri
->auth_str
)
939 strlcpy(auth_str
, ri
->auth_str
, sizeof(auth_str
));
941 if (auth_str
[0] == 0)
944 /* MD5 digest authentication. */
945 #ifdef CRYPTO_OPENSSL
946 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
947 ctx
= EVP_MD_CTX_new();
948 EVP_DigestInit(ctx
, EVP_md5());
949 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
950 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
951 EVP_DigestFinal(ctx
, digest
, &md5_size
);
952 EVP_MD_CTX_free(ctx
);
953 #elif CRYPTO_INTERNAL
954 memset(&ctx
, 0, sizeof(ctx
));
956 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
957 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
958 MD5Final(digest
, &ctx
);
961 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
967 /* Pick correct auth string for sends, prepare auth_str buffer for use.
968 * (left justified and padded).
970 * presumes one of ri or key is valid, and that the auth strings they point
971 * to are nul terminated. If neither are present, auth_str will be fully
975 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
976 char *auth_str
, int len
)
980 memset(auth_str
, 0, len
);
981 if (key
&& key
->string
)
982 strlcpy(auth_str
, key
->string
, len
);
983 else if (ri
->auth_str
)
984 strlcpy(auth_str
, ri
->auth_str
, len
);
989 /* Write RIPv2 simple password authentication information
991 * auth_str is presumed to be 2 bytes and correctly prepared
992 * (left justified and zero padded).
994 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
996 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
998 stream_putw(s
, RIP_FAMILY_AUTH
);
999 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
1000 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
1005 /* write RIPv2 MD5 "authentication header"
1006 * (uses the auth key data field)
1008 * Digest offset field is set to 0.
1010 * returns: offset of the digest offset field, which must be set when
1011 * length to the auth-data MD5 digest is known.
1013 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1018 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1020 /* MD5 authentication. */
1021 stream_putw(s
, RIP_FAMILY_AUTH
);
1022 stream_putw(s
, RIP_AUTH_MD5
);
1024 /* MD5 AH digest offset field.
1026 * Set to placeholder value here, to true value when RIP-2 Packet length
1027 * is known. Actual value is set in .....().
1029 doff
= stream_get_endp(s
);
1034 stream_putc(s
, key
->index
% 256);
1038 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1039 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1041 * to be configurable.
1043 stream_putc(s
, ri
->md5_auth_len
);
1045 /* Sequence Number (non-decreasing). */
1046 /* RFC2080: The value used in the sequence number is
1047 arbitrary, but two suggestions are the time of the
1048 message's creation or a simple message counter. */
1049 stream_putl(s
, time(NULL
));
1051 /* Reserved field must be zero. */
1058 /* If authentication is in used, write the appropriate header
1059 * returns stream offset to which length must later be written
1060 * or 0 if this is not required
1062 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1063 struct key
*key
, char *auth_str
, int len
)
1065 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1067 switch (ri
->auth_type
) {
1068 case RIP_AUTH_SIMPLE_PASSWORD
:
1069 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1070 rip_auth_simple_write(s
, auth_str
, len
);
1073 return rip_auth_md5_ah_write(s
, ri
, key
);
1079 /* Write RIPv2 MD5 authentication data trailer */
1080 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1081 size_t doff
, char *auth_str
, int authlen
)
1084 #ifdef CRYPTO_OPENSSL
1086 #elif CRYPTO_INTERNAL
1089 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1091 /* Make it sure this interface is configured as MD5
1093 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1094 && (authlen
== RIP_AUTH_MD5_SIZE
));
1097 /* Get packet length. */
1098 len
= stream_get_endp(s
);
1100 /* Check packet length. */
1101 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1104 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1109 /* Set the digest offset length in the header */
1110 stream_putw_at(s
, doff
, len
);
1112 /* Set authentication data. */
1113 stream_putw(s
, RIP_FAMILY_AUTH
);
1114 stream_putw(s
, RIP_AUTH_DATA
);
1116 /* Generate a digest for the RIP packet. */
1117 #ifdef CRYPTO_OPENSSL
1118 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1119 ctx
= EVP_MD_CTX_new();
1120 EVP_DigestInit(ctx
, EVP_md5());
1121 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1122 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1123 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1124 EVP_MD_CTX_free(ctx
);
1125 #elif CRYPTO_INTERNAL
1126 memset(&ctx
, 0, sizeof(ctx
));
1128 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1129 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1130 MD5Final(digest
, &ctx
);
1133 /* Copy the digest to the packet. */
1134 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1137 /* RIP routing information. */
1138 static void rip_response_process(struct rip_packet
*packet
, int size
,
1139 struct sockaddr_in
*from
,
1140 struct connected
*ifc
)
1142 struct rip_interface
*ri
= ifc
->ifp
->info
;
1143 struct rip
*rip
= ri
->rip
;
1146 struct prefix_ipv4 ifaddr
;
1147 struct prefix_ipv4 ifaddrclass
;
1150 memset(&ifaddr
, 0, sizeof(ifaddr
));
1151 /* We don't know yet. */
1154 /* The Response must be ignored if it is not from the RIP
1155 port. (RFC2453 - Sec. 3.9.2)*/
1156 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1157 zlog_info("response doesn't come from RIP port: %d",
1159 rip_peer_bad_packet(rip
, from
);
1163 /* The datagram's IPv4 source address should be checked to see
1164 whether the datagram is from a valid neighbor; the source of the
1165 datagram must be on a directly connected network (RFC2453 - Sec.
1167 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1171 "This datagram doesn't came from a valid neighbor: %s",
1172 inet_ntoa(from
->sin_addr
));
1173 rip_peer_bad_packet(rip
, from
);
1177 /* It is also worth checking to see whether the response is from one
1178 of the router's own addresses. */
1180 ; /* Alredy done in rip_read () */
1182 /* Update RIP peer. */
1183 rip_peer_update(rip
, from
, packet
->version
);
1185 /* Set RTE pointer. */
1188 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1189 /* RIPv2 authentication check. */
1190 /* If the Address Family Identifier of the first (and only the
1191 first) entry in the message is 0xFFFF, then the remainder of
1192 the entry contains the authentication. */
1193 /* If the packet gets here it means authentication enabled */
1194 /* Check is done in rip_read(). So, just skipping it */
1195 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1196 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1199 if (rte
->family
!= htons(AF_INET
)) {
1200 /* Address family check. RIP only supports AF_INET. */
1201 zlog_info("Unsupported family %d from %s.",
1203 inet_ntoa(from
->sin_addr
));
1207 /* - is the destination address valid (e.g., unicast; not net 0
1209 if (!rip_destination_check(rte
->prefix
)) {
1211 "Network is net 0 or net 127 or it is not unicast network");
1212 rip_peer_bad_route(rip
, from
);
1216 /* Convert metric value to host byte order. */
1217 rte
->metric
= ntohl(rte
->metric
);
1219 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1220 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1221 zlog_info("Route's metric is not in the 1-16 range.");
1222 rip_peer_bad_route(rip
, from
);
1226 /* RIPv1 does not have nexthop value. */
1227 if (packet
->version
== RIPv1
1228 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1229 zlog_info("RIPv1 packet with nexthop value %s",
1230 inet_ntoa(rte
->nexthop
));
1231 rip_peer_bad_route(rip
, from
);
1235 /* That is, if the provided information is ignored, a possibly
1236 sub-optimal, but absolutely valid, route may be taken. If
1237 the received Next Hop is not directly reachable, it should be
1238 treated as 0.0.0.0. */
1239 if (packet
->version
== RIPv2
1240 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1243 /* Multicast address check. */
1244 addrval
= ntohl(rte
->nexthop
.s_addr
);
1245 if (IN_CLASSD(addrval
)) {
1247 "Nexthop %s is multicast address, skip this rte",
1248 inet_ntoa(rte
->nexthop
));
1252 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1253 rip
->vrf
->vrf_id
)) {
1254 struct route_node
*rn
;
1255 struct rip_info
*rinfo
;
1257 rn
= route_node_match_ipv4(rip
->table
,
1263 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1266 if (IS_RIP_DEBUG_EVENT
)
1268 "Next hop %s is on RIP network. Set nexthop to the packet's originator",
1271 rte
->nexthop
= rinfo
->from
;
1273 if (IS_RIP_DEBUG_EVENT
)
1275 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1278 rte
->nexthop
.s_addr
=
1282 route_unlock_node(rn
);
1284 if (IS_RIP_DEBUG_EVENT
)
1286 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1289 rte
->nexthop
.s_addr
= INADDR_ANY
;
1294 /* For RIPv1, there won't be a valid netmask.
1296 This is a best guess at the masks. If everyone was using old
1297 Ciscos before the 'ip subnet zero' option, it would be almost
1300 Cisco summarize ripv1 advertisements to the classful boundary
1301 (/16 for class B's) except when the RIP packet does to inside
1302 the classful network in question. */
1304 if ((packet
->version
== RIPv1
1305 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1306 || (packet
->version
== RIPv2
1307 && (rte
->prefix
.s_addr
!= INADDR_ANY
1308 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1309 uint32_t destination
;
1311 if (subnetted
== -1) {
1312 memcpy(&ifaddr
, ifc
->address
,
1313 sizeof(struct prefix_ipv4
));
1314 memcpy(&ifaddrclass
, &ifaddr
,
1315 sizeof(struct prefix_ipv4
));
1316 apply_classful_mask_ipv4(&ifaddrclass
);
1318 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1322 destination
= ntohl(rte
->prefix
.s_addr
);
1324 if (IN_CLASSA(destination
))
1325 masklen2ip(8, &rte
->mask
);
1326 else if (IN_CLASSB(destination
))
1327 masklen2ip(16, &rte
->mask
);
1328 else if (IN_CLASSC(destination
))
1329 masklen2ip(24, &rte
->mask
);
1332 masklen2ip(ifaddrclass
.prefixlen
,
1333 (struct in_addr
*)&destination
);
1334 if ((subnetted
== 1)
1335 && ((rte
->prefix
.s_addr
& destination
)
1336 == ifaddrclass
.prefix
.s_addr
)) {
1337 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1338 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1339 != rte
->prefix
.s_addr
)
1340 masklen2ip(32, &rte
->mask
);
1341 if (IS_RIP_DEBUG_EVENT
)
1342 zlog_debug("Subnetted route %s",
1343 inet_ntoa(rte
->prefix
));
1345 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1346 != rte
->prefix
.s_addr
)
1350 if (IS_RIP_DEBUG_EVENT
) {
1351 zlog_debug("Resultant route %s",
1352 inet_ntoa(rte
->prefix
));
1353 zlog_debug("Resultant mask %s",
1354 inet_ntoa(rte
->mask
));
1358 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1359 ignore the entry. */
1360 if ((packet
->version
== RIPv2
)
1361 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1362 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1363 != rte
->prefix
.s_addr
)) {
1365 "RIPv2 address %s is not mask /%d applied one",
1366 inet_ntoa(rte
->prefix
), ip_masklen(rte
->mask
));
1367 rip_peer_bad_route(rip
, from
);
1371 /* Default route's netmask is ignored. */
1372 if (packet
->version
== RIPv2
1373 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1374 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1375 if (IS_RIP_DEBUG_EVENT
)
1377 "Default route with non-zero netmask. Set zero to netmask");
1378 rte
->mask
.s_addr
= INADDR_ANY
;
1381 /* Routing table updates. */
1382 rip_rte_process(rte
, from
, ifc
->ifp
);
1386 /* Make socket for RIP protocol. */
1387 int rip_create_socket(struct vrf
*vrf
)
1391 struct sockaddr_in addr
;
1392 const char *vrf_dev
= NULL
;
1394 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1395 addr
.sin_family
= AF_INET
;
1396 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1397 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1398 addr
.sin_len
= sizeof(struct sockaddr_in
);
1399 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1400 /* sending port must always be the RIP port */
1401 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1403 /* Make datagram socket. */
1404 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1405 vrf_dev
= vrf
->name
;
1406 frr_with_privs(&ripd_privs
) {
1407 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1410 flog_err_sys(EC_LIB_SOCKET
,
1411 "Cannot create UDP socket: %s",
1412 safe_strerror(errno
));
1417 sockopt_broadcast(sock
);
1418 sockopt_reuseaddr(sock
);
1419 sockopt_reuseport(sock
);
1420 setsockopt_ipv4_multicast_loop(sock
, 0);
1421 #ifdef IPTOS_PREC_INTERNETCONTROL
1422 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1424 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1426 frr_with_privs(&ripd_privs
) {
1427 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1429 zlog_err("%s: Can't bind socket %d to %s port %d: %s",
1430 __func__
, sock
, inet_ntoa(addr
.sin_addr
),
1431 (int)ntohs(addr
.sin_port
),
1432 safe_strerror(errno
));
1442 /* RIP packet send to destination address, on interface denoted by
1443 * by connected argument. NULL to argument denotes destination should be
1444 * should be RIP multicast group
1446 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1447 struct connected
*ifc
)
1449 struct rip_interface
*ri
;
1452 struct sockaddr_in sin
;
1456 struct cmsghdr
*cmsgptr
;
1457 char adata
[256] = {};
1458 struct in_pktinfo
*pkt
;
1459 #endif /* GNU_LINUX */
1461 assert(ifc
!= NULL
);
1462 ri
= ifc
->ifp
->info
;
1465 if (IS_RIP_DEBUG_PACKET
) {
1466 #define ADDRESS_SIZE 20
1467 char dst
[ADDRESS_SIZE
];
1470 strlcpy(dst
, inet_ntoa(to
->sin_addr
), sizeof(dst
));
1472 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1473 strlcpy(dst
, inet_ntoa(sin
.sin_addr
), sizeof(dst
));
1476 zlog_debug("rip_send_packet %s > %s (%s)",
1477 inet_ntoa(ifc
->address
->u
.prefix4
), dst
,
1481 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1483 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1485 * with multiple addresses on the same subnet: the first address
1486 * on the subnet is configured "primary", and all subsequent
1488 * on that subnet are treated as "secondary" addresses.
1489 * In order to avoid routing-table bloat on other rip listeners,
1490 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1492 * XXX Since Linux is the only system for which the
1493 * ZEBRA_IFA_SECONDARY
1494 * flag is set, we would end up sending a packet for a
1496 * source address on non-linux systems.
1498 if (IS_RIP_DEBUG_PACKET
)
1499 zlog_debug("duplicate dropped");
1503 /* Make destination address. */
1504 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1505 sin
.sin_family
= AF_INET
;
1506 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1507 sin
.sin_len
= sizeof(struct sockaddr_in
);
1508 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1510 /* When destination is specified, use it's port and address. */
1512 sin
.sin_port
= to
->sin_port
;
1513 sin
.sin_addr
= to
->sin_addr
;
1515 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1516 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1518 rip_interface_multicast_set(rip
->sock
, ifc
);
1521 memset(&msg
, 0, sizeof(msg
));
1522 msg
.msg_name
= (void *)&sin
;
1523 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1530 msg
.msg_control
= (void *)adata
;
1531 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1533 cmsgptr
= (struct cmsghdr
*)adata
;
1534 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1535 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1536 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1537 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1538 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1539 #endif /* GNU_LINUX */
1541 ret
= sendmsg(rip
->sock
, &msg
, 0);
1543 if (IS_RIP_DEBUG_EVENT
)
1544 zlog_debug("SEND to %s.%d", inet_ntoa(sin
.sin_addr
),
1545 ntohs(sin
.sin_port
));
1548 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1553 /* Add redistributed route to RIP table. */
1554 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1555 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1556 unsigned int metric
, unsigned char distance
,
1560 struct route_node
*rp
= NULL
;
1561 struct rip_info
*rinfo
= NULL
, newinfo
;
1562 struct list
*list
= NULL
;
1564 /* Redistribute route */
1565 ret
= rip_destination_check(p
->prefix
);
1569 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1571 memset(&newinfo
, 0, sizeof(struct rip_info
));
1572 newinfo
.type
= type
;
1573 newinfo
.sub_type
= sub_type
;
1575 newinfo
.external_metric
= metric
;
1576 newinfo
.distance
= distance
;
1577 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1582 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1583 rinfo
= listgetdata(listhead(list
));
1585 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1586 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1587 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1588 route_unlock_node(rp
);
1592 /* Manually configured RIP route check. */
1593 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1594 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1595 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1596 if (type
!= ZEBRA_ROUTE_RIP
1597 || ((sub_type
!= RIP_ROUTE_STATIC
)
1598 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1599 route_unlock_node(rp
);
1604 (void)rip_ecmp_replace(rip
, &newinfo
);
1605 route_unlock_node(rp
);
1607 (void)rip_ecmp_add(rip
, &newinfo
);
1609 if (IS_RIP_DEBUG_EVENT
) {
1610 zlog_debug("Redistribute new prefix %s/%d",
1611 inet_ntoa(p
->prefix
), p
->prefixlen
);
1614 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1617 /* Delete redistributed route from RIP table. */
1618 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1619 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1622 struct route_node
*rp
;
1623 struct rip_info
*rinfo
;
1625 ret
= rip_destination_check(p
->prefix
);
1629 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1631 struct list
*list
= rp
->info
;
1633 if (list
!= NULL
&& listcount(list
) != 0) {
1634 rinfo
= listgetdata(listhead(list
));
1635 if (rinfo
!= NULL
&& rinfo
->type
== type
1636 && rinfo
->sub_type
== sub_type
1637 && rinfo
->nh
.ifindex
== ifindex
) {
1638 /* Perform poisoned reverse. */
1639 rinfo
->metric
= RIP_METRIC_INFINITY
;
1640 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1641 rip_garbage_collect
,
1643 RIP_TIMER_OFF(rinfo
->t_timeout
);
1644 rinfo
->flags
|= RIP_RTF_CHANGED
;
1646 if (IS_RIP_DEBUG_EVENT
)
1648 "Poison %s/%d on the interface %s with an "
1649 "infinity metric [delete]",
1650 inet_ntoa(p
->prefix
),
1656 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1659 route_unlock_node(rp
);
1663 /* Response to request called from rip_read ().*/
1664 static void rip_request_process(struct rip_packet
*packet
, int size
,
1665 struct sockaddr_in
*from
, struct connected
*ifc
)
1670 struct prefix_ipv4 p
;
1671 struct route_node
*rp
;
1672 struct rip_info
*rinfo
;
1673 struct rip_interface
*ri
;
1675 /* Does not reponse to the requests on the loopback interfaces */
1676 if (if_is_loopback(ifc
->ifp
))
1679 /* Check RIP process is enabled on this interface. */
1680 ri
= ifc
->ifp
->info
;
1685 /* When passive interface is specified, suppress responses */
1689 /* RIP peer update. */
1690 rip_peer_update(rip
, from
, packet
->version
);
1692 lim
= ((caddr_t
)packet
) + size
;
1695 /* The Request is processed entry by entry. If there are no
1696 entries, no response is given. */
1697 if (lim
== (caddr_t
)rte
)
1700 /* There is one special case. If there is exactly one entry in the
1701 request, and it has an address family identifier of zero and a
1702 metric of infinity (i.e., 16), then this is a request to send the
1703 entire routing table. */
1704 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1705 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1706 /* All route with split horizon */
1707 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1709 if (ntohs(rte
->family
) != AF_INET
)
1712 /* Examine the list of RTEs in the Request one by one. For each
1713 entry, look up the destination in the router's routing
1714 database and, if there is a route, put that route's metric in
1715 the metric field of the RTE. If there is no explicit route
1716 to the specified destination, put infinity in the metric
1717 field. Once all the entries have been filled in, change the
1718 command from Request to Response and send the datagram back
1719 to the requestor. */
1722 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1723 p
.prefix
= rte
->prefix
;
1724 p
.prefixlen
= ip_masklen(rte
->mask
);
1725 apply_mask_ipv4(&p
);
1727 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1729 rinfo
= listgetdata(
1730 listhead((struct list
*)rp
->info
));
1731 rte
->metric
= htonl(rinfo
->metric
);
1732 route_unlock_node(rp
);
1734 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1736 packet
->command
= RIP_RESPONSE
;
1738 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1740 rip
->counters
.queries
++;
1743 /* First entry point of RIP packet. */
1744 static int rip_read(struct thread
*t
)
1746 struct rip
*rip
= THREAD_ARG(t
);
1750 union rip_buf rip_buf
;
1751 struct rip_packet
*packet
;
1752 struct sockaddr_in from
;
1756 struct interface
*ifp
= NULL
;
1757 struct connected
*ifc
;
1758 struct rip_interface
*ri
;
1761 /* Fetch socket then register myself. */
1762 sock
= THREAD_FD(t
);
1765 /* Add myself to tne next event */
1766 rip_event(rip
, RIP_READ
, sock
);
1768 /* RIPd manages only IPv4. */
1769 memset(&from
, 0, sizeof(struct sockaddr_in
));
1770 fromlen
= sizeof(struct sockaddr_in
);
1772 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1773 (struct sockaddr
*)&from
, &fromlen
);
1775 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1776 safe_strerror(errno
));
1780 /* Check is this packet comming from myself? */
1781 if (if_check_address(rip
, from
.sin_addr
)) {
1782 if (IS_RIP_DEBUG_PACKET
)
1783 zlog_debug("ignore packet comes from myself (VRF %s)",
1788 /* Which interface is this packet comes from. */
1789 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1794 /* RIP packet received */
1795 if (IS_RIP_DEBUG_EVENT
)
1796 zlog_debug("RECV packet from %s port %d on %s (VRF %s)",
1797 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1798 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1800 /* If this packet come from unknown interface, ignore it. */
1803 "rip_read: cannot find interface for packet from %s port %d (VRF %s)",
1804 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1810 p
.u
.prefix4
= from
.sin_addr
;
1811 p
.prefixlen
= IPV4_MAX_BITLEN
;
1813 ifc
= connected_lookup_prefix(ifp
, &p
);
1817 "rip_read: cannot find connected address for packet from %s "
1818 "port %d on interface %s (VRF %s)",
1819 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1820 ifp
->name
, rip
->vrf_name
);
1824 /* Packet length check. */
1825 if (len
< RIP_PACKET_MINSIZ
) {
1826 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1828 rip_peer_bad_packet(rip
, &from
);
1831 if (len
> RIP_PACKET_MAXSIZ
) {
1832 zlog_warn("packet size %d is larger than max size %d", len
,
1834 rip_peer_bad_packet(rip
, &from
);
1838 /* Packet alignment check. */
1839 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1840 zlog_warn("packet size %d is wrong for RIP packet alignment",
1842 rip_peer_bad_packet(rip
, &from
);
1846 /* Set RTE number. */
1847 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1849 /* For easy to handle. */
1850 packet
= &rip_buf
.rip_packet
;
1852 /* RIP version check. */
1853 if (packet
->version
== 0) {
1854 zlog_info("version 0 with command %d received.",
1856 rip_peer_bad_packet(rip
, &from
);
1860 /* Dump RIP packet. */
1861 if (IS_RIP_DEBUG_RECV
)
1862 rip_packet_dump(packet
, len
, "RECV");
1864 /* RIP version adjust. This code should rethink now. RFC1058 says
1865 that "Version 1 implementations are to ignore this extra data and
1866 process only the fields specified in this document.". So RIPv3
1867 packet should be treated as RIPv1 ignoring must be zero field. */
1868 if (packet
->version
> RIPv2
)
1869 packet
->version
= RIPv2
;
1871 /* Is RIP running or is this RIP neighbor ?*/
1873 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1874 if (IS_RIP_DEBUG_EVENT
)
1875 zlog_debug("RIP is not enabled on interface %s.",
1877 rip_peer_bad_packet(rip
, &from
);
1881 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1882 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1884 if (vrecv
== RI_RIP_VERSION_NONE
1885 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1886 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1887 if (IS_RIP_DEBUG_PACKET
)
1889 " packet's v%d doesn't fit to if version spec",
1891 rip_peer_bad_packet(rip
, &from
);
1895 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1896 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1897 accepted; authenticated RIP-2 messages shall be discarded. */
1898 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1899 && (packet
->version
== RIPv2
)
1900 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1901 if (IS_RIP_DEBUG_EVENT
)
1903 "packet RIPv%d is dropped because authentication disabled",
1905 ripd_notif_send_auth_type_failure(ifp
->name
);
1906 rip_peer_bad_packet(rip
, &from
);
1911 If the router is configured to authenticate RIP-2 messages, then
1912 RIP-1 messages and RIP-2 messages which pass authentication
1913 testing shall be accepted; unauthenticated and failed
1914 authentication RIP-2 messages shall be discarded. For maximum
1915 security, RIP-1 messages should be ignored when authentication is
1916 in use (see section 4.1); otherwise, the routing information from
1917 authenticated messages will be propagated by RIP-1 routers in an
1918 unauthenticated manner.
1920 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1921 * always reply regardless of authentication settings, because:
1923 * - if there other authorised routers on-link, the REQUESTor can
1924 * passively obtain the routing updates anyway
1925 * - if there are no other authorised routers on-link, RIP can
1926 * easily be disabled for the link to prevent giving out information
1927 * on state of this routers RIP routing table..
1929 * I.e. if RIPv1 has any place anymore these days, it's as a very
1930 * simple way to distribute routing information (e.g. to embedded
1931 * hosts / appliances) and the ability to give out RIPv1
1932 * routing-information freely, while still requiring RIPv2
1933 * authentication for any RESPONSEs might be vaguely useful.
1935 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1936 /* Discard RIPv1 messages other than REQUESTs */
1937 if (packet
->command
!= RIP_REQUEST
) {
1938 if (IS_RIP_DEBUG_PACKET
)
1941 " dropped because authentication enabled");
1942 ripd_notif_send_auth_type_failure(ifp
->name
);
1943 rip_peer_bad_packet(rip
, &from
);
1946 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1947 const char *auth_desc
;
1950 /* There definitely is no authentication in the packet.
1952 if (IS_RIP_DEBUG_PACKET
)
1954 "RIPv2 authentication failed: no auth RTE in packet");
1955 ripd_notif_send_auth_type_failure(ifp
->name
);
1956 rip_peer_bad_packet(rip
, &from
);
1960 /* First RTE must be an Authentication Family RTE */
1961 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1962 if (IS_RIP_DEBUG_PACKET
)
1965 " dropped because authentication enabled");
1966 ripd_notif_send_auth_type_failure(ifp
->name
);
1967 rip_peer_bad_packet(rip
, &from
);
1971 /* Check RIPv2 authentication. */
1972 switch (ntohs(packet
->rte
->tag
)) {
1973 case RIP_AUTH_SIMPLE_PASSWORD
:
1974 auth_desc
= "simple";
1975 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1980 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1981 /* Reset RIP packet length to trim MD5 data. */
1987 auth_desc
= "unknown type";
1988 if (IS_RIP_DEBUG_PACKET
)
1990 "RIPv2 Unknown authentication type %d",
1991 ntohs(packet
->rte
->tag
));
1995 if (IS_RIP_DEBUG_PACKET
)
1996 zlog_debug("RIPv2 %s authentication success",
1999 if (IS_RIP_DEBUG_PACKET
)
2000 zlog_debug("RIPv2 %s authentication failure",
2002 ripd_notif_send_auth_failure(ifp
->name
);
2003 rip_peer_bad_packet(rip
, &from
);
2008 /* Process each command. */
2009 switch (packet
->command
) {
2011 rip_response_process(packet
, len
, &from
, ifc
);
2015 rip_request_process(packet
, len
, &from
, ifc
);
2020 "Obsolete command %s received, please sent it to routed",
2021 lookup_msg(rip_msg
, packet
->command
, NULL
));
2022 rip_peer_bad_packet(rip
, &from
);
2024 case RIP_POLL_ENTRY
:
2025 zlog_info("Obsolete command %s received",
2026 lookup_msg(rip_msg
, packet
->command
, NULL
));
2027 rip_peer_bad_packet(rip
, &from
);
2030 zlog_info("Unknown RIP command %d received", packet
->command
);
2031 rip_peer_bad_packet(rip
, &from
);
2038 /* Write routing table entry to the stream and return next index of
2039 the routing table entry in the stream. */
2040 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2041 uint8_t version
, struct rip_info
*rinfo
)
2043 struct in_addr mask
;
2045 /* Write routing table entry. */
2046 if (version
== RIPv1
) {
2047 stream_putw(s
, AF_INET
);
2049 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2050 stream_put_ipv4(s
, 0);
2051 stream_put_ipv4(s
, 0);
2052 stream_putl(s
, rinfo
->metric_out
);
2054 masklen2ip(p
->prefixlen
, &mask
);
2056 stream_putw(s
, AF_INET
);
2057 stream_putw(s
, rinfo
->tag_out
);
2058 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2059 stream_put_ipv4(s
, mask
.s_addr
);
2060 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2061 stream_putl(s
, rinfo
->metric_out
);
2067 /* Send update to the ifp or spcified neighbor. */
2068 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2069 int route_type
, uint8_t version
)
2074 struct route_node
*rp
;
2075 struct rip_info
*rinfo
;
2076 struct rip_interface
*ri
;
2077 struct prefix_ipv4
*p
;
2078 struct prefix_ipv4 classfull
;
2079 struct prefix_ipv4 ifaddrclass
;
2080 struct key
*key
= NULL
;
2081 /* this might need to made dynamic if RIP ever supported auth methods
2082 with larger key string sizes */
2083 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2084 size_t doff
= 0; /* offset of digest offset field */
2088 struct list
*list
= NULL
;
2089 struct listnode
*listnode
= NULL
;
2091 /* Logging output event. */
2092 if (IS_RIP_DEBUG_EVENT
) {
2094 zlog_debug("update routes to neighbor %s",
2095 inet_ntoa(to
->sin_addr
));
2097 zlog_debug("update routes on interface %s ifindex %d",
2098 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2101 /* Get RIP interface. */
2102 ri
= ifc
->ifp
->info
;
2105 /* Set output stream. */
2108 /* Reset stream and RTE counter. */
2110 rtemax
= RIP_MAX_RTE
;
2112 /* If output interface is in simple password authentication mode, we
2113 need space for authentication data. */
2114 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2117 /* If output interface is in MD5 authentication mode, we need space
2118 for authentication header and data. */
2119 if (ri
->auth_type
== RIP_AUTH_MD5
)
2122 /* If output interface is in simple password authentication mode
2123 and string or keychain is specified we need space for auth. data */
2124 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2125 if (ri
->key_chain
) {
2126 struct keychain
*keychain
;
2128 keychain
= keychain_lookup(ri
->key_chain
);
2130 key
= key_lookup_for_send(keychain
);
2132 /* to be passed to auth functions later */
2133 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2134 if (strlen(auth_str
) == 0)
2138 if (version
== RIPv1
) {
2139 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2140 apply_classful_mask_ipv4(&ifaddrclass
);
2142 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2146 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2147 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2148 rinfo
= listgetdata(listhead(list
));
2149 /* For RIPv1, if we are subnetted, output subnets in our
2151 /* that have the same mask as the output "interface".
2153 /* networks, only the classfull version is output. */
2155 if (version
== RIPv1
) {
2156 p
= (struct prefix_ipv4
*)&rp
->p
;
2158 if (IS_RIP_DEBUG_PACKET
)
2160 "RIPv1 mask check, %s/%d considered for output",
2161 inet_ntoa(rp
->p
.u
.prefix4
),
2166 (struct prefix
*)&ifaddrclass
,
2168 if ((ifc
->address
->prefixlen
2170 && (rp
->p
.prefixlen
!= 32))
2173 memcpy(&classfull
, &rp
->p
,
2174 sizeof(struct prefix_ipv4
));
2175 apply_classful_mask_ipv4(&classfull
);
2176 if (rp
->p
.u
.prefix4
.s_addr
!= 0
2177 && classfull
.prefixlen
2181 if (IS_RIP_DEBUG_PACKET
)
2183 "RIPv1 mask check, %s/%d made it through",
2184 inet_ntoa(rp
->p
.u
.prefix4
),
2187 p
= (struct prefix_ipv4
*)&rp
->p
;
2189 /* Apply output filters. */
2190 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2194 /* Changed route only output. */
2195 if (route_type
== rip_changed_route
2196 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2199 /* Split horizon. */
2200 /* if (split_horizon == rip_split_horizon) */
2201 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2203 * We perform split horizon for RIP and
2205 * For rip routes, we want to suppress the route
2207 * end up sending the route back on the
2209 * learned it from, with a higher metric. For
2211 * we suppress the route if the prefix is a
2213 * source address that we are going to use for
2215 * (in order to handle the case when multiple
2217 * configured on the same interface).
2220 struct rip_info
*tmp_rinfo
= NULL
;
2221 struct connected
*tmp_ifc
= NULL
;
2223 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2225 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2226 && tmp_rinfo
->nh
.ifindex
2227 == ifc
->ifp
->ifindex
) {
2233 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2234 for (ALL_LIST_ELEMENTS_RO(
2235 ifc
->ifp
->connected
,
2239 tmp_ifc
->address
)) {
2249 /* Preparation for route-map. */
2250 rinfo
->metric_set
= 0;
2251 rinfo
->nexthop_out
.s_addr
= 0;
2252 rinfo
->metric_out
= rinfo
->metric
;
2253 rinfo
->tag_out
= rinfo
->tag
;
2254 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2256 /* In order to avoid some local loops,
2257 * if the RIP route has a nexthop via this interface,
2259 * otherwise set it to 0. The nexthop should not be
2261 * beyond the local broadcast/multicast area in order
2262 * to avoid an IGP multi-level recursive look-up.
2265 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2266 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2268 /* Interface route-map */
2269 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2270 ret
= route_map_apply(
2271 ri
->routemap
[RIP_FILTER_OUT
],
2272 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2274 if (ret
== RMAP_DENYMATCH
) {
2275 if (IS_RIP_DEBUG_PACKET
)
2277 "RIP %s/%d is filtered by route-map out",
2278 inet_ntoa(p
->prefix
),
2284 /* Apply redistribute route map - continue, if deny */
2285 if (rip
->redist
[rinfo
->type
].route_map
.name
2286 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2287 ret
= route_map_apply(
2288 rip
->redist
[rinfo
->type
].route_map
.map
,
2289 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2291 if (ret
== RMAP_DENYMATCH
) {
2292 if (IS_RIP_DEBUG_PACKET
)
2294 "%s/%d is filtered by route-map",
2295 inet_ntoa(p
->prefix
),
2301 /* When route-map does not set metric. */
2302 if (!rinfo
->metric_set
) {
2303 /* If redistribute metric is set. */
2304 if (rip
->redist
[rinfo
->type
].metric_config
2305 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2307 rip
->redist
[rinfo
->type
].metric
;
2309 /* If the route is not connected or
2311 one, use default-metric value*/
2312 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2314 != ZEBRA_ROUTE_CONNECT
2316 != RIP_METRIC_INFINITY
)
2318 rip
->default_metric
;
2322 /* Apply offset-list */
2323 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2324 rip_offset_list_apply_out(p
, ifc
->ifp
,
2325 &rinfo
->metric_out
);
2327 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2328 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2330 /* Perform split-horizon with poisoned reverse
2331 * for RIP and connected routes.
2333 if (ri
->split_horizon
2334 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2336 * We perform split horizon for RIP and
2338 * For rip routes, we want to suppress the route
2340 * end up sending the route back on the
2342 * learned it from, with a higher metric. For
2344 * we suppress the route if the prefix is a
2346 * source address that we are going to use for
2348 * (in order to handle the case when multiple
2350 * configured on the same interface).
2352 struct rip_info
*tmp_rinfo
= NULL
;
2353 struct connected
*tmp_ifc
= NULL
;
2355 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2357 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2358 && tmp_rinfo
->nh
.ifindex
2359 == ifc
->ifp
->ifindex
)
2361 RIP_METRIC_INFINITY
;
2363 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2364 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2365 for (ALL_LIST_ELEMENTS_RO(
2366 ifc
->ifp
->connected
,
2370 tmp_ifc
->address
)) {
2372 RIP_METRIC_INFINITY
;
2378 /* Prepare preamble, auth headers, if needs be */
2380 stream_putc(s
, RIP_RESPONSE
);
2381 stream_putc(s
, version
);
2384 /* auth header for !v1 && !no_auth */
2385 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2386 && (version
!= RIPv1
))
2387 doff
= rip_auth_header_write(
2388 s
, ri
, key
, auth_str
,
2389 RIP_AUTH_SIMPLE_SIZE
);
2392 /* Write RTE to the stream. */
2393 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2394 if (num
== rtemax
) {
2395 if (version
== RIPv2
2396 && ri
->auth_type
== RIP_AUTH_MD5
)
2397 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2398 RIP_AUTH_SIMPLE_SIZE
);
2400 ret
= rip_send_packet(STREAM_DATA(s
),
2401 stream_get_endp(s
), to
,
2404 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2405 rip_packet_dump((struct rip_packet
*)
2414 /* Flush unwritten RTE. */
2416 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2417 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2418 RIP_AUTH_SIMPLE_SIZE
);
2420 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2423 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2424 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2425 stream_get_endp(s
), "SEND");
2429 /* Statistics updates. */
2433 /* Send RIP packet to the interface. */
2434 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2437 struct interface
*ifp
= ifc
->ifp
;
2438 struct rip_interface
*ri
= ifp
->info
;
2439 struct sockaddr_in to
;
2441 /* When RIP version is 2 and multicast enable interface. */
2442 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2443 if (IS_RIP_DEBUG_EVENT
)
2444 zlog_debug("multicast announce on %s ", ifp
->name
);
2446 rip_output_process(ifc
, NULL
, route_type
, version
);
2450 /* If we can't send multicast packet, send it with unicast. */
2451 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2452 if (ifc
->address
->family
== AF_INET
) {
2453 /* Destination address and port setting. */
2454 memset(&to
, 0, sizeof(struct sockaddr_in
));
2455 if (ifc
->destination
)
2456 /* use specified broadcast or peer destination
2458 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2459 else if (ifc
->address
->prefixlen
< IPV4_MAX_PREFIXLEN
)
2460 /* calculate the appropriate broadcast address
2462 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2463 ifc
->address
->u
.prefix4
.s_addr
,
2464 ifc
->address
->prefixlen
);
2466 /* do not know where to send the packet */
2468 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2470 if (IS_RIP_DEBUG_EVENT
)
2471 zlog_debug("%s announce to %s on %s",
2472 CONNECTED_PEER(ifc
) ? "unicast"
2474 inet_ntoa(to
.sin_addr
), ifp
->name
);
2476 rip_output_process(ifc
, &to
, route_type
, version
);
2481 /* Update send to all interface and neighbor. */
2482 static void rip_update_process(struct rip
*rip
, int route_type
)
2484 struct listnode
*ifnode
, *ifnnode
;
2485 struct connected
*connected
;
2486 struct interface
*ifp
;
2487 struct rip_interface
*ri
;
2488 struct route_node
*rp
;
2489 struct sockaddr_in to
;
2492 /* Send RIP update to each interface. */
2493 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2494 if (if_is_loopback(ifp
))
2497 if (!if_is_operative(ifp
))
2500 /* Fetch RIP interface information. */
2503 /* When passive interface is specified, suppress announce to the
2510 * If there is no version configuration in the
2512 * use rip's version setting.
2514 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2518 if (IS_RIP_DEBUG_EVENT
)
2519 zlog_debug("SEND UPDATE to %s ifindex %d",
2520 ifp
->name
, ifp
->ifindex
);
2522 /* send update on each connected network */
2523 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2525 if (connected
->address
->family
== AF_INET
) {
2527 rip_update_interface(
2531 && if_is_multicast(ifp
))
2532 rip_update_interface(
2540 /* RIP send updates to each neighbor. */
2541 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2542 if (rp
->info
!= NULL
) {
2545 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2549 "Neighbor %s doesn't have connected interface!",
2550 inet_ntoa(p
->u
.prefix4
));
2554 /* Set destination address and port */
2555 memset(&to
, 0, sizeof(struct sockaddr_in
));
2556 to
.sin_addr
= p
->u
.prefix4
;
2557 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2559 /* RIP version is rip's configuration. */
2560 rip_output_process(connected
, &to
, route_type
,
2565 /* RIP's periodical timer. */
2566 static int rip_update(struct thread
*t
)
2568 struct rip
*rip
= THREAD_ARG(t
);
2570 /* Clear timer pointer. */
2571 rip
->t_update
= NULL
;
2573 if (IS_RIP_DEBUG_EVENT
)
2574 zlog_debug("update timer fire!");
2576 /* Process update output. */
2577 rip_update_process(rip
, rip_all_route
);
2579 /* Triggered updates may be suppressed if a regular update is due by
2580 the time the triggered update would be sent. */
2581 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2584 /* Register myself. */
2585 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2590 /* Walk down the RIP routing table then clear changed flag. */
2591 static void rip_clear_changed_flag(struct rip
*rip
)
2593 struct route_node
*rp
;
2594 struct rip_info
*rinfo
= NULL
;
2595 struct list
*list
= NULL
;
2596 struct listnode
*listnode
= NULL
;
2598 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2599 if ((list
= rp
->info
) != NULL
)
2600 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2601 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2602 /* This flag can be set only on the first entry.
2608 /* Triggered update interval timer. */
2609 static int rip_triggered_interval(struct thread
*t
)
2611 struct rip
*rip
= THREAD_ARG(t
);
2613 rip
->t_triggered_interval
= NULL
;
2617 rip_triggered_update(t
);
2622 /* Execute triggered update. */
2623 static int rip_triggered_update(struct thread
*t
)
2625 struct rip
*rip
= THREAD_ARG(t
);
2628 /* Clear thred pointer. */
2629 rip
->t_triggered_update
= NULL
;
2631 /* Cancel interval timer. */
2632 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2635 /* Logging triggered update. */
2636 if (IS_RIP_DEBUG_EVENT
)
2637 zlog_debug("triggered update!");
2639 /* Split Horizon processing is done when generating triggered
2640 updates as well as normal updates (see section 2.6). */
2641 rip_update_process(rip
, rip_changed_route
);
2643 /* Once all of the triggered updates have been generated, the route
2644 change flags should be cleared. */
2645 rip_clear_changed_flag(rip
);
2647 /* After a triggered update is sent, a timer should be set for a
2648 random interval between 1 and 5 seconds. If other changes that
2649 would trigger updates occur before the timer expires, a single
2650 update is triggered when the timer expires. */
2651 interval
= (frr_weak_random() % 5) + 1;
2653 rip
->t_triggered_interval
= NULL
;
2654 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2655 &rip
->t_triggered_interval
);
2660 /* Withdraw redistributed route. */
2661 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2663 struct route_node
*rp
;
2664 struct rip_info
*rinfo
= NULL
;
2665 struct list
*list
= NULL
;
2667 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2668 if ((list
= rp
->info
) != NULL
) {
2669 rinfo
= listgetdata(listhead(list
));
2670 if (rinfo
->type
== type
2671 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2672 /* Perform poisoned reverse. */
2673 rinfo
->metric
= RIP_METRIC_INFINITY
;
2674 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2675 rip_garbage_collect
,
2677 RIP_TIMER_OFF(rinfo
->t_timeout
);
2678 rinfo
->flags
|= RIP_RTF_CHANGED
;
2680 if (IS_RIP_DEBUG_EVENT
) {
2681 struct prefix_ipv4
*p
=
2682 (struct prefix_ipv4
*)&rp
->p
;
2685 "Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2686 inet_ntoa(p
->prefix
),
2693 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2698 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2702 vrf
= vrf_lookup_by_id(vrf_id
);
2709 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2713 rip
.vrf_name
= (char *)vrf_name
;
2715 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2718 /* Create new RIP instance and set it to global variable. */
2719 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2723 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2724 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2726 /* Set initial value. */
2727 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2728 rip
->default_metric
=
2729 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2731 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2732 rip
->passive_default
=
2733 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2734 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2736 rip
->timeout_time
= yang_get_default_uint32(
2737 "%s/timers/holddown-interval", RIP_INSTANCE
);
2738 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2741 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2743 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2745 /* Initialize RIP data structures. */
2746 rip
->table
= route_table_init();
2747 route_table_set_info(rip
->table
, rip
);
2748 rip
->neighbor
= route_table_init();
2749 rip
->peer_list
= list_new();
2750 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2751 rip
->peer_list
->del
= rip_peer_list_del
;
2752 rip
->distance_table
= route_table_init();
2753 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2754 rip
->enable_interface
= vector_init(1);
2755 rip
->enable_network
= route_table_init();
2756 rip
->passive_nondefault
= vector_init(1);
2757 rip
->offset_list_master
= list_new();
2758 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2759 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2761 /* Distribute list install. */
2762 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2763 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2764 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2766 /* if rmap install. */
2767 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2768 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2769 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2771 /* Make output stream. */
2772 rip
->obuf
= stream_new(1500);
2774 /* Enable the routing instance if possible. */
2775 if (vrf
&& vrf_is_enabled(vrf
))
2776 rip_instance_enable(rip
, vrf
, socket
);
2782 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2787 /* Sned RIP request to the destination. */
2788 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2789 uint8_t version
, struct connected
*connected
)
2792 struct rip_packet rip_packet
;
2793 struct listnode
*node
, *nnode
;
2795 memset(&rip_packet
, 0, sizeof(rip_packet
));
2797 rip_packet
.command
= RIP_REQUEST
;
2798 rip_packet
.version
= version
;
2799 rte
= rip_packet
.rte
;
2800 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2804 * connected is only sent for ripv1 case, or when
2805 * interface does not support multicast. Caller loops
2806 * over each connected address for this case.
2808 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2810 != sizeof(rip_packet
))
2813 return sizeof(rip_packet
);
2816 /* send request on each connected network */
2817 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2818 struct prefix_ipv4
*p
;
2820 p
= (struct prefix_ipv4
*)connected
->address
;
2822 if (p
->family
!= AF_INET
)
2825 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2827 != sizeof(rip_packet
))
2830 return sizeof(rip_packet
);
2833 static int rip_update_jitter(unsigned long time
)
2835 #define JITTER_BOUND 4
2836 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2837 Given that, we cannot let time be less than JITTER_BOUND seconds.
2838 The RIPv2 RFC says jitter should be small compared to
2839 update_time. We consider 1/JITTER_BOUND to be small.
2842 int jitter_input
= time
;
2845 if (jitter_input
< JITTER_BOUND
)
2846 jitter_input
= JITTER_BOUND
;
2848 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2851 return jitter
/ JITTER_BOUND
;
2854 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2861 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2863 case RIP_UPDATE_EVENT
:
2864 RIP_TIMER_OFF(rip
->t_update
);
2865 jitter
= rip_update_jitter(rip
->update_time
);
2866 thread_add_timer(master
, rip_update
, rip
,
2867 sock
? 2 : rip
->update_time
+ jitter
,
2870 case RIP_TRIGGERED_UPDATE
:
2871 if (rip
->t_triggered_interval
)
2874 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2875 &rip
->t_triggered_update
);
2884 rip_update_default_metric (void)
2886 struct route_node
*np
;
2887 struct rip_info
*rinfo
= NULL
;
2888 struct list
*list
= NULL
;
2889 struct listnode
*listnode
= NULL
;
2891 for (np
= route_top (rip
->table
); np
; np
= route_next (np
))
2892 if ((list
= np
->info
) != NULL
)
2893 for (ALL_LIST_ELEMENTS_RO (list
, listnode
, rinfo
))
2894 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&& rinfo
->type
!= ZEBRA_ROUTE_CONNECT
)
2895 rinfo
->metric
= rip
->default_metric
;
2899 struct rip_distance
*rip_distance_new(void)
2901 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2904 void rip_distance_free(struct rip_distance
*rdistance
)
2906 if (rdistance
->access_list
)
2907 free(rdistance
->access_list
);
2908 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2911 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2912 struct route_node
*node
)
2914 struct rip_distance
*rdistance
;
2916 rdistance
= node
->info
;
2918 rip_distance_free(rdistance
);
2921 /* Apply RIP information to distance method. */
2922 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2924 struct route_node
*rn
;
2925 struct prefix_ipv4 p
;
2926 struct rip_distance
*rdistance
;
2927 struct access_list
*alist
;
2929 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2931 p
.prefix
= rinfo
->from
;
2932 p
.prefixlen
= IPV4_MAX_BITLEN
;
2934 /* Check source address. */
2935 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2937 rdistance
= rn
->info
;
2938 route_unlock_node(rn
);
2940 if (rdistance
->access_list
) {
2941 alist
= access_list_lookup(AFI_IP
,
2942 rdistance
->access_list
);
2945 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2949 return rdistance
->distance
;
2951 return rdistance
->distance
;
2955 return rip
->distance
;
2960 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2962 struct route_node
*rn
;
2963 struct rip_distance
*rdistance
;
2967 vty_out(vty
, " Distance: (default is %u)\n",
2968 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2970 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2971 if ((rdistance
= rn
->info
) != NULL
) {
2974 " Address Distance List\n");
2977 snprintf(buf
, sizeof(buf
), "%s/%d",
2978 inet_ntoa(rn
->p
.u
.prefix4
), rn
->p
.prefixlen
);
2979 vty_out(vty
, " %-20s %4d %s\n", buf
,
2980 rdistance
->distance
,
2981 rdistance
->access_list
? rdistance
->access_list
2986 /* Update ECMP routes to zebra when ECMP is disabled. */
2987 void rip_ecmp_disable(struct rip
*rip
)
2989 struct route_node
*rp
;
2990 struct rip_info
*rinfo
, *tmp_rinfo
;
2992 struct listnode
*node
, *nextnode
;
2994 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2995 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2996 rinfo
= listgetdata(listhead(list
));
2997 if (!rip_route_rte(rinfo
))
3000 /* Drop all other entries, except the first one. */
3001 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
3002 if (tmp_rinfo
!= rinfo
) {
3003 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
3005 tmp_rinfo
->t_garbage_collect
);
3006 list_delete_node(list
, node
);
3007 rip_info_free(tmp_rinfo
);
3011 rip_zebra_ipv4_add(rip
, rp
);
3013 /* Set the route change flag. */
3014 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
3016 /* Signal the output process to trigger an update. */
3017 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
3021 /* Print out routes update time. */
3022 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
3027 char timebuf
[TIME_BUF
];
3028 struct thread
*thread
;
3030 if ((thread
= rinfo
->t_timeout
) != NULL
) {
3031 clock
= thread_timer_remain_second(thread
);
3032 gmtime_r(&clock
, &tm
);
3033 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
3034 vty_out(vty
, "%5s", timebuf
);
3035 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
3036 clock
= thread_timer_remain_second(thread
);
3037 gmtime_r(&clock
, &tm
);
3038 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
3039 vty_out(vty
, "%5s", timebuf
);
3043 static const char *rip_route_type_print(int sub_type
)
3048 case RIP_ROUTE_STATIC
:
3050 case RIP_ROUTE_DEFAULT
:
3052 case RIP_ROUTE_REDISTRIBUTE
:
3054 case RIP_ROUTE_INTERFACE
:
3063 "show ip rip [vrf NAME]",
3070 struct route_node
*np
;
3071 struct rip_info
*rinfo
= NULL
;
3072 struct list
*list
= NULL
;
3073 struct listnode
*listnode
= NULL
;
3074 const char *vrf_name
;
3077 if (argv_find(argv
, argc
, "vrf", &idx
))
3078 vrf_name
= argv
[idx
+ 1]->arg
;
3080 vrf_name
= VRF_DEFAULT_NAME
;
3082 rip
= rip_lookup_by_vrf_name(vrf_name
);
3084 vty_out(vty
, "%% RIP instance not found\n");
3087 if (!rip
->enabled
) {
3088 vty_out(vty
, "%% RIP instance is disabled\n");
3093 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3095 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3096 " (i) - interface\n\n"
3097 " Network Next Hop Metric From Tag Time\n");
3099 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3100 if ((list
= np
->info
) != NULL
)
3101 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3105 vty
, "%c(%s) %s/%d",
3106 /* np->lock, For debugging. */
3107 zebra_route_char(rinfo
->type
),
3108 rip_route_type_print(rinfo
->sub_type
),
3109 inet_ntoa(np
->p
.u
.prefix4
),
3115 vty_out(vty
, "%*s", len
, " ");
3117 switch (rinfo
->nh
.type
) {
3118 case NEXTHOP_TYPE_IPV4
:
3119 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3120 vty_out(vty
, "%-20s %2d ",
3121 inet_ntoa(rinfo
->nh
.gate
.ipv4
),
3124 case NEXTHOP_TYPE_IFINDEX
:
3129 case NEXTHOP_TYPE_BLACKHOLE
:
3134 case NEXTHOP_TYPE_IPV6
:
3135 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3137 "V6 Address Hidden %2d ",
3142 /* Route which exist in kernel routing table. */
3143 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3144 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3145 vty_out(vty
, "%-15s ",
3146 inet_ntoa(rinfo
->from
));
3147 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3148 (route_tag_t
)rinfo
->tag
);
3149 rip_vty_out_uptime(vty
, rinfo
);
3150 } else if (rinfo
->metric
3151 == RIP_METRIC_INFINITY
) {
3152 vty_out(vty
, "self ");
3153 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3154 (route_tag_t
)rinfo
->tag
);
3155 rip_vty_out_uptime(vty
, rinfo
);
3157 if (rinfo
->external_metric
) {
3159 vty
, "self (%s:%d)",
3162 rinfo
->external_metric
);
3165 vty_out(vty
, "%*s", len
,
3170 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3171 (route_tag_t
)rinfo
->tag
);
3179 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3180 DEFUN (show_ip_rip_status
,
3181 show_ip_rip_status_cmd
,
3182 "show ip rip [vrf NAME] status",
3187 "IP routing protocol process parameters and statistics\n")
3190 struct interface
*ifp
;
3191 struct rip_interface
*ri
;
3192 extern const struct message ri_version_msg
[];
3193 const char *send_version
;
3194 const char *receive_version
;
3195 const char *vrf_name
;
3198 if (argv_find(argv
, argc
, "vrf", &idx
))
3199 vrf_name
= argv
[idx
+ 1]->arg
;
3201 vrf_name
= VRF_DEFAULT_NAME
;
3203 rip
= rip_lookup_by_vrf_name(vrf_name
);
3205 vty_out(vty
, "%% RIP instance not found\n");
3208 if (!rip
->enabled
) {
3209 vty_out(vty
, "%% RIP instance is disabled\n");
3213 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3214 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3216 vty_out(vty
, " next due in %lu seconds\n",
3217 thread_timer_remain_second(rip
->t_update
));
3218 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3219 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3221 /* Filtering status show. */
3222 config_show_distribute(vty
, rip
->distribute_ctx
);
3224 /* Default metric information. */
3225 vty_out(vty
, " Default redistribution metric is %u\n",
3226 rip
->default_metric
);
3228 /* Redistribute information. */
3229 vty_out(vty
, " Redistributing:");
3230 rip_show_redistribute_config(vty
, rip
);
3233 vty_out(vty
, " Default version control: send version %s,",
3234 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3235 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3236 vty_out(vty
, " receive any version \n");
3238 vty_out(vty
, " receive version %s \n",
3239 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3241 vty_out(vty
, " Interface Send Recv Key-chain\n");
3243 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3249 if (ri
->enable_network
|| ri
->enable_interface
) {
3250 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3252 lookup_msg(ri_version_msg
,
3253 rip
->version_send
, NULL
);
3255 send_version
= lookup_msg(ri_version_msg
,
3258 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3260 lookup_msg(ri_version_msg
,
3261 rip
->version_recv
, NULL
);
3263 receive_version
= lookup_msg(
3264 ri_version_msg
, ri
->ri_receive
, NULL
);
3266 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3267 send_version
, receive_version
,
3268 ri
->key_chain
? ri
->key_chain
: "");
3272 vty_out(vty
, " Routing for Networks:\n");
3273 rip_show_network_config(vty
, rip
);
3276 int found_passive
= 0;
3277 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3280 if ((ri
->enable_network
|| ri
->enable_interface
)
3282 if (!found_passive
) {
3284 " Passive Interface(s):\n");
3287 vty_out(vty
, " %s\n", ifp
->name
);
3292 vty_out(vty
, " Routing Information Sources:\n");
3294 " Gateway BadPackets BadRoutes Distance Last Update\n");
3295 rip_peer_display(vty
, rip
);
3297 rip_distance_show(vty
, rip
);
3302 /* RIP configuration write function. */
3303 static int config_write_rip(struct vty
*vty
)
3308 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3309 char xpath
[XPATH_MAXLEN
];
3310 struct lyd_node
*dnode
;
3312 snprintf(xpath
, sizeof(xpath
),
3313 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3315 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3318 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3320 /* Distribute configuration. */
3321 config_write_distribute(vty
, rip
->distribute_ctx
);
3323 /* Interface routemap configuration */
3324 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3332 static int config_write_rip(struct vty
*vty
);
3333 /* RIP node structure. */
3334 static struct cmd_node rip_node
= {
3337 .parent_node
= CONFIG_NODE
,
3338 .prompt
= "%s(config-router)# ",
3339 .config_write
= config_write_rip
,
3342 /* Distribute-list update functions. */
3343 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3344 struct distribute
*dist
)
3346 struct interface
*ifp
;
3347 struct rip_interface
*ri
;
3348 struct access_list
*alist
;
3349 struct prefix_list
*plist
;
3351 if (!ctx
->vrf
|| !dist
->ifname
)
3354 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3360 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3361 alist
= access_list_lookup(AFI_IP
,
3362 dist
->list
[DISTRIBUTE_V4_IN
]);
3364 ri
->list
[RIP_FILTER_IN
] = alist
;
3366 ri
->list
[RIP_FILTER_IN
] = NULL
;
3368 ri
->list
[RIP_FILTER_IN
] = NULL
;
3370 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3371 alist
= access_list_lookup(AFI_IP
,
3372 dist
->list
[DISTRIBUTE_V4_OUT
]);
3374 ri
->list
[RIP_FILTER_OUT
] = alist
;
3376 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3378 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3380 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3381 plist
= prefix_list_lookup(AFI_IP
,
3382 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3384 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3386 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3388 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3390 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3391 plist
= prefix_list_lookup(AFI_IP
,
3392 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3394 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3396 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3398 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3401 void rip_distribute_update_interface(struct interface
*ifp
)
3403 struct rip_interface
*ri
= ifp
->info
;
3404 struct rip
*rip
= ri
->rip
;
3405 struct distribute
*dist
;
3409 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3411 rip_distribute_update(rip
->distribute_ctx
, dist
);
3414 /* Update all interface's distribute list. */
3416 static void rip_distribute_update_all(struct prefix_list
*notused
)
3418 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3419 struct interface
*ifp
;
3421 FOR_ALL_INTERFACES (vrf
, ifp
)
3422 rip_distribute_update_interface(ifp
);
3425 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3427 rip_distribute_update_all(NULL
);
3430 /* Delete all added rip route. */
3431 void rip_clean(struct rip
*rip
)
3434 rip_instance_disable(rip
);
3436 stream_free(rip
->obuf
);
3438 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3439 if (rip
->redist
[i
].route_map
.name
)
3440 free(rip
->redist
[i
].route_map
.name
);
3442 route_table_finish(rip
->table
);
3443 route_table_finish(rip
->neighbor
);
3444 list_delete(&rip
->peer_list
);
3445 distribute_list_delete(&rip
->distribute_ctx
);
3446 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3448 rip_clean_network(rip
);
3449 rip_passive_nondefault_clean(rip
);
3450 vector_free(rip
->enable_interface
);
3451 route_table_finish(rip
->enable_network
);
3452 vector_free(rip
->passive_nondefault
);
3453 list_delete(&rip
->offset_list_master
);
3454 rip_interfaces_clean(rip
);
3455 route_table_finish(rip
->distance_table
);
3457 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3458 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3459 XFREE(MTYPE_RIP
, rip
);
3462 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3463 struct if_rmap
*if_rmap
)
3465 struct interface
*ifp
= NULL
;
3466 struct rip_interface
*ri
;
3467 struct route_map
*rmap
;
3468 struct vrf
*vrf
= NULL
;
3471 vrf
= vrf_lookup_by_name(ctx
->name
);
3473 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3478 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3479 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3481 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3483 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3485 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3487 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3488 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3490 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3492 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3494 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3497 void rip_if_rmap_update_interface(struct interface
*ifp
)
3499 struct rip_interface
*ri
= ifp
->info
;
3500 struct rip
*rip
= ri
->rip
;
3501 struct if_rmap
*if_rmap
;
3502 struct if_rmap_ctx
*ctx
;
3506 ctx
= rip
->if_rmap_ctx
;
3509 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3511 rip_if_rmap_update(ctx
, if_rmap
);
3514 static void rip_routemap_update_redistribute(struct rip
*rip
)
3516 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3517 if (rip
->redist
[i
].route_map
.name
) {
3518 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3519 rip
->redist
[i
].route_map
.name
);
3520 route_map_counter_increment(
3521 rip
->redist
[i
].route_map
.map
);
3527 static void rip_routemap_update(const char *notused
)
3529 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3531 struct interface
*ifp
;
3533 FOR_ALL_INTERFACES (vrf
, ifp
)
3534 rip_if_rmap_update_interface(ifp
);
3538 rip_routemap_update_redistribute(rip
);
3541 /* Link RIP instance to VRF. */
3542 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3544 struct interface
*ifp
;
3547 rip
->distribute_ctx
->vrf
= vrf
;
3550 FOR_ALL_INTERFACES (vrf
, ifp
)
3551 rip_interface_sync(ifp
);
3554 /* Unlink RIP instance from VRF. */
3555 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3557 struct interface
*ifp
;
3560 rip
->distribute_ctx
->vrf
= NULL
;
3563 FOR_ALL_INTERFACES (vrf
, ifp
)
3564 rip_interface_sync(ifp
);
3567 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3571 rip_vrf_link(rip
, vrf
);
3572 rip
->enabled
= true;
3574 /* Resend all redistribute requests. */
3575 rip_redistribute_enable(rip
);
3577 /* Create read and timer thread. */
3578 rip_event(rip
, RIP_READ
, rip
->sock
);
3579 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3581 rip_zebra_vrf_register(vrf
);
3584 static void rip_instance_disable(struct rip
*rip
)
3586 struct vrf
*vrf
= rip
->vrf
;
3587 struct route_node
*rp
;
3589 /* Clear RIP routes */
3590 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3591 struct rip_info
*rinfo
;
3593 struct listnode
*listnode
;
3595 if ((list
= rp
->info
) == NULL
)
3598 rinfo
= listgetdata(listhead(list
));
3599 if (rip_route_rte(rinfo
))
3600 rip_zebra_ipv4_delete(rip
, rp
);
3602 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3603 RIP_TIMER_OFF(rinfo
->t_timeout
);
3604 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3605 rip_info_free(rinfo
);
3609 route_unlock_node(rp
);
3612 /* Flush all redistribute requests. */
3613 rip_redistribute_disable(rip
);
3615 /* Cancel RIP related timers. */
3616 RIP_TIMER_OFF(rip
->t_update
);
3617 RIP_TIMER_OFF(rip
->t_triggered_update
);
3618 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3620 /* Cancel read thread. */
3621 THREAD_READ_OFF(rip
->t_read
);
3623 /* Close RIP socket. */
3627 /* Clear existing peers. */
3628 list_delete_all_node(rip
->peer_list
);
3630 rip_zebra_vrf_deregister(vrf
);
3632 rip_vrf_unlink(rip
, vrf
);
3633 rip
->enabled
= false;
3636 static int rip_vrf_new(struct vrf
*vrf
)
3638 if (IS_RIP_DEBUG_EVENT
)
3639 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3645 static int rip_vrf_delete(struct vrf
*vrf
)
3647 if (IS_RIP_DEBUG_EVENT
)
3648 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3654 static int rip_vrf_enable(struct vrf
*vrf
)
3659 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3661 char *old_vrf_name
= NULL
;
3663 rip
= (struct rip
*)vrf
->info
;
3666 /* update vrf name */
3668 old_vrf_name
= rip
->vrf_name
;
3669 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf
->name
);
3671 * HACK: Change the RIP VRF in the running configuration directly,
3672 * bypassing the northbound layer. This is necessary to avoid deleting
3673 * the RIP and readding it in the new VRF, which would have
3674 * several implications.
3676 if (yang_module_find("frr-ripd") && old_vrf_name
) {
3677 struct lyd_node
*rip_dnode
;
3679 rip_dnode
= yang_dnode_get(
3680 running_config
->dnode
,
3681 "/frr-ripd:ripd/instance[vrf='%s']/vrf",
3684 yang_dnode_change_leaf(rip_dnode
, vrf
->name
);
3685 running_config
->version
++;
3688 XFREE(MTYPE_RIP_VRF_NAME
, old_vrf_name
);
3690 if (!rip
|| rip
->enabled
)
3693 if (IS_RIP_DEBUG_EVENT
)
3694 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3697 /* Activate the VRF RIP instance. */
3698 if (!rip
->enabled
) {
3699 socket
= rip_create_socket(vrf
);
3703 rip_instance_enable(rip
, vrf
, socket
);
3709 static int rip_vrf_disable(struct vrf
*vrf
)
3713 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3714 if (!rip
|| !rip
->enabled
)
3717 if (IS_RIP_DEBUG_EVENT
)
3718 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3721 /* Deactivate the VRF RIP instance. */
3723 rip_instance_disable(rip
);
3728 void rip_vrf_init(void)
3730 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3734 void rip_vrf_terminate(void)
3739 /* Allocate new rip structure and set default value. */
3742 /* Install top nodes. */
3743 install_node(&rip_node
);
3745 /* Install rip commands. */
3746 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3747 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3749 install_default(RIP_NODE
);
3751 /* Debug related init. */
3754 /* Access list install. */
3756 access_list_add_hook(rip_distribute_update_all_wrapper
);
3757 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3759 /* Prefix list initialize.*/
3761 prefix_list_add_hook(rip_distribute_update_all
);
3762 prefix_list_delete_hook(rip_distribute_update_all
);
3764 /* Distribute list install. */
3765 distribute_list_init(RIP_NODE
);
3768 rip_route_map_init();
3770 route_map_add_hook(rip_routemap_update
);
3771 route_map_delete_hook(rip_routemap_update
);
3773 if_rmap_init(RIP_NODE
);