1 /* RIP version 1 and 2.
2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h"
39 #include "distribute.h"
40 #ifdef CRYPTO_INTERNAL
45 #include "lib_errors.h"
46 #include "northbound_cli.h"
48 #include "lib/printfrr.h"
50 #include "ripd/ripd.h"
51 #include "ripd/rip_nb.h"
52 #include "ripd/rip_debug.h"
53 #include "ripd/rip_errors.h"
54 #include "ripd/rip_interface.h"
56 /* UDP receive buffer size */
57 #define RIP_UDP_RCV_BUF 41600
59 DEFINE_MGROUP(RIPD
, "ripd");
60 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure");
61 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name");
62 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info");
63 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance");
66 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
68 static int rip_triggered_update(struct thread
*);
69 static int rip_update_jitter(unsigned long);
70 static void rip_distance_table_node_cleanup(struct route_table
*table
,
71 struct route_node
*node
);
72 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
73 static void rip_instance_disable(struct rip
*rip
);
75 static void rip_distribute_update(struct distribute_ctx
*ctx
,
76 struct distribute
*dist
);
78 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
79 struct if_rmap
*if_rmap
);
81 /* RIP output routes type. */
82 enum { rip_all_route
, rip_changed_route
};
84 /* RIP command strings. */
85 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
86 {RIP_RESPONSE
, "RESPONSE"},
87 {RIP_TRACEON
, "TRACEON"},
88 {RIP_TRACEOFF
, "TRACEOFF"},
90 {RIP_POLL_ENTRY
, "POLL ENTRY"},
93 /* Generate rb-tree of RIP instances. */
94 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
96 return strcmp(a
->vrf_name
, b
->vrf_name
);
98 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
100 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
102 /* Utility function to set broadcast option to the socket. */
103 static int sockopt_broadcast(int sock
)
108 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
111 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
117 int rip_route_rte(struct rip_info
*rinfo
)
119 return (rinfo
->type
== ZEBRA_ROUTE_RIP
120 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
123 static struct rip_info
*rip_info_new(void)
125 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
128 void rip_info_free(struct rip_info
*rinfo
)
130 XFREE(MTYPE_RIP_INFO
, rinfo
);
133 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
135 return route_table_get_info(rinfo
->rp
->table
);
138 /* RIP route garbage collect timer. */
139 static int rip_garbage_collect(struct thread
*t
)
141 struct rip_info
*rinfo
;
142 struct route_node
*rp
;
144 rinfo
= THREAD_ARG(t
);
146 /* Off timeout timer. */
147 RIP_TIMER_OFF(rinfo
->t_timeout
);
149 /* Get route_node pointer. */
152 /* Unlock route_node. */
153 listnode_delete(rp
->info
, rinfo
);
154 if (list_isempty((struct list
*)rp
->info
)) {
155 list_delete((struct list
**)&rp
->info
);
156 route_unlock_node(rp
);
159 /* Free RIP routing information. */
160 rip_info_free(rinfo
);
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("%pFX filtered by distribute %s", p
,
345 if (ri
->prefix
[rip_distribute
]) {
346 if (prefix_list_apply(ri
->prefix
[rip_distribute
],
349 if (IS_RIP_DEBUG_PACKET
)
350 zlog_debug("%pFX filtered by prefix-list %s", p
,
356 /* All interface filter check. */
357 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
359 if (dist
->list
[distribute
]) {
360 alist
= access_list_lookup(AFI_IP
,
361 dist
->list
[distribute
]);
364 if (access_list_apply(alist
, (struct prefix
*)p
)
366 if (IS_RIP_DEBUG_PACKET
)
368 "%pFX filtered by distribute %s",
374 if (dist
->prefix
[distribute
]) {
375 plist
= prefix_list_lookup(AFI_IP
,
376 dist
->prefix
[distribute
]);
379 if (prefix_list_apply(plist
, (struct prefix
*)p
)
381 if (IS_RIP_DEBUG_PACKET
)
383 "%pFX filtered by prefix-list %s",
393 /* Check nexthop address validity. */
394 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
396 struct interface
*ifp
;
397 struct listnode
*cnode
;
398 struct connected
*ifc
;
401 /* If nexthop address matches local configured address then it is
404 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
405 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
408 if (p
->family
== AF_INET
409 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
416 /* RIP add route to routing table. */
417 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
418 struct interface
*ifp
)
422 struct prefix_ipv4 p
;
423 struct route_node
*rp
;
424 struct rip_info
*rinfo
= NULL
, newinfo
;
425 struct rip_interface
*ri
;
426 struct in_addr
*nexthop
;
428 unsigned char old_dist
, new_dist
;
429 struct list
*list
= NULL
;
430 struct listnode
*node
= NULL
;
432 /* Make prefix structure. */
433 memset(&p
, 0, sizeof(struct prefix_ipv4
));
435 p
.prefix
= rte
->prefix
;
436 p
.prefixlen
= ip_masklen(rte
->mask
);
438 /* Make sure mask is applied. */
444 /* Apply input filters. */
445 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
449 memset(&newinfo
, 0, sizeof(newinfo
));
450 newinfo
.type
= ZEBRA_ROUTE_RIP
;
451 newinfo
.sub_type
= RIP_ROUTE_RTE
;
452 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
453 newinfo
.from
= from
->sin_addr
;
454 newinfo
.nh
.ifindex
= ifp
->ifindex
;
455 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
456 newinfo
.metric
= rte
->metric
;
457 newinfo
.metric_out
= rte
->metric
; /* XXX */
458 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
460 /* Modify entry according to the interface routemap. */
461 if (ri
->routemap
[RIP_FILTER_IN
]) {
462 /* The object should be of the type of rip_info */
463 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
464 (struct prefix
*)&p
, &newinfo
);
466 if (ret
== RMAP_DENYMATCH
) {
467 if (IS_RIP_DEBUG_PACKET
)
469 "RIP %pFX is filtered by route-map in",
474 /* Get back the object */
475 rte
->nexthop
= newinfo
.nexthop_out
;
476 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
477 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
481 /* Once the entry has been validated, update the metric by
482 adding the cost of the network on which the message
483 arrived. If the result is greater than infinity, use infinity
484 (RFC2453 Sec. 3.9.2) */
485 /* Zebra ripd can handle offset-list in. */
486 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
488 /* If offset-list does not modify the metric use interface's
491 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
493 if (rte
->metric
> RIP_METRIC_INFINITY
)
494 rte
->metric
= RIP_METRIC_INFINITY
;
496 /* Set nexthop pointer. */
497 if (rte
->nexthop
.s_addr
== INADDR_ANY
)
498 nexthop
= &from
->sin_addr
;
500 nexthop
= &rte
->nexthop
;
502 /* Check if nexthop address is myself, then do nothing. */
503 if (rip_nexthop_check(rip
, nexthop
) < 0) {
504 if (IS_RIP_DEBUG_PACKET
)
505 zlog_debug("Nexthop address %pI4 is myself",
510 /* Get index for the prefix. */
511 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
514 newinfo
.nh
.gate
.ipv4
= *nexthop
;
515 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
516 newinfo
.metric
= rte
->metric
;
517 newinfo
.tag
= ntohs(rte
->tag
);
518 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
520 new_dist
= newinfo
.distance
? newinfo
.distance
521 : ZEBRA_RIP_DISTANCE_DEFAULT
;
523 /* Check to see whether there is already RIP route on the table. */
524 if ((list
= rp
->info
) != NULL
)
525 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
526 /* Need to compare with redistributed entry or local
528 if (!rip_route_rte(rinfo
))
531 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
532 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
535 if (!listnextnode(node
)) {
536 /* Not found in the list */
538 if (rte
->metric
> rinfo
->metric
) {
539 /* New route has a greater metric.
541 route_unlock_node(rp
);
545 if (rte
->metric
< rinfo
->metric
)
546 /* New route has a smaller metric.
547 * Replace the ECMP list
548 * with the new one in below. */
551 /* Metrics are same. We compare the distances.
553 old_dist
= rinfo
->distance
555 : ZEBRA_RIP_DISTANCE_DEFAULT
;
557 if (new_dist
> old_dist
) {
558 /* New route has a greater distance.
560 route_unlock_node(rp
);
564 if (new_dist
< old_dist
)
565 /* New route has a smaller distance.
566 * Replace the ECMP list
567 * with the new one in below. */
570 /* Metrics and distances are both same. Keep
572 * the new route is added in the ECMP list in
578 /* Local static route. */
579 if (rinfo
->type
== ZEBRA_ROUTE_RIP
580 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
581 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
582 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
583 route_unlock_node(rp
);
587 /* Redistributed route check. */
588 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
589 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
590 old_dist
= rinfo
->distance
;
591 /* Only routes directly connected to an interface
593 * may have a valid NULL distance */
594 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
597 : ZEBRA_RIP_DISTANCE_DEFAULT
;
598 /* If imported route does not have STRICT precedence,
599 mark it as a ghost */
600 if (new_dist
<= old_dist
601 && rte
->metric
!= RIP_METRIC_INFINITY
)
602 rip_ecmp_replace(rip
, &newinfo
);
604 route_unlock_node(rp
);
611 route_unlock_node(rp
);
613 /* Now, check to see whether there is already an explicit route
614 for the destination prefix. If there is no such route, add
615 this route to the routing table, unless the metric is
616 infinity (there is no point in adding a route which
618 if (rte
->metric
!= RIP_METRIC_INFINITY
)
619 rip_ecmp_add(rip
, &newinfo
);
621 /* Route is there but we are not sure the route is RIP or not.
624 /* If there is an existing route, compare the next hop address
625 to the address of the router from which the datagram came.
626 If this datagram is from the same router as the existing
627 route, reinitialize the timeout. */
628 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
629 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
631 old_dist
= rinfo
->distance
? rinfo
->distance
632 : ZEBRA_RIP_DISTANCE_DEFAULT
;
634 /* Next, compare the metrics. If the datagram is from the same
635 router as the existing route, and the new metric is different
636 than the old one; or, if the new metric is lower than the old
637 one, or if the tag has been changed; or if there is a route
638 with a lower administrave distance; or an update of the
639 distance on the actual route; do the following actions: */
640 if ((same
&& rinfo
->metric
!= rte
->metric
)
641 || (rte
->metric
< rinfo
->metric
)
642 || ((same
) && (rinfo
->metric
== rte
->metric
)
643 && (newinfo
.tag
!= rinfo
->tag
))
644 || (old_dist
> new_dist
)
645 || ((old_dist
!= new_dist
) && same
)) {
646 if (listcount(list
) == 1) {
647 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
648 rip_ecmp_replace(rip
, &newinfo
);
650 rip_ecmp_delete(rip
, rinfo
);
652 if (newinfo
.metric
< rinfo
->metric
)
653 rip_ecmp_replace(rip
, &newinfo
);
654 else if (newinfo
.metric
> rinfo
->metric
)
655 rip_ecmp_delete(rip
, rinfo
);
656 else if (new_dist
< old_dist
)
657 rip_ecmp_replace(rip
, &newinfo
);
658 else if (new_dist
> old_dist
)
659 rip_ecmp_delete(rip
, rinfo
);
661 int update
= CHECK_FLAG(rinfo
->flags
,
666 assert(newinfo
.metric
667 != RIP_METRIC_INFINITY
);
669 RIP_TIMER_OFF(rinfo
->t_timeout
);
670 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
671 memcpy(rinfo
, &newinfo
,
672 sizeof(struct rip_info
));
673 rip_timeout_update(rip
, rinfo
);
676 rip_zebra_ipv4_add(rip
, rp
);
678 /* - Set the route change flag on the
680 rinfo
= listgetdata(listhead(list
));
681 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
682 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
685 } else /* same & no change */
686 rip_timeout_update(rip
, rinfo
);
688 /* Unlock tempolary lock of the route. */
689 route_unlock_node(rp
);
693 /* Dump RIP packet */
694 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
699 const char *command_str
;
703 /* Set command string. */
704 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
705 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
707 command_str
= "unknown";
709 /* Dump packet header. */
710 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
711 packet
->version
, size
);
713 /* Dump each routing table entry. */
716 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
717 if (packet
->version
== RIPv2
) {
718 netmask
= ip_masklen(rte
->mask
);
720 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
722 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
723 p
= (uint8_t *)&rte
->prefix
;
726 " family 0x%X type %d auth string: %s",
729 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
730 struct rip_md5_info
*md5
;
732 md5
= (struct rip_md5_info
*)&packet
736 " family 0x%X type %d (MD5 authentication)",
740 " RIP-2 packet len %d Key ID %d Auth Data len %d",
741 ntohs(md5
->packet_len
),
742 md5
->keyid
, md5
->auth_len
);
743 zlog_debug(" Sequence Number %ld",
744 (unsigned long)ntohl(
746 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
747 p
= (uint8_t *)&rte
->prefix
;
750 " family 0x%X type %d (MD5 data)",
754 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
755 p
[0], p
[1], p
[2], p
[3], p
[4],
756 p
[5], p
[6], p
[7], p
[8], p
[9],
757 p
[10], p
[11], p
[12], p
[13],
761 " family 0x%X type %d (Unknown auth type)",
767 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
769 &rte
->prefix
, netmask
, &rte
->nexthop
,
771 (route_tag_t
)ntohs(rte
->tag
),
772 (unsigned long)ntohl(rte
->metric
));
774 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
776 &rte
->prefix
, ntohs(rte
->family
),
777 (route_tag_t
)ntohs(rte
->tag
),
778 (unsigned long)ntohl(rte
->metric
));
783 /* Check if the destination address is valid (unicast; not net 0
784 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
785 check net 0 because we accept default route. */
786 static int rip_destination_check(struct in_addr addr
)
788 uint32_t destination
;
790 /* Convert to host byte order. */
791 destination
= ntohl(addr
.s_addr
);
793 if (IPV4_NET127(destination
))
796 /* Net 0 may match to the default route. */
797 if (IPV4_NET0(destination
) && destination
!= 0)
800 /* Unicast address must belong to class A, B, C. */
801 if (IN_CLASSA(destination
))
803 if (IN_CLASSB(destination
))
805 if (IN_CLASSC(destination
))
811 /* RIP version 2 authentication. */
812 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
813 struct interface
*ifp
)
815 struct rip_interface
*ri
;
816 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
819 /* reject passwords with zeros in the middle of the string */
820 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
821 if (auth_str
[i
] != '\0')
825 if (IS_RIP_DEBUG_EVENT
)
826 zlog_debug("RIPv2 simple password authentication from %pI4",
831 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
832 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
835 /* Simple password authentication. */
837 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
841 struct keychain
*keychain
;
844 keychain
= keychain_lookup(ri
->key_chain
);
845 if (keychain
== NULL
|| keychain
->key
== NULL
)
848 key
= key_match_for_accept(keychain
, auth_str
);
855 /* RIP version 2 authentication with MD5. */
856 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
857 int length
, struct interface
*ifp
)
859 struct rip_interface
*ri
;
860 struct rip_md5_info
*md5
;
861 struct rip_md5_data
*md5data
;
862 struct keychain
*keychain
;
864 #ifdef CRYPTO_OPENSSL
866 #elif CRYPTO_INTERNAL
869 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
871 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
873 if (IS_RIP_DEBUG_EVENT
)
874 zlog_debug("RIPv2 MD5 authentication from %pI4",
878 md5
= (struct rip_md5_info
*)&packet
->rte
;
880 /* Check auth type. */
881 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
884 /* If the authentication length is less than 16, then it must be wrong
886 * any interpretation of rfc2082. Some implementations also interpret
887 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
888 * RIP_AUTH_MD5_COMPAT_SIZE.
890 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
891 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
892 if (IS_RIP_DEBUG_EVENT
)
894 "RIPv2 MD5 authentication, strange authentication length field %d",
899 /* grab and verify check packet length */
900 packet_len
= ntohs(md5
->packet_len
);
902 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
903 if (IS_RIP_DEBUG_EVENT
)
905 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
906 md5
->packet_len
, length
);
910 /* retrieve authentication data */
911 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
914 keychain
= keychain_lookup(ri
->key_chain
);
915 if (keychain
== NULL
)
918 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
919 if (key
== NULL
|| key
->string
== NULL
)
922 memcpy(auth_str
, key
->string
,
923 MIN(sizeof(auth_str
), strlen(key
->string
)));
924 } else if (ri
->auth_str
)
925 memcpy(auth_str
, ri
->auth_str
,
926 MIN(sizeof(auth_str
), strlen(ri
->auth_str
)));
928 if (auth_str
[0] == 0)
931 /* MD5 digest authentication. */
932 #ifdef CRYPTO_OPENSSL
933 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
934 ctx
= EVP_MD_CTX_new();
935 EVP_DigestInit(ctx
, EVP_md5());
936 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
937 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
938 EVP_DigestFinal(ctx
, digest
, &md5_size
);
939 EVP_MD_CTX_free(ctx
);
940 #elif CRYPTO_INTERNAL
941 memset(&ctx
, 0, sizeof(ctx
));
943 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
944 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
945 MD5Final(digest
, &ctx
);
948 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
954 /* Pick correct auth string for sends, prepare auth_str buffer for use.
955 * (left justified and padded).
957 * presumes one of ri or key is valid, and that the auth strings they point
958 * to are nul terminated. If neither are present, auth_str will be fully
962 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
963 char *auth_str
, int len
)
967 memset(auth_str
, 0, len
);
968 if (key
&& key
->string
)
969 memcpy(auth_str
, key
->string
,
970 MIN((size_t)len
, strlen(key
->string
)));
971 else if (ri
->auth_str
)
972 memcpy(auth_str
, ri
->auth_str
,
973 MIN((size_t)len
, strlen(ri
->auth_str
)));
978 /* Write RIPv2 simple password authentication information
980 * auth_str is presumed to be 2 bytes and correctly prepared
981 * (left justified and zero padded).
983 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
985 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
987 stream_putw(s
, RIP_FAMILY_AUTH
);
988 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
989 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
994 /* write RIPv2 MD5 "authentication header"
995 * (uses the auth key data field)
997 * Digest offset field is set to 0.
999 * returns: offset of the digest offset field, which must be set when
1000 * length to the auth-data MD5 digest is known.
1002 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1007 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1009 /* MD5 authentication. */
1010 stream_putw(s
, RIP_FAMILY_AUTH
);
1011 stream_putw(s
, RIP_AUTH_MD5
);
1013 /* MD5 AH digest offset field.
1015 * Set to placeholder value here, to true value when RIP-2 Packet length
1016 * is known. Actual value is set in .....().
1018 doff
= stream_get_endp(s
);
1023 stream_putc(s
, key
->index
% 256);
1027 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1028 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1030 * to be configurable.
1032 stream_putc(s
, ri
->md5_auth_len
);
1034 /* Sequence Number (non-decreasing). */
1035 /* RFC2080: The value used in the sequence number is
1036 arbitrary, but two suggestions are the time of the
1037 message's creation or a simple message counter. */
1038 stream_putl(s
, time(NULL
));
1040 /* Reserved field must be zero. */
1047 /* If authentication is in used, write the appropriate header
1048 * returns stream offset to which length must later be written
1049 * or 0 if this is not required
1051 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1052 struct key
*key
, char *auth_str
, int len
)
1054 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1056 switch (ri
->auth_type
) {
1057 case RIP_AUTH_SIMPLE_PASSWORD
:
1058 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1059 rip_auth_simple_write(s
, auth_str
, len
);
1062 return rip_auth_md5_ah_write(s
, ri
, key
);
1068 /* Write RIPv2 MD5 authentication data trailer */
1069 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1070 size_t doff
, char *auth_str
, int authlen
)
1073 #ifdef CRYPTO_OPENSSL
1075 #elif CRYPTO_INTERNAL
1078 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1080 /* Make it sure this interface is configured as MD5
1082 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1083 && (authlen
== RIP_AUTH_MD5_SIZE
));
1086 /* Get packet length. */
1087 len
= stream_get_endp(s
);
1089 /* Check packet length. */
1090 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1093 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1098 /* Set the digest offset length in the header */
1099 stream_putw_at(s
, doff
, len
);
1101 /* Set authentication data. */
1102 stream_putw(s
, RIP_FAMILY_AUTH
);
1103 stream_putw(s
, RIP_AUTH_DATA
);
1105 /* Generate a digest for the RIP packet. */
1106 #ifdef CRYPTO_OPENSSL
1107 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1108 ctx
= EVP_MD_CTX_new();
1109 EVP_DigestInit(ctx
, EVP_md5());
1110 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1111 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1112 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1113 EVP_MD_CTX_free(ctx
);
1114 #elif CRYPTO_INTERNAL
1115 memset(&ctx
, 0, sizeof(ctx
));
1117 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1118 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1119 MD5Final(digest
, &ctx
);
1122 /* Copy the digest to the packet. */
1123 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1126 /* RIP routing information. */
1127 static void rip_response_process(struct rip_packet
*packet
, int size
,
1128 struct sockaddr_in
*from
,
1129 struct connected
*ifc
)
1131 struct rip_interface
*ri
= ifc
->ifp
->info
;
1132 struct rip
*rip
= ri
->rip
;
1135 struct prefix_ipv4 ifaddr
;
1136 struct prefix_ipv4 ifaddrclass
;
1139 memset(&ifaddr
, 0, sizeof(ifaddr
));
1140 /* We don't know yet. */
1143 /* The Response must be ignored if it is not from the RIP
1144 port. (RFC2453 - Sec. 3.9.2)*/
1145 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1146 zlog_info("response doesn't come from RIP port: %d",
1148 rip_peer_bad_packet(rip
, from
);
1152 /* The datagram's IPv4 source address should be checked to see
1153 whether the datagram is from a valid neighbor; the source of the
1154 datagram must be on a directly connected network (RFC2453 - Sec.
1156 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1160 "This datagram doesn't come from a valid neighbor: %pI4",
1162 rip_peer_bad_packet(rip
, from
);
1166 /* It is also worth checking to see whether the response is from one
1167 of the router's own addresses. */
1169 ; /* Alredy done in rip_read () */
1171 /* Update RIP peer. */
1172 rip_peer_update(rip
, from
, packet
->version
);
1174 /* Set RTE pointer. */
1177 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1178 /* RIPv2 authentication check. */
1179 /* If the Address Family Identifier of the first (and only the
1180 first) entry in the message is 0xFFFF, then the remainder of
1181 the entry contains the authentication. */
1182 /* If the packet gets here it means authentication enabled */
1183 /* Check is done in rip_read(). So, just skipping it */
1184 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1185 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1188 if (rte
->family
!= htons(AF_INET
)) {
1189 /* Address family check. RIP only supports AF_INET. */
1190 zlog_info("Unsupported family %d from %pI4",
1196 /* - is the destination address valid (e.g., unicast; not net 0
1198 if (!rip_destination_check(rte
->prefix
)) {
1200 "Network is net 0 or net 127 or it is not unicast network");
1201 rip_peer_bad_route(rip
, from
);
1205 /* Convert metric value to host byte order. */
1206 rte
->metric
= ntohl(rte
->metric
);
1208 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1209 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1210 zlog_info("Route's metric is not in the 1-16 range.");
1211 rip_peer_bad_route(rip
, from
);
1215 /* RIPv1 does not have nexthop value. */
1216 if (packet
->version
== RIPv1
1217 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1218 zlog_info("RIPv1 packet with nexthop value %pI4",
1220 rip_peer_bad_route(rip
, from
);
1224 /* That is, if the provided information is ignored, a possibly
1225 sub-optimal, but absolutely valid, route may be taken. If
1226 the received Next Hop is not directly reachable, it should be
1227 treated as 0.0.0.0. */
1228 if (packet
->version
== RIPv2
1229 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1232 /* Multicast address check. */
1233 addrval
= ntohl(rte
->nexthop
.s_addr
);
1234 if (IN_CLASSD(addrval
)) {
1236 "Nexthop %pI4 is multicast address, skip this rte",
1241 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1242 rip
->vrf
->vrf_id
)) {
1243 struct route_node
*rn
;
1244 struct rip_info
*rinfo
;
1246 rn
= route_node_match_ipv4(rip
->table
,
1252 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1255 if (IS_RIP_DEBUG_EVENT
)
1257 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1259 rte
->nexthop
= rinfo
->from
;
1261 if (IS_RIP_DEBUG_EVENT
)
1263 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1265 rte
->nexthop
.s_addr
=
1269 route_unlock_node(rn
);
1271 if (IS_RIP_DEBUG_EVENT
)
1273 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1275 rte
->nexthop
.s_addr
= INADDR_ANY
;
1280 /* For RIPv1, there won't be a valid netmask.
1282 This is a best guess at the masks. If everyone was using old
1283 Ciscos before the 'ip subnet zero' option, it would be almost
1286 Cisco summarize ripv1 advertisements to the classful boundary
1287 (/16 for class B's) except when the RIP packet does to inside
1288 the classful network in question. */
1290 if ((packet
->version
== RIPv1
1291 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1292 || (packet
->version
== RIPv2
1293 && (rte
->prefix
.s_addr
!= INADDR_ANY
1294 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1295 uint32_t destination
;
1297 if (subnetted
== -1) {
1298 memcpy(&ifaddr
, ifc
->address
,
1299 sizeof(struct prefix_ipv4
));
1300 memcpy(&ifaddrclass
, &ifaddr
,
1301 sizeof(struct prefix_ipv4
));
1302 apply_classful_mask_ipv4(&ifaddrclass
);
1304 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1308 destination
= ntohl(rte
->prefix
.s_addr
);
1310 if (IN_CLASSA(destination
))
1311 masklen2ip(8, &rte
->mask
);
1312 else if (IN_CLASSB(destination
))
1313 masklen2ip(16, &rte
->mask
);
1314 else if (IN_CLASSC(destination
))
1315 masklen2ip(24, &rte
->mask
);
1318 masklen2ip(ifaddrclass
.prefixlen
,
1319 (struct in_addr
*)&destination
);
1320 if ((subnetted
== 1)
1321 && ((rte
->prefix
.s_addr
& destination
)
1322 == ifaddrclass
.prefix
.s_addr
)) {
1323 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1324 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1325 != rte
->prefix
.s_addr
)
1326 masklen2ip(32, &rte
->mask
);
1327 if (IS_RIP_DEBUG_EVENT
)
1328 zlog_debug("Subnetted route %pI4",
1331 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1332 != rte
->prefix
.s_addr
)
1336 if (IS_RIP_DEBUG_EVENT
) {
1337 zlog_debug("Resultant route %pI4",
1339 zlog_debug("Resultant mask %pI4",
1344 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1345 ignore the entry. */
1346 if ((packet
->version
== RIPv2
)
1347 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1348 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1349 != rte
->prefix
.s_addr
)) {
1351 "RIPv2 address %pI4 is not mask /%d applied one",
1352 &rte
->prefix
, ip_masklen(rte
->mask
));
1353 rip_peer_bad_route(rip
, from
);
1357 /* Default route's netmask is ignored. */
1358 if (packet
->version
== RIPv2
1359 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1360 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1361 if (IS_RIP_DEBUG_EVENT
)
1363 "Default route with non-zero netmask. Set zero to netmask");
1364 rte
->mask
.s_addr
= INADDR_ANY
;
1367 /* Routing table updates. */
1368 rip_rte_process(rte
, from
, ifc
->ifp
);
1372 /* Make socket for RIP protocol. */
1373 int rip_create_socket(struct vrf
*vrf
)
1377 struct sockaddr_in addr
;
1378 const char *vrf_dev
= NULL
;
1380 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1381 addr
.sin_family
= AF_INET
;
1382 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1383 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1384 addr
.sin_len
= sizeof(struct sockaddr_in
);
1385 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1386 /* sending port must always be the RIP port */
1387 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1389 /* Make datagram socket. */
1390 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1391 vrf_dev
= vrf
->name
;
1392 frr_with_privs(&ripd_privs
) {
1393 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1396 flog_err_sys(EC_LIB_SOCKET
,
1397 "Cannot create UDP socket: %s",
1398 safe_strerror(errno
));
1403 sockopt_broadcast(sock
);
1404 sockopt_reuseaddr(sock
);
1405 sockopt_reuseport(sock
);
1406 setsockopt_ipv4_multicast_loop(sock
, 0);
1407 #ifdef IPTOS_PREC_INTERNETCONTROL
1408 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1410 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1412 frr_with_privs(&ripd_privs
) {
1413 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1415 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1416 __func__
, sock
, &addr
.sin_addr
,
1417 (int)ntohs(addr
.sin_port
),
1418 safe_strerror(errno
));
1428 /* RIP packet send to destination address, on interface denoted by
1429 * by connected argument. NULL to argument denotes destination should be
1430 * should be RIP multicast group
1432 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1433 struct connected
*ifc
)
1435 struct rip_interface
*ri
;
1438 struct sockaddr_in sin
;
1442 struct cmsghdr
*cmsgptr
;
1443 char adata
[256] = {};
1444 struct in_pktinfo
*pkt
;
1445 #endif /* GNU_LINUX */
1447 assert(ifc
!= NULL
);
1448 ri
= ifc
->ifp
->info
;
1451 if (IS_RIP_DEBUG_PACKET
) {
1452 #define ADDRESS_SIZE 20
1453 char dst
[ADDRESS_SIZE
];
1456 inet_ntop(AF_INET
, &to
->sin_addr
, dst
, sizeof(dst
));
1458 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1459 inet_ntop(AF_INET
, &sin
.sin_addr
, dst
, sizeof(dst
));
1462 zlog_debug("rip_send_packet %pI4 > %s (%s)",
1463 &ifc
->address
->u
.prefix4
, dst
,
1467 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1469 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1471 * with multiple addresses on the same subnet: the first address
1472 * on the subnet is configured "primary", and all subsequent
1474 * on that subnet are treated as "secondary" addresses.
1475 * In order to avoid routing-table bloat on other rip listeners,
1476 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1478 * XXX Since Linux is the only system for which the
1479 * ZEBRA_IFA_SECONDARY
1480 * flag is set, we would end up sending a packet for a
1482 * source address on non-linux systems.
1484 if (IS_RIP_DEBUG_PACKET
)
1485 zlog_debug("duplicate dropped");
1489 /* Make destination address. */
1490 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1491 sin
.sin_family
= AF_INET
;
1492 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1493 sin
.sin_len
= sizeof(struct sockaddr_in
);
1494 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1496 /* When destination is specified, use it's port and address. */
1498 sin
.sin_port
= to
->sin_port
;
1499 sin
.sin_addr
= to
->sin_addr
;
1501 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1502 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1504 rip_interface_multicast_set(rip
->sock
, ifc
);
1507 memset(&msg
, 0, sizeof(msg
));
1508 msg
.msg_name
= (void *)&sin
;
1509 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1516 msg
.msg_control
= (void *)adata
;
1517 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1519 cmsgptr
= (struct cmsghdr
*)adata
;
1520 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1521 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1522 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1523 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1524 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1525 #endif /* GNU_LINUX */
1527 ret
= sendmsg(rip
->sock
, &msg
, 0);
1529 if (IS_RIP_DEBUG_EVENT
)
1530 zlog_debug("SEND to %pI4%d", &sin
.sin_addr
,
1531 ntohs(sin
.sin_port
));
1534 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1539 /* Add redistributed route to RIP table. */
1540 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1541 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1542 unsigned int metric
, unsigned char distance
,
1546 struct route_node
*rp
= NULL
;
1547 struct rip_info
*rinfo
= NULL
, newinfo
;
1548 struct list
*list
= NULL
;
1550 /* Redistribute route */
1551 ret
= rip_destination_check(p
->prefix
);
1555 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1557 memset(&newinfo
, 0, sizeof(struct rip_info
));
1558 newinfo
.type
= type
;
1559 newinfo
.sub_type
= sub_type
;
1561 newinfo
.external_metric
= metric
;
1562 newinfo
.distance
= distance
;
1563 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1568 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1569 rinfo
= listgetdata(listhead(list
));
1571 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1572 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1573 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1574 route_unlock_node(rp
);
1578 /* Manually configured RIP route check. */
1579 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1580 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1581 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1582 if (type
!= ZEBRA_ROUTE_RIP
1583 || ((sub_type
!= RIP_ROUTE_STATIC
)
1584 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1585 route_unlock_node(rp
);
1590 (void)rip_ecmp_replace(rip
, &newinfo
);
1591 route_unlock_node(rp
);
1593 (void)rip_ecmp_add(rip
, &newinfo
);
1595 if (IS_RIP_DEBUG_EVENT
) {
1596 zlog_debug("Redistribute new prefix %pFX", p
);
1599 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1602 /* Delete redistributed route from RIP table. */
1603 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1604 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1607 struct route_node
*rp
;
1608 struct rip_info
*rinfo
;
1610 ret
= rip_destination_check(p
->prefix
);
1614 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1616 struct list
*list
= rp
->info
;
1618 if (list
!= NULL
&& listcount(list
) != 0) {
1619 rinfo
= listgetdata(listhead(list
));
1620 if (rinfo
!= NULL
&& rinfo
->type
== type
1621 && rinfo
->sub_type
== sub_type
1622 && rinfo
->nh
.ifindex
== ifindex
) {
1623 /* Perform poisoned reverse. */
1624 rinfo
->metric
= RIP_METRIC_INFINITY
;
1625 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1626 rip_garbage_collect
,
1628 RIP_TIMER_OFF(rinfo
->t_timeout
);
1629 rinfo
->flags
|= RIP_RTF_CHANGED
;
1631 if (IS_RIP_DEBUG_EVENT
)
1633 "Poison %pFX on the interface %s with an infinity metric [delete]",
1639 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1642 route_unlock_node(rp
);
1646 /* Response to request called from rip_read ().*/
1647 static void rip_request_process(struct rip_packet
*packet
, int size
,
1648 struct sockaddr_in
*from
, struct connected
*ifc
)
1653 struct prefix_ipv4 p
;
1654 struct route_node
*rp
;
1655 struct rip_info
*rinfo
;
1656 struct rip_interface
*ri
;
1658 /* Does not reponse to the requests on the loopback interfaces */
1659 if (if_is_loopback(ifc
->ifp
))
1662 /* Check RIP process is enabled on this interface. */
1663 ri
= ifc
->ifp
->info
;
1668 /* When passive interface is specified, suppress responses */
1672 /* RIP peer update. */
1673 rip_peer_update(rip
, from
, packet
->version
);
1675 lim
= ((caddr_t
)packet
) + size
;
1678 /* The Request is processed entry by entry. If there are no
1679 entries, no response is given. */
1680 if (lim
== (caddr_t
)rte
)
1683 /* There is one special case. If there is exactly one entry in the
1684 request, and it has an address family identifier of zero and a
1685 metric of infinity (i.e., 16), then this is a request to send the
1686 entire routing table. */
1687 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1688 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1689 /* All route with split horizon */
1690 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1692 if (ntohs(rte
->family
) != AF_INET
)
1695 /* Examine the list of RTEs in the Request one by one. For each
1696 entry, look up the destination in the router's routing
1697 database and, if there is a route, put that route's metric in
1698 the metric field of the RTE. If there is no explicit route
1699 to the specified destination, put infinity in the metric
1700 field. Once all the entries have been filled in, change the
1701 command from Request to Response and send the datagram back
1702 to the requestor. */
1705 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1706 p
.prefix
= rte
->prefix
;
1707 p
.prefixlen
= ip_masklen(rte
->mask
);
1708 apply_mask_ipv4(&p
);
1710 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1712 rinfo
= listgetdata(
1713 listhead((struct list
*)rp
->info
));
1714 rte
->metric
= htonl(rinfo
->metric
);
1715 route_unlock_node(rp
);
1717 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1719 packet
->command
= RIP_RESPONSE
;
1721 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1723 rip
->counters
.queries
++;
1726 /* First entry point of RIP packet. */
1727 static int rip_read(struct thread
*t
)
1729 struct rip
*rip
= THREAD_ARG(t
);
1733 union rip_buf rip_buf
;
1734 struct rip_packet
*packet
;
1735 struct sockaddr_in from
;
1739 struct interface
*ifp
= NULL
;
1740 struct connected
*ifc
;
1741 struct rip_interface
*ri
;
1744 /* Fetch socket then register myself. */
1745 sock
= THREAD_FD(t
);
1747 /* Add myself to tne next event */
1748 rip_event(rip
, RIP_READ
, sock
);
1750 /* RIPd manages only IPv4. */
1751 memset(&from
, 0, sizeof(struct sockaddr_in
));
1752 fromlen
= sizeof(struct sockaddr_in
);
1754 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1755 (struct sockaddr
*)&from
, &fromlen
);
1757 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1758 safe_strerror(errno
));
1762 /* Check is this packet comming from myself? */
1763 if (if_check_address(rip
, from
.sin_addr
)) {
1764 if (IS_RIP_DEBUG_PACKET
)
1765 zlog_debug("ignore packet comes from myself (VRF %s)",
1770 /* Which interface is this packet comes from. */
1771 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1776 /* RIP packet received */
1777 if (IS_RIP_DEBUG_EVENT
)
1778 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1779 &from
.sin_addr
, ntohs(from
.sin_port
),
1780 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1782 /* If this packet come from unknown interface, ignore it. */
1785 "rip_read: cannot find interface for packet from %pI4 port %d (VRF %s)",
1786 &from
.sin_addr
, ntohs(from
.sin_port
),
1792 p
.u
.prefix4
= from
.sin_addr
;
1793 p
.prefixlen
= IPV4_MAX_BITLEN
;
1795 ifc
= connected_lookup_prefix(ifp
, &p
);
1799 "rip_read: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1800 &from
.sin_addr
, ntohs(from
.sin_port
),
1801 ifp
->name
, rip
->vrf_name
);
1805 /* Packet length check. */
1806 if (len
< RIP_PACKET_MINSIZ
) {
1807 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1809 rip_peer_bad_packet(rip
, &from
);
1812 if (len
> RIP_PACKET_MAXSIZ
) {
1813 zlog_warn("packet size %d is larger than max size %d", len
,
1815 rip_peer_bad_packet(rip
, &from
);
1819 /* Packet alignment check. */
1820 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1821 zlog_warn("packet size %d is wrong for RIP packet alignment",
1823 rip_peer_bad_packet(rip
, &from
);
1827 /* Set RTE number. */
1828 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1830 /* For easy to handle. */
1831 packet
= &rip_buf
.rip_packet
;
1833 /* RIP version check. */
1834 if (packet
->version
== 0) {
1835 zlog_info("version 0 with command %d received.",
1837 rip_peer_bad_packet(rip
, &from
);
1841 /* Dump RIP packet. */
1842 if (IS_RIP_DEBUG_RECV
)
1843 rip_packet_dump(packet
, len
, "RECV");
1845 /* RIP version adjust. This code should rethink now. RFC1058 says
1846 that "Version 1 implementations are to ignore this extra data and
1847 process only the fields specified in this document.". So RIPv3
1848 packet should be treated as RIPv1 ignoring must be zero field. */
1849 if (packet
->version
> RIPv2
)
1850 packet
->version
= RIPv2
;
1852 /* Is RIP running or is this RIP neighbor ?*/
1854 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1855 if (IS_RIP_DEBUG_EVENT
)
1856 zlog_debug("RIP is not enabled on interface %s.",
1858 rip_peer_bad_packet(rip
, &from
);
1862 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1863 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1865 if (vrecv
== RI_RIP_VERSION_NONE
1866 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1867 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1868 if (IS_RIP_DEBUG_PACKET
)
1870 " packet's v%d doesn't fit to if version spec",
1872 rip_peer_bad_packet(rip
, &from
);
1876 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1877 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1878 accepted; authenticated RIP-2 messages shall be discarded. */
1879 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1880 && (packet
->version
== RIPv2
)
1881 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1882 if (IS_RIP_DEBUG_EVENT
)
1884 "packet RIPv%d is dropped because authentication disabled",
1886 ripd_notif_send_auth_type_failure(ifp
->name
);
1887 rip_peer_bad_packet(rip
, &from
);
1892 If the router is configured to authenticate RIP-2 messages, then
1893 RIP-1 messages and RIP-2 messages which pass authentication
1894 testing shall be accepted; unauthenticated and failed
1895 authentication RIP-2 messages shall be discarded. For maximum
1896 security, RIP-1 messages should be ignored when authentication is
1897 in use (see section 4.1); otherwise, the routing information from
1898 authenticated messages will be propagated by RIP-1 routers in an
1899 unauthenticated manner.
1901 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1902 * always reply regardless of authentication settings, because:
1904 * - if there other authorised routers on-link, the REQUESTor can
1905 * passively obtain the routing updates anyway
1906 * - if there are no other authorised routers on-link, RIP can
1907 * easily be disabled for the link to prevent giving out information
1908 * on state of this routers RIP routing table..
1910 * I.e. if RIPv1 has any place anymore these days, it's as a very
1911 * simple way to distribute routing information (e.g. to embedded
1912 * hosts / appliances) and the ability to give out RIPv1
1913 * routing-information freely, while still requiring RIPv2
1914 * authentication for any RESPONSEs might be vaguely useful.
1916 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1917 /* Discard RIPv1 messages other than REQUESTs */
1918 if (packet
->command
!= RIP_REQUEST
) {
1919 if (IS_RIP_DEBUG_PACKET
)
1921 "RIPv1 dropped because authentication enabled");
1922 ripd_notif_send_auth_type_failure(ifp
->name
);
1923 rip_peer_bad_packet(rip
, &from
);
1926 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1927 const char *auth_desc
;
1930 /* There definitely is no authentication in the packet.
1932 if (IS_RIP_DEBUG_PACKET
)
1934 "RIPv2 authentication failed: no auth RTE in packet");
1935 ripd_notif_send_auth_type_failure(ifp
->name
);
1936 rip_peer_bad_packet(rip
, &from
);
1940 /* First RTE must be an Authentication Family RTE */
1941 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1942 if (IS_RIP_DEBUG_PACKET
)
1944 "RIPv2 dropped because authentication enabled");
1945 ripd_notif_send_auth_type_failure(ifp
->name
);
1946 rip_peer_bad_packet(rip
, &from
);
1950 /* Check RIPv2 authentication. */
1951 switch (ntohs(packet
->rte
->tag
)) {
1952 case RIP_AUTH_SIMPLE_PASSWORD
:
1953 auth_desc
= "simple";
1954 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1959 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1960 /* Reset RIP packet length to trim MD5 data. */
1966 auth_desc
= "unknown type";
1967 if (IS_RIP_DEBUG_PACKET
)
1969 "RIPv2 Unknown authentication type %d",
1970 ntohs(packet
->rte
->tag
));
1974 if (IS_RIP_DEBUG_PACKET
)
1975 zlog_debug("RIPv2 %s authentication success",
1978 if (IS_RIP_DEBUG_PACKET
)
1979 zlog_debug("RIPv2 %s authentication failure",
1981 ripd_notif_send_auth_failure(ifp
->name
);
1982 rip_peer_bad_packet(rip
, &from
);
1987 /* Process each command. */
1988 switch (packet
->command
) {
1990 rip_response_process(packet
, len
, &from
, ifc
);
1994 rip_request_process(packet
, len
, &from
, ifc
);
1999 "Obsolete command %s received, please sent it to routed",
2000 lookup_msg(rip_msg
, packet
->command
, NULL
));
2001 rip_peer_bad_packet(rip
, &from
);
2003 case RIP_POLL_ENTRY
:
2004 zlog_info("Obsolete command %s received",
2005 lookup_msg(rip_msg
, packet
->command
, NULL
));
2006 rip_peer_bad_packet(rip
, &from
);
2009 zlog_info("Unknown RIP command %d received", packet
->command
);
2010 rip_peer_bad_packet(rip
, &from
);
2017 /* Write routing table entry to the stream and return next index of
2018 the routing table entry in the stream. */
2019 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2020 uint8_t version
, struct rip_info
*rinfo
)
2022 struct in_addr mask
;
2024 /* Write routing table entry. */
2025 if (version
== RIPv1
) {
2026 stream_putw(s
, AF_INET
);
2028 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2029 stream_put_ipv4(s
, 0);
2030 stream_put_ipv4(s
, 0);
2031 stream_putl(s
, rinfo
->metric_out
);
2033 masklen2ip(p
->prefixlen
, &mask
);
2035 stream_putw(s
, AF_INET
);
2036 stream_putw(s
, rinfo
->tag_out
);
2037 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2038 stream_put_ipv4(s
, mask
.s_addr
);
2039 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2040 stream_putl(s
, rinfo
->metric_out
);
2046 /* Send update to the ifp or spcified neighbor. */
2047 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2048 int route_type
, uint8_t version
)
2053 struct route_node
*rp
;
2054 struct rip_info
*rinfo
;
2055 struct rip_interface
*ri
;
2056 struct prefix_ipv4
*p
;
2057 struct prefix_ipv4 classfull
;
2058 struct prefix_ipv4 ifaddrclass
;
2059 struct key
*key
= NULL
;
2060 /* this might need to made dynamic if RIP ever supported auth methods
2061 with larger key string sizes */
2062 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2063 size_t doff
= 0; /* offset of digest offset field */
2067 struct list
*list
= NULL
;
2068 struct listnode
*listnode
= NULL
;
2070 /* Logging output event. */
2071 if (IS_RIP_DEBUG_EVENT
) {
2073 zlog_debug("update routes to neighbor %pI4",
2076 zlog_debug("update routes on interface %s ifindex %d",
2077 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2080 /* Get RIP interface. */
2081 ri
= ifc
->ifp
->info
;
2084 /* Set output stream. */
2087 /* Reset stream and RTE counter. */
2089 rtemax
= RIP_MAX_RTE
;
2091 /* If output interface is in simple password authentication mode, we
2092 need space for authentication data. */
2093 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2096 /* If output interface is in MD5 authentication mode, we need space
2097 for authentication header and data. */
2098 if (ri
->auth_type
== RIP_AUTH_MD5
)
2101 /* If output interface is in simple password authentication mode
2102 and string or keychain is specified we need space for auth. data */
2103 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2104 if (ri
->key_chain
) {
2105 struct keychain
*keychain
;
2107 keychain
= keychain_lookup(ri
->key_chain
);
2109 key
= key_lookup_for_send(keychain
);
2111 /* to be passed to auth functions later */
2112 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2113 if (strlen(auth_str
) == 0)
2117 if (version
== RIPv1
) {
2118 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2119 apply_classful_mask_ipv4(&ifaddrclass
);
2121 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2125 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2126 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2127 rinfo
= listgetdata(listhead(list
));
2128 /* For RIPv1, if we are subnetted, output subnets in our
2130 /* that have the same mask as the output "interface".
2132 /* networks, only the classfull version is output. */
2134 if (version
== RIPv1
) {
2135 p
= (struct prefix_ipv4
*)&rp
->p
;
2137 if (IS_RIP_DEBUG_PACKET
)
2139 "RIPv1 mask check, %pFX considered for output",
2144 (struct prefix
*)&ifaddrclass
,
2146 if ((ifc
->address
->prefixlen
2149 != IPV4_MAX_BITLEN
))
2152 memcpy(&classfull
, &rp
->p
,
2153 sizeof(struct prefix_ipv4
));
2154 apply_classful_mask_ipv4(&classfull
);
2155 if (rp
->p
.u
.prefix4
.s_addr
!= INADDR_ANY
2156 && classfull
.prefixlen
2160 if (IS_RIP_DEBUG_PACKET
)
2162 "RIPv1 mask check, %pFX made it through",
2165 p
= (struct prefix_ipv4
*)&rp
->p
;
2167 /* Apply output filters. */
2168 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2172 /* Changed route only output. */
2173 if (route_type
== rip_changed_route
2174 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2177 /* Split horizon. */
2178 /* if (split_horizon == rip_split_horizon) */
2179 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2181 * We perform split horizon for RIP and
2183 * For rip routes, we want to suppress the route
2185 * end up sending the route back on the
2187 * learned it from, with a higher metric. For
2189 * we suppress the route if the prefix is a
2191 * source address that we are going to use for
2193 * (in order to handle the case when multiple
2195 * configured on the same interface).
2198 struct rip_info
*tmp_rinfo
= NULL
;
2199 struct connected
*tmp_ifc
= NULL
;
2201 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2203 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2204 && tmp_rinfo
->nh
.ifindex
2205 == ifc
->ifp
->ifindex
) {
2211 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2212 for (ALL_LIST_ELEMENTS_RO(
2213 ifc
->ifp
->connected
,
2217 tmp_ifc
->address
)) {
2227 /* Preparation for route-map. */
2228 rinfo
->metric_set
= 0;
2229 rinfo
->nexthop_out
.s_addr
= 0;
2230 rinfo
->metric_out
= rinfo
->metric
;
2231 rinfo
->tag_out
= rinfo
->tag
;
2232 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2234 /* In order to avoid some local loops,
2235 * if the RIP route has a nexthop via this interface,
2237 * otherwise set it to 0. The nexthop should not be
2239 * beyond the local broadcast/multicast area in order
2240 * to avoid an IGP multi-level recursive look-up.
2243 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2244 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2246 /* Interface route-map */
2247 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2248 ret
= route_map_apply(
2249 ri
->routemap
[RIP_FILTER_OUT
],
2250 (struct prefix
*)p
, rinfo
);
2252 if (ret
== RMAP_DENYMATCH
) {
2253 if (IS_RIP_DEBUG_PACKET
)
2255 "RIP %pFX is filtered by route-map out",
2261 /* Apply redistribute route map - continue, if deny */
2262 if (rip
->redist
[rinfo
->type
].route_map
.name
2263 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2264 ret
= route_map_apply(
2265 rip
->redist
[rinfo
->type
].route_map
.map
,
2266 (struct prefix
*)p
, rinfo
);
2268 if (ret
== RMAP_DENYMATCH
) {
2269 if (IS_RIP_DEBUG_PACKET
)
2271 "%pFX is filtered by route-map",
2277 /* When route-map does not set metric. */
2278 if (!rinfo
->metric_set
) {
2279 /* If redistribute metric is set. */
2280 if (rip
->redist
[rinfo
->type
].metric_config
2281 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2283 rip
->redist
[rinfo
->type
].metric
;
2285 /* If the route is not connected or
2287 one, use default-metric value*/
2288 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2290 != ZEBRA_ROUTE_CONNECT
2292 != RIP_METRIC_INFINITY
)
2294 rip
->default_metric
;
2298 /* Apply offset-list */
2299 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2300 rip_offset_list_apply_out(p
, ifc
->ifp
,
2301 &rinfo
->metric_out
);
2303 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2304 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2306 /* Perform split-horizon with poisoned reverse
2307 * for RIP and connected routes.
2309 if (ri
->split_horizon
2310 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2312 * We perform split horizon for RIP and
2314 * For rip routes, we want to suppress the route
2316 * end up sending the route back on the
2318 * learned it from, with a higher metric. For
2320 * we suppress the route if the prefix is a
2322 * source address that we are going to use for
2324 * (in order to handle the case when multiple
2326 * configured on the same interface).
2328 struct rip_info
*tmp_rinfo
= NULL
;
2329 struct connected
*tmp_ifc
= NULL
;
2331 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2333 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2334 && tmp_rinfo
->nh
.ifindex
2335 == ifc
->ifp
->ifindex
)
2337 RIP_METRIC_INFINITY
;
2339 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2340 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2341 for (ALL_LIST_ELEMENTS_RO(
2342 ifc
->ifp
->connected
,
2346 tmp_ifc
->address
)) {
2348 RIP_METRIC_INFINITY
;
2354 /* Prepare preamble, auth headers, if needs be */
2356 stream_putc(s
, RIP_RESPONSE
);
2357 stream_putc(s
, version
);
2360 /* auth header for !v1 && !no_auth */
2361 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2362 && (version
!= RIPv1
))
2363 doff
= rip_auth_header_write(
2364 s
, ri
, key
, auth_str
,
2365 RIP_AUTH_SIMPLE_SIZE
);
2368 /* Write RTE to the stream. */
2369 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2370 if (num
== rtemax
) {
2371 if (version
== RIPv2
2372 && ri
->auth_type
== RIP_AUTH_MD5
)
2373 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2374 RIP_AUTH_SIMPLE_SIZE
);
2376 ret
= rip_send_packet(STREAM_DATA(s
),
2377 stream_get_endp(s
), to
,
2380 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2381 rip_packet_dump((struct rip_packet
*)
2390 /* Flush unwritten RTE. */
2392 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2393 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2394 RIP_AUTH_SIMPLE_SIZE
);
2396 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2399 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2400 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2401 stream_get_endp(s
), "SEND");
2405 /* Statistics updates. */
2409 /* Send RIP packet to the interface. */
2410 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2413 struct interface
*ifp
= ifc
->ifp
;
2414 struct rip_interface
*ri
= ifp
->info
;
2415 struct sockaddr_in to
;
2417 /* When RIP version is 2 and multicast enable interface. */
2418 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2419 if (IS_RIP_DEBUG_EVENT
)
2420 zlog_debug("multicast announce on %s ", ifp
->name
);
2422 rip_output_process(ifc
, NULL
, route_type
, version
);
2426 /* If we can't send multicast packet, send it with unicast. */
2427 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2428 if (ifc
->address
->family
== AF_INET
) {
2429 /* Destination address and port setting. */
2430 memset(&to
, 0, sizeof(struct sockaddr_in
));
2431 if (ifc
->destination
)
2432 /* use specified broadcast or peer destination
2434 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2435 else if (ifc
->address
->prefixlen
< IPV4_MAX_BITLEN
)
2436 /* calculate the appropriate broadcast address
2438 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2439 ifc
->address
->u
.prefix4
.s_addr
,
2440 ifc
->address
->prefixlen
);
2442 /* do not know where to send the packet */
2444 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2446 if (IS_RIP_DEBUG_EVENT
)
2447 zlog_debug("%s announce to %pI4 on %s",
2448 CONNECTED_PEER(ifc
) ? "unicast"
2450 &to
.sin_addr
, ifp
->name
);
2452 rip_output_process(ifc
, &to
, route_type
, version
);
2457 /* Update send to all interface and neighbor. */
2458 static void rip_update_process(struct rip
*rip
, int route_type
)
2460 struct listnode
*ifnode
, *ifnnode
;
2461 struct connected
*connected
;
2462 struct interface
*ifp
;
2463 struct rip_interface
*ri
;
2464 struct route_node
*rp
;
2465 struct sockaddr_in to
;
2468 /* Send RIP update to each interface. */
2469 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2470 if (if_is_loopback(ifp
))
2473 if (!if_is_operative(ifp
))
2476 /* Fetch RIP interface information. */
2479 /* When passive interface is specified, suppress announce to the
2486 * If there is no version configuration in the
2488 * use rip's version setting.
2490 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2494 if (IS_RIP_DEBUG_EVENT
)
2495 zlog_debug("SEND UPDATE to %s ifindex %d",
2496 ifp
->name
, ifp
->ifindex
);
2498 /* send update on each connected network */
2499 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2501 if (connected
->address
->family
== AF_INET
) {
2503 rip_update_interface(
2507 && if_is_multicast(ifp
))
2508 rip_update_interface(
2516 /* RIP send updates to each neighbor. */
2517 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2518 if (rp
->info
!= NULL
) {
2521 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2525 "Neighbor %pI4 doesn't have connected interface!",
2530 /* Set destination address and port */
2531 memset(&to
, 0, sizeof(struct sockaddr_in
));
2532 to
.sin_addr
= p
->u
.prefix4
;
2533 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2535 /* RIP version is rip's configuration. */
2536 rip_output_process(connected
, &to
, route_type
,
2541 /* RIP's periodical timer. */
2542 static int rip_update(struct thread
*t
)
2544 struct rip
*rip
= THREAD_ARG(t
);
2546 if (IS_RIP_DEBUG_EVENT
)
2547 zlog_debug("update timer fire!");
2549 /* Process update output. */
2550 rip_update_process(rip
, rip_all_route
);
2552 /* Triggered updates may be suppressed if a regular update is due by
2553 the time the triggered update would be sent. */
2554 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2557 /* Register myself. */
2558 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2563 /* Walk down the RIP routing table then clear changed flag. */
2564 static void rip_clear_changed_flag(struct rip
*rip
)
2566 struct route_node
*rp
;
2567 struct rip_info
*rinfo
= NULL
;
2568 struct list
*list
= NULL
;
2569 struct listnode
*listnode
= NULL
;
2571 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2572 if ((list
= rp
->info
) != NULL
)
2573 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2574 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2575 /* This flag can be set only on the first entry.
2581 /* Triggered update interval timer. */
2582 static int rip_triggered_interval(struct thread
*t
)
2584 struct rip
*rip
= THREAD_ARG(t
);
2588 rip_triggered_update(t
);
2593 /* Execute triggered update. */
2594 static int rip_triggered_update(struct thread
*t
)
2596 struct rip
*rip
= THREAD_ARG(t
);
2599 /* Cancel interval timer. */
2600 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2603 /* Logging triggered update. */
2604 if (IS_RIP_DEBUG_EVENT
)
2605 zlog_debug("triggered update!");
2607 /* Split Horizon processing is done when generating triggered
2608 updates as well as normal updates (see section 2.6). */
2609 rip_update_process(rip
, rip_changed_route
);
2611 /* Once all of the triggered updates have been generated, the route
2612 change flags should be cleared. */
2613 rip_clear_changed_flag(rip
);
2615 /* After a triggered update is sent, a timer should be set for a
2616 random interval between 1 and 5 seconds. If other changes that
2617 would trigger updates occur before the timer expires, a single
2618 update is triggered when the timer expires. */
2619 interval
= (frr_weak_random() % 5) + 1;
2621 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2622 &rip
->t_triggered_interval
);
2627 /* Withdraw redistributed route. */
2628 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2630 struct route_node
*rp
;
2631 struct rip_info
*rinfo
= NULL
;
2632 struct list
*list
= NULL
;
2634 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2635 if ((list
= rp
->info
) != NULL
) {
2636 rinfo
= listgetdata(listhead(list
));
2637 if (rinfo
->type
== type
2638 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2639 /* Perform poisoned reverse. */
2640 rinfo
->metric
= RIP_METRIC_INFINITY
;
2641 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2642 rip_garbage_collect
,
2644 RIP_TIMER_OFF(rinfo
->t_timeout
);
2645 rinfo
->flags
|= RIP_RTF_CHANGED
;
2647 if (IS_RIP_DEBUG_EVENT
) {
2648 struct prefix_ipv4
*p
=
2649 (struct prefix_ipv4
*)&rp
->p
;
2652 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2659 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2664 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2668 vrf
= vrf_lookup_by_id(vrf_id
);
2675 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2679 rip
.vrf_name
= (char *)vrf_name
;
2681 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2684 /* Create new RIP instance and set it to global variable. */
2685 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2689 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2690 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2692 /* Set initial value. */
2693 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2694 rip
->default_metric
=
2695 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2697 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2698 rip
->passive_default
=
2699 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2700 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2702 rip
->timeout_time
= yang_get_default_uint32(
2703 "%s/timers/holddown-interval", RIP_INSTANCE
);
2704 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2707 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2709 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2711 /* Initialize RIP data structures. */
2712 rip
->table
= route_table_init();
2713 route_table_set_info(rip
->table
, rip
);
2714 rip
->neighbor
= route_table_init();
2715 rip
->peer_list
= list_new();
2716 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2717 rip
->peer_list
->del
= rip_peer_list_del
;
2718 rip
->distance_table
= route_table_init();
2719 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2720 rip
->enable_interface
= vector_init(1);
2721 rip
->enable_network
= route_table_init();
2722 rip
->passive_nondefault
= vector_init(1);
2723 rip
->offset_list_master
= list_new();
2724 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2725 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2727 /* Distribute list install. */
2728 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2729 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2730 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2732 /* if rmap install. */
2733 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2734 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2735 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2737 /* Make output stream. */
2738 rip
->obuf
= stream_new(1500);
2740 /* Enable the routing instance if possible. */
2741 if (vrf
&& vrf_is_enabled(vrf
))
2742 rip_instance_enable(rip
, vrf
, socket
);
2748 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2753 /* Sned RIP request to the destination. */
2754 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2755 uint8_t version
, struct connected
*connected
)
2758 struct rip_packet rip_packet
;
2759 struct listnode
*node
, *nnode
;
2761 memset(&rip_packet
, 0, sizeof(rip_packet
));
2763 rip_packet
.command
= RIP_REQUEST
;
2764 rip_packet
.version
= version
;
2765 rte
= rip_packet
.rte
;
2766 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2770 * connected is only sent for ripv1 case, or when
2771 * interface does not support multicast. Caller loops
2772 * over each connected address for this case.
2774 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2776 != sizeof(rip_packet
))
2779 return sizeof(rip_packet
);
2782 /* send request on each connected network */
2783 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2784 struct prefix_ipv4
*p
;
2786 p
= (struct prefix_ipv4
*)connected
->address
;
2788 if (p
->family
!= AF_INET
)
2791 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2793 != sizeof(rip_packet
))
2796 return sizeof(rip_packet
);
2799 static int rip_update_jitter(unsigned long time
)
2801 #define JITTER_BOUND 4
2802 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2803 Given that, we cannot let time be less than JITTER_BOUND seconds.
2804 The RIPv2 RFC says jitter should be small compared to
2805 update_time. We consider 1/JITTER_BOUND to be small.
2808 int jitter_input
= time
;
2811 if (jitter_input
< JITTER_BOUND
)
2812 jitter_input
= JITTER_BOUND
;
2814 jitter
= (((frr_weak_random() % ((jitter_input
* 2) + 1))
2817 return jitter
/ JITTER_BOUND
;
2820 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2826 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2828 case RIP_UPDATE_EVENT
:
2829 RIP_TIMER_OFF(rip
->t_update
);
2830 jitter
= rip_update_jitter(rip
->update_time
);
2831 thread_add_timer(master
, rip_update
, rip
,
2832 sock
? 2 : rip
->update_time
+ jitter
,
2835 case RIP_TRIGGERED_UPDATE
:
2836 if (rip
->t_triggered_interval
)
2839 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2840 &rip
->t_triggered_update
);
2847 struct rip_distance
*rip_distance_new(void)
2849 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2852 void rip_distance_free(struct rip_distance
*rdistance
)
2854 if (rdistance
->access_list
)
2855 free(rdistance
->access_list
);
2856 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2859 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2860 struct route_node
*node
)
2862 struct rip_distance
*rdistance
;
2864 rdistance
= node
->info
;
2866 rip_distance_free(rdistance
);
2869 /* Apply RIP information to distance method. */
2870 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2872 struct route_node
*rn
;
2873 struct prefix_ipv4 p
;
2874 struct rip_distance
*rdistance
;
2875 struct access_list
*alist
;
2877 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2879 p
.prefix
= rinfo
->from
;
2880 p
.prefixlen
= IPV4_MAX_BITLEN
;
2882 /* Check source address. */
2883 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2885 rdistance
= rn
->info
;
2886 route_unlock_node(rn
);
2888 if (rdistance
->access_list
) {
2889 alist
= access_list_lookup(AFI_IP
,
2890 rdistance
->access_list
);
2893 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2897 return rdistance
->distance
;
2899 return rdistance
->distance
;
2903 return rip
->distance
;
2908 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2910 struct route_node
*rn
;
2911 struct rip_distance
*rdistance
;
2915 vty_out(vty
, " Distance: (default is %u)\n",
2916 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2918 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2919 if ((rdistance
= rn
->info
) != NULL
) {
2922 " Address Distance List\n");
2925 snprintfrr(buf
, sizeof(buf
), "%pFX", &rn
->p
);
2926 vty_out(vty
, " %-20s %4d %s\n", buf
,
2927 rdistance
->distance
,
2928 rdistance
->access_list
? rdistance
->access_list
2933 /* Update ECMP routes to zebra when ECMP is disabled. */
2934 void rip_ecmp_disable(struct rip
*rip
)
2936 struct route_node
*rp
;
2937 struct rip_info
*rinfo
, *tmp_rinfo
;
2939 struct listnode
*node
, *nextnode
;
2941 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2942 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2943 rinfo
= listgetdata(listhead(list
));
2944 if (!rip_route_rte(rinfo
))
2947 /* Drop all other entries, except the first one. */
2948 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
2949 if (tmp_rinfo
!= rinfo
) {
2950 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
2952 tmp_rinfo
->t_garbage_collect
);
2953 list_delete_node(list
, node
);
2954 rip_info_free(tmp_rinfo
);
2958 rip_zebra_ipv4_add(rip
, rp
);
2960 /* Set the route change flag. */
2961 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2963 /* Signal the output process to trigger an update. */
2964 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2968 /* Print out routes update time. */
2969 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2974 char timebuf
[TIME_BUF
];
2975 struct thread
*thread
;
2977 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2978 clock
= thread_timer_remain_second(thread
);
2979 gmtime_r(&clock
, &tm
);
2980 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2981 vty_out(vty
, "%5s", timebuf
);
2982 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2983 clock
= thread_timer_remain_second(thread
);
2984 gmtime_r(&clock
, &tm
);
2985 strftime(timebuf
, TIME_BUF
, "%M:%S", &tm
);
2986 vty_out(vty
, "%5s", timebuf
);
2990 static const char *rip_route_type_print(int sub_type
)
2995 case RIP_ROUTE_STATIC
:
2997 case RIP_ROUTE_DEFAULT
:
2999 case RIP_ROUTE_REDISTRIBUTE
:
3001 case RIP_ROUTE_INTERFACE
:
3010 "show ip rip [vrf NAME]",
3017 struct route_node
*np
;
3018 struct rip_info
*rinfo
= NULL
;
3019 struct list
*list
= NULL
;
3020 struct listnode
*listnode
= NULL
;
3021 const char *vrf_name
;
3024 if (argv_find(argv
, argc
, "vrf", &idx
))
3025 vrf_name
= argv
[idx
+ 1]->arg
;
3027 vrf_name
= VRF_DEFAULT_NAME
;
3029 rip
= rip_lookup_by_vrf_name(vrf_name
);
3031 vty_out(vty
, "%% RIP instance not found\n");
3034 if (!rip
->enabled
) {
3035 vty_out(vty
, "%% RIP instance is disabled\n");
3040 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3042 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3043 " (i) - interface\n\n"
3044 " Network Next Hop Metric From Tag Time\n");
3046 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3047 if ((list
= np
->info
) != NULL
)
3048 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3053 /* np->lock, For debugging. */
3054 zebra_route_char(rinfo
->type
),
3055 rip_route_type_print(rinfo
->sub_type
),
3061 vty_out(vty
, "%*s", len
, " ");
3063 switch (rinfo
->nh
.type
) {
3064 case NEXTHOP_TYPE_IPV4
:
3065 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3066 vty_out(vty
, "%-20pI4 %2d ",
3067 &rinfo
->nh
.gate
.ipv4
,
3070 case NEXTHOP_TYPE_IFINDEX
:
3075 case NEXTHOP_TYPE_BLACKHOLE
:
3080 case NEXTHOP_TYPE_IPV6
:
3081 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3083 "V6 Address Hidden %2d ",
3088 /* Route which exist in kernel routing table. */
3089 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3090 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3091 vty_out(vty
, "%-15pI4 ",
3093 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3094 (route_tag_t
)rinfo
->tag
);
3095 rip_vty_out_uptime(vty
, rinfo
);
3096 } else if (rinfo
->metric
3097 == RIP_METRIC_INFINITY
) {
3098 vty_out(vty
, "self ");
3099 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3100 (route_tag_t
)rinfo
->tag
);
3101 rip_vty_out_uptime(vty
, rinfo
);
3103 if (rinfo
->external_metric
) {
3105 vty
, "self (%s:%d)",
3108 rinfo
->external_metric
);
3111 vty_out(vty
, "%*s", len
,
3116 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3117 (route_tag_t
)rinfo
->tag
);
3125 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3126 DEFUN (show_ip_rip_status
,
3127 show_ip_rip_status_cmd
,
3128 "show ip rip [vrf NAME] status",
3133 "IP routing protocol process parameters and statistics\n")
3136 struct interface
*ifp
;
3137 struct rip_interface
*ri
;
3138 extern const struct message ri_version_msg
[];
3139 const char *send_version
;
3140 const char *receive_version
;
3141 const char *vrf_name
;
3144 if (argv_find(argv
, argc
, "vrf", &idx
))
3145 vrf_name
= argv
[idx
+ 1]->arg
;
3147 vrf_name
= VRF_DEFAULT_NAME
;
3149 rip
= rip_lookup_by_vrf_name(vrf_name
);
3151 vty_out(vty
, "%% RIP instance not found\n");
3154 if (!rip
->enabled
) {
3155 vty_out(vty
, "%% RIP instance is disabled\n");
3159 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3160 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3162 vty_out(vty
, " next due in %lu seconds\n",
3163 thread_timer_remain_second(rip
->t_update
));
3164 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3165 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3167 /* Filtering status show. */
3168 config_show_distribute(vty
, rip
->distribute_ctx
);
3170 /* Default metric information. */
3171 vty_out(vty
, " Default redistribution metric is %u\n",
3172 rip
->default_metric
);
3174 /* Redistribute information. */
3175 vty_out(vty
, " Redistributing:");
3176 rip_show_redistribute_config(vty
, rip
);
3179 vty_out(vty
, " Default version control: send version %s,",
3180 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3181 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3182 vty_out(vty
, " receive any version \n");
3184 vty_out(vty
, " receive version %s \n",
3185 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3187 vty_out(vty
, " Interface Send Recv Key-chain\n");
3189 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3195 if (ri
->enable_network
|| ri
->enable_interface
) {
3196 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3198 lookup_msg(ri_version_msg
,
3199 rip
->version_send
, NULL
);
3201 send_version
= lookup_msg(ri_version_msg
,
3204 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3206 lookup_msg(ri_version_msg
,
3207 rip
->version_recv
, NULL
);
3209 receive_version
= lookup_msg(
3210 ri_version_msg
, ri
->ri_receive
, NULL
);
3212 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3213 send_version
, receive_version
,
3214 ri
->key_chain
? ri
->key_chain
: "");
3218 vty_out(vty
, " Routing for Networks:\n");
3219 rip_show_network_config(vty
, rip
);
3222 int found_passive
= 0;
3223 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3226 if ((ri
->enable_network
|| ri
->enable_interface
)
3228 if (!found_passive
) {
3230 " Passive Interface(s):\n");
3233 vty_out(vty
, " %s\n", ifp
->name
);
3238 vty_out(vty
, " Routing Information Sources:\n");
3240 " Gateway BadPackets BadRoutes Distance Last Update\n");
3241 rip_peer_display(vty
, rip
);
3243 rip_distance_show(vty
, rip
);
3248 /* RIP configuration write function. */
3249 static int config_write_rip(struct vty
*vty
)
3254 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3255 char xpath
[XPATH_MAXLEN
];
3256 struct lyd_node
*dnode
;
3258 snprintf(xpath
, sizeof(xpath
),
3259 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3261 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3264 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3266 /* Distribute configuration. */
3267 config_write_distribute(vty
, rip
->distribute_ctx
);
3269 /* Interface routemap configuration */
3270 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3272 vty_out(vty
, "exit\n");
3280 static int config_write_rip(struct vty
*vty
);
3281 /* RIP node structure. */
3282 static struct cmd_node rip_node
= {
3285 .parent_node
= CONFIG_NODE
,
3286 .prompt
= "%s(config-router)# ",
3287 .config_write
= config_write_rip
,
3290 /* Distribute-list update functions. */
3291 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3292 struct distribute
*dist
)
3294 struct interface
*ifp
;
3295 struct rip_interface
*ri
;
3296 struct access_list
*alist
;
3297 struct prefix_list
*plist
;
3299 if (!ctx
->vrf
|| !dist
->ifname
)
3302 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3308 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3309 alist
= access_list_lookup(AFI_IP
,
3310 dist
->list
[DISTRIBUTE_V4_IN
]);
3312 ri
->list
[RIP_FILTER_IN
] = alist
;
3314 ri
->list
[RIP_FILTER_IN
] = NULL
;
3316 ri
->list
[RIP_FILTER_IN
] = NULL
;
3318 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3319 alist
= access_list_lookup(AFI_IP
,
3320 dist
->list
[DISTRIBUTE_V4_OUT
]);
3322 ri
->list
[RIP_FILTER_OUT
] = alist
;
3324 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3326 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3328 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3329 plist
= prefix_list_lookup(AFI_IP
,
3330 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3332 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3334 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3336 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3338 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3339 plist
= prefix_list_lookup(AFI_IP
,
3340 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3342 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3344 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3346 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3349 void rip_distribute_update_interface(struct interface
*ifp
)
3351 struct rip_interface
*ri
= ifp
->info
;
3352 struct rip
*rip
= ri
->rip
;
3353 struct distribute
*dist
;
3357 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3359 rip_distribute_update(rip
->distribute_ctx
, dist
);
3362 /* Update all interface's distribute list. */
3364 static void rip_distribute_update_all(struct prefix_list
*notused
)
3366 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3367 struct interface
*ifp
;
3369 FOR_ALL_INTERFACES (vrf
, ifp
)
3370 rip_distribute_update_interface(ifp
);
3373 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3375 rip_distribute_update_all(NULL
);
3378 /* Delete all added rip route. */
3379 void rip_clean(struct rip
*rip
)
3381 rip_interfaces_clean(rip
);
3384 rip_instance_disable(rip
);
3386 stream_free(rip
->obuf
);
3388 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3389 if (rip
->redist
[i
].route_map
.name
)
3390 free(rip
->redist
[i
].route_map
.name
);
3392 route_table_finish(rip
->table
);
3393 route_table_finish(rip
->neighbor
);
3394 list_delete(&rip
->peer_list
);
3395 distribute_list_delete(&rip
->distribute_ctx
);
3396 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3398 rip_clean_network(rip
);
3399 rip_passive_nondefault_clean(rip
);
3400 vector_free(rip
->enable_interface
);
3401 route_table_finish(rip
->enable_network
);
3402 vector_free(rip
->passive_nondefault
);
3403 list_delete(&rip
->offset_list_master
);
3404 route_table_finish(rip
->distance_table
);
3406 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3407 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3408 XFREE(MTYPE_RIP
, rip
);
3411 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3412 struct if_rmap
*if_rmap
)
3414 struct interface
*ifp
= NULL
;
3415 struct rip_interface
*ri
;
3416 struct route_map
*rmap
;
3417 struct vrf
*vrf
= NULL
;
3420 vrf
= vrf_lookup_by_name(ctx
->name
);
3422 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3427 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3428 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3430 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3432 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3434 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3436 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3437 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3439 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3441 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3443 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3446 void rip_if_rmap_update_interface(struct interface
*ifp
)
3448 struct rip_interface
*ri
= ifp
->info
;
3449 struct rip
*rip
= ri
->rip
;
3450 struct if_rmap
*if_rmap
;
3451 struct if_rmap_ctx
*ctx
;
3455 ctx
= rip
->if_rmap_ctx
;
3458 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3460 rip_if_rmap_update(ctx
, if_rmap
);
3463 static void rip_routemap_update_redistribute(struct rip
*rip
)
3465 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3466 if (rip
->redist
[i
].route_map
.name
) {
3467 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3468 rip
->redist
[i
].route_map
.name
);
3469 route_map_counter_increment(
3470 rip
->redist
[i
].route_map
.map
);
3476 static void rip_routemap_update(const char *notused
)
3478 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3480 struct interface
*ifp
;
3482 FOR_ALL_INTERFACES (vrf
, ifp
)
3483 rip_if_rmap_update_interface(ifp
);
3487 rip_routemap_update_redistribute(rip
);
3490 /* Link RIP instance to VRF. */
3491 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3493 struct interface
*ifp
;
3496 rip
->distribute_ctx
->vrf
= vrf
;
3499 FOR_ALL_INTERFACES (vrf
, ifp
)
3500 rip_interface_sync(ifp
);
3503 /* Unlink RIP instance from VRF. */
3504 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3506 struct interface
*ifp
;
3509 rip
->distribute_ctx
->vrf
= NULL
;
3512 FOR_ALL_INTERFACES (vrf
, ifp
)
3513 rip_interface_sync(ifp
);
3516 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3520 rip_vrf_link(rip
, vrf
);
3521 rip
->enabled
= true;
3523 /* Resend all redistribute requests. */
3524 rip_redistribute_enable(rip
);
3526 /* Create read and timer thread. */
3527 rip_event(rip
, RIP_READ
, rip
->sock
);
3528 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3530 rip_zebra_vrf_register(vrf
);
3533 static void rip_instance_disable(struct rip
*rip
)
3535 struct vrf
*vrf
= rip
->vrf
;
3536 struct route_node
*rp
;
3538 /* Clear RIP routes */
3539 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3540 struct rip_info
*rinfo
;
3542 struct listnode
*listnode
;
3544 if ((list
= rp
->info
) == NULL
)
3547 rinfo
= listgetdata(listhead(list
));
3548 if (rip_route_rte(rinfo
))
3549 rip_zebra_ipv4_delete(rip
, rp
);
3551 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3552 RIP_TIMER_OFF(rinfo
->t_timeout
);
3553 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3554 rip_info_free(rinfo
);
3558 route_unlock_node(rp
);
3561 /* Flush all redistribute requests. */
3562 rip_redistribute_disable(rip
);
3564 /* Cancel RIP related timers. */
3565 RIP_TIMER_OFF(rip
->t_update
);
3566 RIP_TIMER_OFF(rip
->t_triggered_update
);
3567 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3569 /* Cancel read thread. */
3570 thread_cancel(&rip
->t_read
);
3572 /* Close RIP socket. */
3576 /* Clear existing peers. */
3577 list_delete_all_node(rip
->peer_list
);
3579 rip_zebra_vrf_deregister(vrf
);
3581 rip_vrf_unlink(rip
, vrf
);
3582 rip
->enabled
= false;
3585 static int rip_vrf_new(struct vrf
*vrf
)
3587 if (IS_RIP_DEBUG_EVENT
)
3588 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3594 static int rip_vrf_delete(struct vrf
*vrf
)
3596 if (IS_RIP_DEBUG_EVENT
)
3597 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3603 static int rip_vrf_enable(struct vrf
*vrf
)
3608 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3610 char *old_vrf_name
= NULL
;
3612 rip
= (struct rip
*)vrf
->info
;
3615 /* update vrf name */
3617 old_vrf_name
= rip
->vrf_name
;
3618 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf
->name
);
3620 * HACK: Change the RIP VRF in the running configuration directly,
3621 * bypassing the northbound layer. This is necessary to avoid deleting
3622 * the RIP and readding it in the new VRF, which would have
3623 * several implications.
3625 if (yang_module_find("frr-ripd") && old_vrf_name
) {
3626 struct lyd_node
*rip_dnode
;
3627 char oldpath
[XPATH_MAXLEN
];
3628 char newpath
[XPATH_MAXLEN
];
3630 rip_dnode
= yang_dnode_getf(
3631 running_config
->dnode
,
3632 "/frr-ripd:ripd/instance[vrf='%s']/vrf",
3635 yang_dnode_get_path(lyd_parent(rip_dnode
),
3636 oldpath
, sizeof(oldpath
));
3637 yang_dnode_change_leaf(rip_dnode
, vrf
->name
);
3638 yang_dnode_get_path(lyd_parent(rip_dnode
),
3639 newpath
, sizeof(newpath
));
3640 nb_running_move_tree(oldpath
, newpath
);
3641 running_config
->version
++;
3644 XFREE(MTYPE_RIP_VRF_NAME
, old_vrf_name
);
3646 if (!rip
|| rip
->enabled
)
3649 if (IS_RIP_DEBUG_EVENT
)
3650 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3653 /* Activate the VRF RIP instance. */
3654 if (!rip
->enabled
) {
3655 socket
= rip_create_socket(vrf
);
3659 rip_instance_enable(rip
, vrf
, socket
);
3665 static int rip_vrf_disable(struct vrf
*vrf
)
3669 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3670 if (!rip
|| !rip
->enabled
)
3673 if (IS_RIP_DEBUG_EVENT
)
3674 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3677 /* Deactivate the VRF RIP instance. */
3679 rip_instance_disable(rip
);
3684 void rip_vrf_init(void)
3686 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3692 void rip_vrf_terminate(void)
3697 /* Allocate new rip structure and set default value. */
3700 /* Install top nodes. */
3701 install_node(&rip_node
);
3703 /* Install rip commands. */
3704 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3705 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3707 install_default(RIP_NODE
);
3709 /* Debug related init. */
3712 /* Access list install. */
3714 access_list_add_hook(rip_distribute_update_all_wrapper
);
3715 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3717 /* Prefix list initialize.*/
3719 prefix_list_add_hook(rip_distribute_update_all
);
3720 prefix_list_delete_hook(rip_distribute_update_all
);
3723 rip_route_map_init();
3725 route_map_add_hook(rip_routemap_update
);
3726 route_map_delete_hook(rip_routemap_update
);
3728 if_rmap_init(RIP_NODE
);