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 "ripd/ripd.h"
49 #include "ripd/rip_nb.h"
50 #include "ripd/rip_debug.h"
51 #include "ripd/rip_errors.h"
52 #include "ripd/rip_interface.h"
54 /* UDP receive buffer size */
55 #define RIP_UDP_RCV_BUF 41600
57 DEFINE_MGROUP(RIPD
, "ripd")
58 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure")
59 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name")
60 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info")
61 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance")
64 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
66 static int rip_triggered_update(struct thread
*);
67 static int rip_update_jitter(unsigned long);
68 static void rip_distance_table_node_cleanup(struct route_table
*table
,
69 struct route_node
*node
);
70 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
71 static void rip_instance_disable(struct rip
*rip
);
73 static void rip_distribute_update(struct distribute_ctx
*ctx
,
74 struct distribute
*dist
);
76 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
77 struct if_rmap
*if_rmap
);
79 /* RIP output routes type. */
80 enum { rip_all_route
, rip_changed_route
};
82 /* RIP command strings. */
83 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
84 {RIP_RESPONSE
, "RESPONSE"},
85 {RIP_TRACEON
, "TRACEON"},
86 {RIP_TRACEOFF
, "TRACEOFF"},
88 {RIP_POLL_ENTRY
, "POLL ENTRY"},
91 /* Generate rb-tree of RIP instances. */
92 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
94 return strcmp(a
->vrf_name
, b
->vrf_name
);
96 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
98 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
100 /* Utility function to set boradcast option to the socket. */
101 static int sockopt_broadcast(int sock
)
106 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
109 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
115 int rip_route_rte(struct rip_info
*rinfo
)
117 return (rinfo
->type
== ZEBRA_ROUTE_RIP
118 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
121 static struct rip_info
*rip_info_new(void)
123 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
126 void rip_info_free(struct rip_info
*rinfo
)
128 XFREE(MTYPE_RIP_INFO
, rinfo
);
131 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
133 return route_table_get_info(rinfo
->rp
->table
);
136 /* RIP route garbage collect timer. */
137 static int rip_garbage_collect(struct thread
*t
)
139 struct rip_info
*rinfo
;
140 struct route_node
*rp
;
142 rinfo
= THREAD_ARG(t
);
143 rinfo
->t_garbage_collect
= NULL
;
145 /* Off timeout timer. */
146 RIP_TIMER_OFF(rinfo
->t_timeout
);
148 /* Get route_node pointer. */
151 /* Unlock route_node. */
152 listnode_delete(rp
->info
, rinfo
);
153 if (list_isempty((struct list
*)rp
->info
)) {
154 list_delete((struct list
**)&rp
->info
);
155 route_unlock_node(rp
);
158 /* Free RIP routing information. */
159 rip_info_free(rinfo
);
164 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
166 /* Add new route to the ECMP list.
167 * RETURN: the new entry added in the list, or NULL if it is not the first
168 * entry and ECMP is not allowed.
170 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
172 struct route_node
*rp
= rinfo_new
->rp
;
173 struct rip_info
*rinfo
= NULL
;
174 struct list
*list
= NULL
;
176 if (rp
->info
== NULL
)
177 rp
->info
= list_new();
178 list
= (struct list
*)rp
->info
;
180 /* If ECMP is not allowed and some entry already exists in the list,
182 if (listcount(list
) && !rip
->ecmp
)
185 rinfo
= rip_info_new();
186 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
187 listnode_add(list
, rinfo
);
189 if (rip_route_rte(rinfo
)) {
190 rip_timeout_update(rip
, rinfo
);
191 rip_zebra_ipv4_add(rip
, rp
);
194 /* Set the route change flag on the first entry. */
195 rinfo
= listgetdata(listhead(list
));
196 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
198 /* Signal the output process to trigger an update (see section 2.5). */
199 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
204 /* Replace the ECMP list with the new route.
205 * RETURN: the new entry added in the list
207 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
209 struct route_node
*rp
= rinfo_new
->rp
;
210 struct list
*list
= (struct list
*)rp
->info
;
211 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
212 struct listnode
*node
= NULL
, *nextnode
= NULL
;
214 if (list
== NULL
|| listcount(list
) == 0)
215 return rip_ecmp_add(rip
, rinfo_new
);
217 /* Get the first entry */
218 rinfo
= listgetdata(listhead(list
));
220 /* Learnt route replaced by a local one. Delete it from zebra. */
221 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
222 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
223 rip_zebra_ipv4_delete(rip
, rp
);
225 /* Re-use the first entry, and delete the others. */
226 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
227 if (tmp_rinfo
!= rinfo
) {
228 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
229 RIP_TIMER_OFF(tmp_rinfo
->t_garbage_collect
);
230 list_delete_node(list
, node
);
231 rip_info_free(tmp_rinfo
);
234 RIP_TIMER_OFF(rinfo
->t_timeout
);
235 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
236 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
238 if (rip_route_rte(rinfo
)) {
239 rip_timeout_update(rip
, rinfo
);
240 /* The ADD message implies an update. */
241 rip_zebra_ipv4_add(rip
, rp
);
244 /* Set the route change flag. */
245 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
247 /* Signal the output process to trigger an update (see section 2.5). */
248 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
253 /* Delete one route from the ECMP list.
255 * null - the entry is freed, and other entries exist in the list
256 * the entry - the entry is the last one in the list; its metric is set
257 * to INFINITY, and the garbage collector is started for it
259 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
261 struct route_node
*rp
= rinfo
->rp
;
262 struct list
*list
= (struct list
*)rp
->info
;
264 RIP_TIMER_OFF(rinfo
->t_timeout
);
266 if (listcount(list
) > 1) {
267 /* Some other ECMP entries still exist. Just delete this entry.
269 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
270 listnode_delete(list
, rinfo
);
271 if (rip_route_rte(rinfo
)
272 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
273 /* The ADD message implies the update. */
274 rip_zebra_ipv4_add(rip
, rp
);
275 rip_info_free(rinfo
);
278 assert(rinfo
== listgetdata(listhead(list
)));
280 /* This is the only entry left in the list. We must keep it in
281 * the list for garbage collection time, with INFINITY metric.
284 rinfo
->metric
= RIP_METRIC_INFINITY
;
285 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
288 if (rip_route_rte(rinfo
)
289 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
290 rip_zebra_ipv4_delete(rip
, rp
);
293 /* Set the route change flag on the first entry. */
294 rinfo
= listgetdata(listhead(list
));
295 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
297 /* Signal the output process to trigger an update (see section 2.5). */
298 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
303 /* Timeout RIP routes. */
304 static int rip_timeout(struct thread
*t
)
306 struct rip_info
*rinfo
= THREAD_ARG(t
);
307 struct rip
*rip
= rip_info_get_instance(rinfo
);
309 rip_ecmp_delete(rip
, rinfo
);
314 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
316 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
317 RIP_TIMER_OFF(rinfo
->t_timeout
);
318 thread_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
323 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
324 struct rip_interface
*ri
)
326 struct distribute
*dist
;
327 struct access_list
*alist
;
328 struct prefix_list
*plist
;
329 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
331 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
333 /* Input distribute-list filtering. */
334 if (ri
->list
[rip_distribute
]) {
335 if (access_list_apply(ri
->list
[rip_distribute
],
338 if (IS_RIP_DEBUG_PACKET
)
339 zlog_debug("%s/%d filtered by distribute %s",
340 inet_ntoa(p
->prefix
), p
->prefixlen
,
345 if (ri
->prefix
[rip_distribute
]) {
346 if (prefix_list_apply(ri
->prefix
[rip_distribute
],
349 if (IS_RIP_DEBUG_PACKET
)
350 zlog_debug("%s/%d filtered by prefix-list %s",
351 inet_ntoa(p
->prefix
), p
->prefixlen
,
357 /* All interface filter check. */
358 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
360 if (dist
->list
[distribute
]) {
361 alist
= access_list_lookup(AFI_IP
,
362 dist
->list
[distribute
]);
365 if (access_list_apply(alist
, (struct prefix
*)p
)
367 if (IS_RIP_DEBUG_PACKET
)
369 "%s/%d filtered by distribute %s",
370 inet_ntoa(p
->prefix
),
371 p
->prefixlen
, inout
);
376 if (dist
->prefix
[distribute
]) {
377 plist
= prefix_list_lookup(AFI_IP
,
378 dist
->prefix
[distribute
]);
381 if (prefix_list_apply(plist
, (struct prefix
*)p
)
383 if (IS_RIP_DEBUG_PACKET
)
385 "%s/%d filtered by prefix-list %s",
386 inet_ntoa(p
->prefix
),
387 p
->prefixlen
, inout
);
396 /* Check nexthop address validity. */
397 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
399 struct interface
*ifp
;
400 struct listnode
*cnode
;
401 struct connected
*ifc
;
404 /* If nexthop address matches local configured address then it is
407 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
408 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
411 if (p
->family
== AF_INET
412 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
419 /* RIP add route to routing table. */
420 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
421 struct interface
*ifp
)
425 struct prefix_ipv4 p
;
426 struct route_node
*rp
;
427 struct rip_info
*rinfo
= NULL
, newinfo
;
428 struct rip_interface
*ri
;
429 struct in_addr
*nexthop
;
431 unsigned char old_dist
, new_dist
;
432 struct list
*list
= NULL
;
433 struct listnode
*node
= NULL
;
435 /* Make prefix structure. */
436 memset(&p
, 0, sizeof(struct prefix_ipv4
));
438 p
.prefix
= rte
->prefix
;
439 p
.prefixlen
= ip_masklen(rte
->mask
);
441 /* Make sure mask is applied. */
447 /* Apply input filters. */
448 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
452 memset(&newinfo
, 0, sizeof(newinfo
));
453 newinfo
.type
= ZEBRA_ROUTE_RIP
;
454 newinfo
.sub_type
= RIP_ROUTE_RTE
;
455 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
456 newinfo
.from
= from
->sin_addr
;
457 newinfo
.nh
.ifindex
= ifp
->ifindex
;
458 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
459 newinfo
.metric
= rte
->metric
;
460 newinfo
.metric_out
= rte
->metric
; /* XXX */
461 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
463 /* Modify entry according to the interface routemap. */
464 if (ri
->routemap
[RIP_FILTER_IN
]) {
465 /* The object should be of the type of rip_info */
466 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
467 (struct prefix
*)&p
, RMAP_RIP
, &newinfo
);
469 if (ret
== RMAP_DENYMATCH
) {
470 if (IS_RIP_DEBUG_PACKET
)
472 "RIP %s/%d is filtered by route-map in",
473 inet_ntoa(p
.prefix
), p
.prefixlen
);
477 /* Get back the object */
478 rte
->nexthop
= newinfo
.nexthop_out
;
479 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
480 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
484 /* Once the entry has been validated, update the metric by
485 adding the cost of the network on wich the message
486 arrived. If the result is greater than infinity, use infinity
487 (RFC2453 Sec. 3.9.2) */
488 /* Zebra ripd can handle offset-list in. */
489 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
491 /* If offset-list does not modify the metric use interface's
494 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
496 if (rte
->metric
> RIP_METRIC_INFINITY
)
497 rte
->metric
= RIP_METRIC_INFINITY
;
499 /* Set nexthop pointer. */
500 if (rte
->nexthop
.s_addr
== 0)
501 nexthop
= &from
->sin_addr
;
503 nexthop
= &rte
->nexthop
;
505 /* Check if nexthop address is myself, then do nothing. */
506 if (rip_nexthop_check(rip
, nexthop
) < 0) {
507 if (IS_RIP_DEBUG_PACKET
)
508 zlog_debug("Nexthop address %s is myself",
509 inet_ntoa(*nexthop
));
513 /* Get index for the prefix. */
514 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
517 newinfo
.nh
.gate
.ipv4
= *nexthop
;
518 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
519 newinfo
.metric
= rte
->metric
;
520 newinfo
.tag
= ntohs(rte
->tag
);
521 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
523 new_dist
= newinfo
.distance
? newinfo
.distance
524 : ZEBRA_RIP_DISTANCE_DEFAULT
;
526 /* Check to see whether there is already RIP route on the table. */
527 if ((list
= rp
->info
) != NULL
)
528 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
529 /* Need to compare with redistributed entry or local
531 if (!rip_route_rte(rinfo
))
534 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
535 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
538 if (!listnextnode(node
)) {
539 /* Not found in the list */
541 if (rte
->metric
> rinfo
->metric
) {
542 /* New route has a greater metric.
544 route_unlock_node(rp
);
548 if (rte
->metric
< rinfo
->metric
)
549 /* New route has a smaller metric.
550 * Replace the ECMP list
551 * with the new one in below. */
554 /* Metrics are same. We compare the distances.
556 old_dist
= rinfo
->distance
558 : ZEBRA_RIP_DISTANCE_DEFAULT
;
560 if (new_dist
> old_dist
) {
561 /* New route has a greater distance.
563 route_unlock_node(rp
);
567 if (new_dist
< old_dist
)
568 /* New route has a smaller distance.
569 * Replace the ECMP list
570 * with the new one in below. */
573 /* Metrics and distances are both same. Keep
575 * the new route is added in the ECMP list in
581 /* Local static route. */
582 if (rinfo
->type
== ZEBRA_ROUTE_RIP
583 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
584 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
585 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
586 route_unlock_node(rp
);
590 /* Redistributed route check. */
591 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
592 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
593 old_dist
= rinfo
->distance
;
594 /* Only routes directly connected to an interface
596 * may have a valid NULL distance */
597 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= 0)
600 : ZEBRA_RIP_DISTANCE_DEFAULT
;
601 /* If imported route does not have STRICT precedence,
602 mark it as a ghost */
603 if (new_dist
<= old_dist
604 && rte
->metric
!= RIP_METRIC_INFINITY
)
605 rip_ecmp_replace(rip
, &newinfo
);
607 route_unlock_node(rp
);
614 route_unlock_node(rp
);
616 /* Now, check to see whether there is already an explicit route
617 for the destination prefix. If there is no such route, add
618 this route to the routing table, unless the metric is
619 infinity (there is no point in adding a route which
621 if (rte
->metric
!= RIP_METRIC_INFINITY
)
622 rip_ecmp_add(rip
, &newinfo
);
624 /* Route is there but we are not sure the route is RIP or not.
627 /* If there is an existing route, compare the next hop address
628 to the address of the router from which the datagram came.
629 If this datagram is from the same router as the existing
630 route, reinitialize the timeout. */
631 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
632 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
634 old_dist
= rinfo
->distance
? rinfo
->distance
635 : ZEBRA_RIP_DISTANCE_DEFAULT
;
637 /* Next, compare the metrics. If the datagram is from the same
638 router as the existing route, and the new metric is different
639 than the old one; or, if the new metric is lower than the old
640 one, or if the tag has been changed; or if there is a route
641 with a lower administrave distance; or an update of the
642 distance on the actual route; do the following actions: */
643 if ((same
&& rinfo
->metric
!= rte
->metric
)
644 || (rte
->metric
< rinfo
->metric
)
645 || ((same
) && (rinfo
->metric
== rte
->metric
)
646 && (newinfo
.tag
!= rinfo
->tag
))
647 || (old_dist
> new_dist
)
648 || ((old_dist
!= new_dist
) && same
)) {
649 if (listcount(list
) == 1) {
650 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
651 rip_ecmp_replace(rip
, &newinfo
);
653 rip_ecmp_delete(rip
, rinfo
);
655 if (newinfo
.metric
< rinfo
->metric
)
656 rip_ecmp_replace(rip
, &newinfo
);
657 else if (newinfo
.metric
> rinfo
->metric
)
658 rip_ecmp_delete(rip
, rinfo
);
659 else if (new_dist
< old_dist
)
660 rip_ecmp_replace(rip
, &newinfo
);
661 else if (new_dist
> old_dist
)
662 rip_ecmp_delete(rip
, rinfo
);
664 int update
= CHECK_FLAG(rinfo
->flags
,
669 assert(newinfo
.metric
670 != RIP_METRIC_INFINITY
);
672 RIP_TIMER_OFF(rinfo
->t_timeout
);
673 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
674 memcpy(rinfo
, &newinfo
,
675 sizeof(struct rip_info
));
676 rip_timeout_update(rip
, rinfo
);
679 rip_zebra_ipv4_add(rip
, rp
);
681 /* - Set the route change flag on the
683 rinfo
= listgetdata(listhead(list
));
684 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
685 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
688 } else /* same & no change */
689 rip_timeout_update(rip
, rinfo
);
691 /* Unlock tempolary lock of the route. */
692 route_unlock_node(rp
);
696 /* Dump RIP packet */
697 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
702 const char *command_str
;
703 char pbuf
[BUFSIZ
], nbuf
[BUFSIZ
];
707 /* Set command string. */
708 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
709 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
711 command_str
= "unknown";
713 /* Dump packet header. */
714 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
715 packet
->version
, size
);
717 /* Dump each routing table entry. */
720 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
721 if (packet
->version
== RIPv2
) {
722 netmask
= ip_masklen(rte
->mask
);
724 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
726 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
727 p
= (uint8_t *)&rte
->prefix
;
730 " family 0x%X type %d auth string: %s",
733 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
734 struct rip_md5_info
*md5
;
736 md5
= (struct rip_md5_info
*)&packet
740 " family 0x%X type %d (MD5 authentication)",
744 " RIP-2 packet len %d Key ID %d"
746 ntohs(md5
->packet_len
),
747 md5
->keyid
, md5
->auth_len
);
748 zlog_debug(" Sequence Number %ld",
749 (unsigned long)ntohl(
751 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
752 p
= (uint8_t *)&rte
->prefix
;
755 " family 0x%X type %d (MD5 data)",
759 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
760 "%02X%02X%02X%02X%02X%02X%02X%02X",
761 p
[0], p
[1], p
[2], p
[3], p
[4],
762 p
[5], p
[6], p
[7], p
[8], p
[9],
763 p
[10], p
[11], p
[12], p
[13],
767 " family 0x%X type %d (Unknown auth type)",
773 " %s/%d -> %s family %d tag %" ROUTE_TAG_PRI
775 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
,
778 inet_ntop(AF_INET
, &rte
->nexthop
, nbuf
,
781 (route_tag_t
)ntohs(rte
->tag
),
782 (unsigned long)ntohl(rte
->metric
));
785 " %s family %d tag %" ROUTE_TAG_PRI
787 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
, BUFSIZ
),
789 (route_tag_t
)ntohs(rte
->tag
),
790 (unsigned long)ntohl(rte
->metric
));
795 /* Check if the destination address is valid (unicast; not net 0
796 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
797 check net 0 because we accept default route. */
798 static int rip_destination_check(struct in_addr addr
)
800 uint32_t destination
;
802 /* Convert to host byte order. */
803 destination
= ntohl(addr
.s_addr
);
805 if (IPV4_NET127(destination
))
808 /* Net 0 may match to the default route. */
809 if (IPV4_NET0(destination
) && destination
!= 0)
812 /* Unicast address must belong to class A, B, C. */
813 if (IN_CLASSA(destination
))
815 if (IN_CLASSB(destination
))
817 if (IN_CLASSC(destination
))
823 /* RIP version 2 authentication. */
824 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
825 struct interface
*ifp
)
827 struct rip_interface
*ri
;
828 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
831 /* reject passwords with zeros in the middle of the string */
832 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
833 if (auth_str
[i
] != '\0')
837 if (IS_RIP_DEBUG_EVENT
)
838 zlog_debug("RIPv2 simple password authentication from %s",
839 inet_ntoa(from
->sin_addr
));
843 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
844 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
847 /* Simple password authentication. */
849 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
853 struct keychain
*keychain
;
856 keychain
= keychain_lookup(ri
->key_chain
);
857 if (keychain
== NULL
|| keychain
->key
== NULL
)
860 key
= key_match_for_accept(keychain
, auth_str
);
867 /* RIP version 2 authentication with MD5. */
868 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
869 int length
, struct interface
*ifp
)
871 struct rip_interface
*ri
;
872 struct rip_md5_info
*md5
;
873 struct rip_md5_data
*md5data
;
874 struct keychain
*keychain
;
876 #ifdef CRYPTO_OPENSSL
878 #elif CRYPTO_INTERNAL
881 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
883 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
885 if (IS_RIP_DEBUG_EVENT
)
886 zlog_debug("RIPv2 MD5 authentication from %s",
887 inet_ntoa(from
->sin_addr
));
890 md5
= (struct rip_md5_info
*)&packet
->rte
;
892 /* Check auth type. */
893 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
896 /* If the authentication length is less than 16, then it must be wrong
898 * any interpretation of rfc2082. Some implementations also interpret
899 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
900 * RIP_AUTH_MD5_COMPAT_SIZE.
902 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
903 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
904 if (IS_RIP_DEBUG_EVENT
)
906 "RIPv2 MD5 authentication, strange authentication "
912 /* grab and verify check packet length */
913 packet_len
= ntohs(md5
->packet_len
);
915 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
916 if (IS_RIP_DEBUG_EVENT
)
918 "RIPv2 MD5 authentication, packet length field %d "
919 "greater than received length %d!",
920 md5
->packet_len
, length
);
924 /* retrieve authentication data */
925 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
928 keychain
= keychain_lookup(ri
->key_chain
);
929 if (keychain
== NULL
)
932 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
933 if (key
== NULL
|| key
->string
== NULL
)
936 strlcpy(auth_str
, key
->string
, sizeof(auth_str
));
937 } else if (ri
->auth_str
)
938 strlcpy(auth_str
, ri
->auth_str
, sizeof(auth_str
));
940 if (auth_str
[0] == 0)
943 /* MD5 digest authentication. */
944 #ifdef CRYPTO_OPENSSL
945 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
946 ctx
= EVP_MD_CTX_new();
947 EVP_DigestInit(ctx
, EVP_md5());
948 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
949 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
950 EVP_DigestFinal(ctx
, digest
, &md5_size
);
951 EVP_MD_CTX_free(ctx
);
952 #elif CRYPTO_INTERNAL
953 memset(&ctx
, 0, sizeof(ctx
));
955 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
956 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
957 MD5Final(digest
, &ctx
);
960 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
966 /* Pick correct auth string for sends, prepare auth_str buffer for use.
967 * (left justified and padded).
969 * presumes one of ri or key is valid, and that the auth strings they point
970 * to are nul terminated. If neither are present, auth_str will be fully
974 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
975 char *auth_str
, int len
)
979 memset(auth_str
, 0, len
);
980 if (key
&& key
->string
)
981 strlcpy(auth_str
, key
->string
, len
);
982 else if (ri
->auth_str
)
983 strlcpy(auth_str
, ri
->auth_str
, len
);
988 /* Write RIPv2 simple password authentication information
990 * auth_str is presumed to be 2 bytes and correctly prepared
991 * (left justified and zero padded).
993 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
995 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
997 stream_putw(s
, RIP_FAMILY_AUTH
);
998 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
999 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
1004 /* write RIPv2 MD5 "authentication header"
1005 * (uses the auth key data field)
1007 * Digest offset field is set to 0.
1009 * returns: offset of the digest offset field, which must be set when
1010 * length to the auth-data MD5 digest is known.
1012 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1017 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1019 /* MD5 authentication. */
1020 stream_putw(s
, RIP_FAMILY_AUTH
);
1021 stream_putw(s
, RIP_AUTH_MD5
);
1023 /* MD5 AH digest offset field.
1025 * Set to placeholder value here, to true value when RIP-2 Packet length
1026 * is known. Actual value is set in .....().
1028 doff
= stream_get_endp(s
);
1033 stream_putc(s
, key
->index
% 256);
1037 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1038 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1040 * to be configurable.
1042 stream_putc(s
, ri
->md5_auth_len
);
1044 /* Sequence Number (non-decreasing). */
1045 /* RFC2080: The value used in the sequence number is
1046 arbitrary, but two suggestions are the time of the
1047 message's creation or a simple message counter. */
1048 stream_putl(s
, time(NULL
));
1050 /* Reserved field must be zero. */
1057 /* If authentication is in used, write the appropriate header
1058 * returns stream offset to which length must later be written
1059 * or 0 if this is not required
1061 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1062 struct key
*key
, char *auth_str
, int len
)
1064 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1066 switch (ri
->auth_type
) {
1067 case RIP_AUTH_SIMPLE_PASSWORD
:
1068 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1069 rip_auth_simple_write(s
, auth_str
, len
);
1072 return rip_auth_md5_ah_write(s
, ri
, key
);
1078 /* Write RIPv2 MD5 authentication data trailer */
1079 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1080 size_t doff
, char *auth_str
, int authlen
)
1083 #ifdef CRYPTO_OPENSSL
1085 #elif CRYPTO_INTERNAL
1088 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1090 /* Make it sure this interface is configured as MD5
1092 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1093 && (authlen
== RIP_AUTH_MD5_SIZE
));
1096 /* Get packet length. */
1097 len
= stream_get_endp(s
);
1099 /* Check packet length. */
1100 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1103 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1108 /* Set the digest offset length in the header */
1109 stream_putw_at(s
, doff
, len
);
1111 /* Set authentication data. */
1112 stream_putw(s
, RIP_FAMILY_AUTH
);
1113 stream_putw(s
, RIP_AUTH_DATA
);
1115 /* Generate a digest for the RIP packet. */
1116 #ifdef CRYPTO_OPENSSL
1117 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1118 ctx
= EVP_MD_CTX_new();
1119 EVP_DigestInit(ctx
, EVP_md5());
1120 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1121 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1122 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1123 EVP_MD_CTX_free(ctx
);
1124 #elif CRYPTO_INTERNAL
1125 memset(&ctx
, 0, sizeof(ctx
));
1127 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1128 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1129 MD5Final(digest
, &ctx
);
1132 /* Copy the digest to the packet. */
1133 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1136 /* RIP routing information. */
1137 static void rip_response_process(struct rip_packet
*packet
, int size
,
1138 struct sockaddr_in
*from
,
1139 struct connected
*ifc
)
1141 struct rip_interface
*ri
= ifc
->ifp
->info
;
1142 struct rip
*rip
= ri
->rip
;
1145 struct prefix_ipv4 ifaddr
;
1146 struct prefix_ipv4 ifaddrclass
;
1149 memset(&ifaddr
, 0, sizeof(ifaddr
));
1150 /* We don't know yet. */
1153 /* The Response must be ignored if it is not from the RIP
1154 port. (RFC2453 - Sec. 3.9.2)*/
1155 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1156 zlog_info("response doesn't come from RIP port: %d",
1158 rip_peer_bad_packet(rip
, from
);
1162 /* The datagram's IPv4 source address should be checked to see
1163 whether the datagram is from a valid neighbor; the source of the
1164 datagram must be on a directly connected network (RFC2453 - Sec.
1166 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1170 "This datagram doesn't came from a valid neighbor: %s",
1171 inet_ntoa(from
->sin_addr
));
1172 rip_peer_bad_packet(rip
, from
);
1176 /* It is also worth checking to see whether the response is from one
1177 of the router's own addresses. */
1179 ; /* Alredy done in rip_read () */
1181 /* Update RIP peer. */
1182 rip_peer_update(rip
, from
, packet
->version
);
1184 /* Set RTE pointer. */
1187 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1188 /* RIPv2 authentication check. */
1189 /* If the Address Family Identifier of the first (and only the
1190 first) entry in the message is 0xFFFF, then the remainder of
1191 the entry contains the authentication. */
1192 /* If the packet gets here it means authentication enabled */
1193 /* Check is done in rip_read(). So, just skipping it */
1194 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1195 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1198 if (rte
->family
!= htons(AF_INET
)) {
1199 /* Address family check. RIP only supports AF_INET. */
1200 zlog_info("Unsupported family %d from %s.",
1202 inet_ntoa(from
->sin_addr
));
1206 /* - is the destination address valid (e.g., unicast; not net 0
1208 if (!rip_destination_check(rte
->prefix
)) {
1210 "Network is net 0 or net 127 or it is not unicast network");
1211 rip_peer_bad_route(rip
, from
);
1215 /* Convert metric value to host byte order. */
1216 rte
->metric
= ntohl(rte
->metric
);
1218 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1219 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1220 zlog_info("Route's metric is not in the 1-16 range.");
1221 rip_peer_bad_route(rip
, from
);
1225 /* RIPv1 does not have nexthop value. */
1226 if (packet
->version
== RIPv1
&& rte
->nexthop
.s_addr
!= 0) {
1227 zlog_info("RIPv1 packet with nexthop value %s",
1228 inet_ntoa(rte
->nexthop
));
1229 rip_peer_bad_route(rip
, from
);
1233 /* That is, if the provided information is ignored, a possibly
1234 sub-optimal, but absolutely valid, route may be taken. If
1235 the received Next Hop is not directly reachable, it should be
1236 treated as 0.0.0.0. */
1237 if (packet
->version
== RIPv2
&& rte
->nexthop
.s_addr
!= 0) {
1240 /* Multicast address check. */
1241 addrval
= ntohl(rte
->nexthop
.s_addr
);
1242 if (IN_CLASSD(addrval
)) {
1244 "Nexthop %s is multicast address, skip this rte",
1245 inet_ntoa(rte
->nexthop
));
1249 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1250 rip
->vrf
->vrf_id
)) {
1251 struct route_node
*rn
;
1252 struct rip_info
*rinfo
;
1254 rn
= route_node_match_ipv4(rip
->table
,
1260 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1263 if (IS_RIP_DEBUG_EVENT
)
1265 "Next hop %s is on RIP network. Set nexthop to the packet's originator",
1268 rte
->nexthop
= rinfo
->from
;
1270 if (IS_RIP_DEBUG_EVENT
)
1272 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1275 rte
->nexthop
.s_addr
= 0;
1278 route_unlock_node(rn
);
1280 if (IS_RIP_DEBUG_EVENT
)
1282 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1285 rte
->nexthop
.s_addr
= 0;
1290 /* For RIPv1, there won't be a valid netmask.
1292 This is a best guess at the masks. If everyone was using old
1293 Ciscos before the 'ip subnet zero' option, it would be almost
1296 Cisco summarize ripv1 advertisements to the classful boundary
1297 (/16 for class B's) except when the RIP packet does to inside
1298 the classful network in question. */
1300 if ((packet
->version
== RIPv1
&& rte
->prefix
.s_addr
!= 0)
1301 || (packet
->version
== RIPv2
1302 && (rte
->prefix
.s_addr
!= 0
1303 && rte
->mask
.s_addr
== 0))) {
1304 uint32_t destination
;
1306 if (subnetted
== -1) {
1307 memcpy(&ifaddr
, ifc
->address
,
1308 sizeof(struct prefix_ipv4
));
1309 memcpy(&ifaddrclass
, &ifaddr
,
1310 sizeof(struct prefix_ipv4
));
1311 apply_classful_mask_ipv4(&ifaddrclass
);
1313 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1317 destination
= ntohl(rte
->prefix
.s_addr
);
1319 if (IN_CLASSA(destination
))
1320 masklen2ip(8, &rte
->mask
);
1321 else if (IN_CLASSB(destination
))
1322 masklen2ip(16, &rte
->mask
);
1323 else if (IN_CLASSC(destination
))
1324 masklen2ip(24, &rte
->mask
);
1327 masklen2ip(ifaddrclass
.prefixlen
,
1328 (struct in_addr
*)&destination
);
1329 if ((subnetted
== 1)
1330 && ((rte
->prefix
.s_addr
& destination
)
1331 == ifaddrclass
.prefix
.s_addr
)) {
1332 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1333 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1334 != rte
->prefix
.s_addr
)
1335 masklen2ip(32, &rte
->mask
);
1336 if (IS_RIP_DEBUG_EVENT
)
1337 zlog_debug("Subnetted route %s",
1338 inet_ntoa(rte
->prefix
));
1340 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1341 != rte
->prefix
.s_addr
)
1345 if (IS_RIP_DEBUG_EVENT
) {
1346 zlog_debug("Resultant route %s",
1347 inet_ntoa(rte
->prefix
));
1348 zlog_debug("Resultant mask %s",
1349 inet_ntoa(rte
->mask
));
1353 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1354 ignore the entry. */
1355 if ((packet
->version
== RIPv2
) && (rte
->mask
.s_addr
!= 0)
1356 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1357 != rte
->prefix
.s_addr
)) {
1359 "RIPv2 address %s is not mask /%d applied one",
1360 inet_ntoa(rte
->prefix
), ip_masklen(rte
->mask
));
1361 rip_peer_bad_route(rip
, from
);
1365 /* Default route's netmask is ignored. */
1366 if (packet
->version
== RIPv2
&& (rte
->prefix
.s_addr
== 0)
1367 && (rte
->mask
.s_addr
!= 0)) {
1368 if (IS_RIP_DEBUG_EVENT
)
1370 "Default route with non-zero netmask. Set zero to netmask");
1371 rte
->mask
.s_addr
= 0;
1374 /* Routing table updates. */
1375 rip_rte_process(rte
, from
, ifc
->ifp
);
1379 /* Make socket for RIP protocol. */
1380 int rip_create_socket(struct vrf
*vrf
)
1384 struct sockaddr_in addr
;
1385 const char *vrf_dev
= NULL
;
1387 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1388 addr
.sin_family
= AF_INET
;
1389 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1390 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1391 addr
.sin_len
= sizeof(struct sockaddr_in
);
1392 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1393 /* sending port must always be the RIP port */
1394 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1396 /* Make datagram socket. */
1397 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1398 vrf_dev
= vrf
->name
;
1399 frr_with_privs(&ripd_privs
) {
1400 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1403 flog_err_sys(EC_LIB_SOCKET
,
1404 "Cannot create UDP socket: %s",
1405 safe_strerror(errno
));
1410 sockopt_broadcast(sock
);
1411 sockopt_reuseaddr(sock
);
1412 sockopt_reuseport(sock
);
1413 setsockopt_ipv4_multicast_loop(sock
, 0);
1414 #ifdef IPTOS_PREC_INTERNETCONTROL
1415 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1417 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1419 frr_with_privs(&ripd_privs
) {
1420 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1422 zlog_err("%s: Can't bind socket %d to %s port %d: %s",
1423 __func__
, sock
, inet_ntoa(addr
.sin_addr
),
1424 (int)ntohs(addr
.sin_port
),
1425 safe_strerror(errno
));
1435 /* RIP packet send to destination address, on interface denoted by
1436 * by connected argument. NULL to argument denotes destination should be
1437 * should be RIP multicast group
1439 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1440 struct connected
*ifc
)
1442 struct rip_interface
*ri
;
1445 struct sockaddr_in sin
;
1449 struct cmsghdr
*cmsgptr
;
1450 char adata
[256] = {};
1451 struct in_pktinfo
*pkt
;
1452 #endif /* GNU_LINUX */
1454 assert(ifc
!= NULL
);
1455 ri
= ifc
->ifp
->info
;
1458 if (IS_RIP_DEBUG_PACKET
) {
1459 #define ADDRESS_SIZE 20
1460 char dst
[ADDRESS_SIZE
];
1463 strlcpy(dst
, inet_ntoa(to
->sin_addr
), sizeof(dst
));
1465 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1466 strlcpy(dst
, inet_ntoa(sin
.sin_addr
), sizeof(dst
));
1469 zlog_debug("rip_send_packet %s > %s (%s)",
1470 inet_ntoa(ifc
->address
->u
.prefix4
), dst
,
1474 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1476 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1478 * with multiple addresses on the same subnet: the first address
1479 * on the subnet is configured "primary", and all subsequent
1481 * on that subnet are treated as "secondary" addresses.
1482 * In order to avoid routing-table bloat on other rip listeners,
1483 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1485 * XXX Since Linux is the only system for which the
1486 * ZEBRA_IFA_SECONDARY
1487 * flag is set, we would end up sending a packet for a
1489 * source address on non-linux systems.
1491 if (IS_RIP_DEBUG_PACKET
)
1492 zlog_debug("duplicate dropped");
1496 /* Make destination address. */
1497 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1498 sin
.sin_family
= AF_INET
;
1499 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1500 sin
.sin_len
= sizeof(struct sockaddr_in
);
1501 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1503 /* When destination is specified, use it's port and address. */
1505 sin
.sin_port
= to
->sin_port
;
1506 sin
.sin_addr
= to
->sin_addr
;
1508 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1509 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1511 rip_interface_multicast_set(rip
->sock
, ifc
);
1514 memset(&msg
, 0, sizeof(msg
));
1515 msg
.msg_name
= (void *)&sin
;
1516 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1523 msg
.msg_control
= (void *)adata
;
1524 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1526 cmsgptr
= (struct cmsghdr
*)adata
;
1527 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1528 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1529 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1530 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1531 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1532 #endif /* GNU_LINUX */
1534 ret
= sendmsg(rip
->sock
, &msg
, 0);
1536 if (IS_RIP_DEBUG_EVENT
)
1537 zlog_debug("SEND to %s.%d", inet_ntoa(sin
.sin_addr
),
1538 ntohs(sin
.sin_port
));
1541 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1546 /* Add redistributed route to RIP table. */
1547 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1548 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1549 unsigned int metric
, unsigned char distance
,
1553 struct route_node
*rp
= NULL
;
1554 struct rip_info
*rinfo
= NULL
, newinfo
;
1555 struct list
*list
= NULL
;
1557 /* Redistribute route */
1558 ret
= rip_destination_check(p
->prefix
);
1562 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1564 memset(&newinfo
, 0, sizeof(struct rip_info
));
1565 newinfo
.type
= type
;
1566 newinfo
.sub_type
= sub_type
;
1568 newinfo
.external_metric
= metric
;
1569 newinfo
.distance
= distance
;
1570 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1575 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1576 rinfo
= listgetdata(listhead(list
));
1578 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1579 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1580 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1581 route_unlock_node(rp
);
1585 /* Manually configured RIP route check. */
1586 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1587 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1588 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1589 if (type
!= ZEBRA_ROUTE_RIP
1590 || ((sub_type
!= RIP_ROUTE_STATIC
)
1591 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1592 route_unlock_node(rp
);
1597 (void)rip_ecmp_replace(rip
, &newinfo
);
1598 route_unlock_node(rp
);
1600 (void)rip_ecmp_add(rip
, &newinfo
);
1602 if (IS_RIP_DEBUG_EVENT
) {
1603 zlog_debug("Redistribute new prefix %s/%d",
1604 inet_ntoa(p
->prefix
), p
->prefixlen
);
1607 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1610 /* Delete redistributed route from RIP table. */
1611 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1612 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1615 struct route_node
*rp
;
1616 struct rip_info
*rinfo
;
1618 ret
= rip_destination_check(p
->prefix
);
1622 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1624 struct list
*list
= rp
->info
;
1626 if (list
!= NULL
&& listcount(list
) != 0) {
1627 rinfo
= listgetdata(listhead(list
));
1628 if (rinfo
!= NULL
&& rinfo
->type
== type
1629 && rinfo
->sub_type
== sub_type
1630 && rinfo
->nh
.ifindex
== ifindex
) {
1631 /* Perform poisoned reverse. */
1632 rinfo
->metric
= RIP_METRIC_INFINITY
;
1633 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1634 rip_garbage_collect
,
1636 RIP_TIMER_OFF(rinfo
->t_timeout
);
1637 rinfo
->flags
|= RIP_RTF_CHANGED
;
1639 if (IS_RIP_DEBUG_EVENT
)
1641 "Poison %s/%d on the interface %s with an "
1642 "infinity metric [delete]",
1643 inet_ntoa(p
->prefix
),
1649 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1652 route_unlock_node(rp
);
1656 /* Response to request called from rip_read ().*/
1657 static void rip_request_process(struct rip_packet
*packet
, int size
,
1658 struct sockaddr_in
*from
, struct connected
*ifc
)
1663 struct prefix_ipv4 p
;
1664 struct route_node
*rp
;
1665 struct rip_info
*rinfo
;
1666 struct rip_interface
*ri
;
1668 /* Does not reponse to the requests on the loopback interfaces */
1669 if (if_is_loopback(ifc
->ifp
))
1672 /* Check RIP process is enabled on this interface. */
1673 ri
= ifc
->ifp
->info
;
1678 /* When passive interface is specified, suppress responses */
1682 /* RIP peer update. */
1683 rip_peer_update(rip
, from
, packet
->version
);
1685 lim
= ((caddr_t
)packet
) + size
;
1688 /* The Request is processed entry by entry. If there are no
1689 entries, no response is given. */
1690 if (lim
== (caddr_t
)rte
)
1693 /* There is one special case. If there is exactly one entry in the
1694 request, and it has an address family identifier of zero and a
1695 metric of infinity (i.e., 16), then this is a request to send the
1696 entire routing table. */
1697 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1698 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1699 /* All route with split horizon */
1700 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1702 if (ntohs(rte
->family
) != AF_INET
)
1705 /* Examine the list of RTEs in the Request one by one. For each
1706 entry, look up the destination in the router's routing
1707 database and, if there is a route, put that route's metric in
1708 the metric field of the RTE. If there is no explicit route
1709 to the specified destination, put infinity in the metric
1710 field. Once all the entries have been filled in, change the
1711 command from Request to Response and send the datagram back
1712 to the requestor. */
1715 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1716 p
.prefix
= rte
->prefix
;
1717 p
.prefixlen
= ip_masklen(rte
->mask
);
1718 apply_mask_ipv4(&p
);
1720 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1722 rinfo
= listgetdata(
1723 listhead((struct list
*)rp
->info
));
1724 rte
->metric
= htonl(rinfo
->metric
);
1725 route_unlock_node(rp
);
1727 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1729 packet
->command
= RIP_RESPONSE
;
1731 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1733 rip
->counters
.queries
++;
1736 /* First entry point of RIP packet. */
1737 static int rip_read(struct thread
*t
)
1739 struct rip
*rip
= THREAD_ARG(t
);
1743 union rip_buf rip_buf
;
1744 struct rip_packet
*packet
;
1745 struct sockaddr_in from
;
1749 struct interface
*ifp
= NULL
;
1750 struct connected
*ifc
;
1751 struct rip_interface
*ri
;
1754 /* Fetch socket then register myself. */
1755 sock
= THREAD_FD(t
);
1758 /* Add myself to tne next event */
1759 rip_event(rip
, RIP_READ
, sock
);
1761 /* RIPd manages only IPv4. */
1762 memset(&from
, 0, sizeof(struct sockaddr_in
));
1763 fromlen
= sizeof(struct sockaddr_in
);
1765 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1766 (struct sockaddr
*)&from
, &fromlen
);
1768 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1769 safe_strerror(errno
));
1773 /* Check is this packet comming from myself? */
1774 if (if_check_address(rip
, from
.sin_addr
)) {
1775 if (IS_RIP_DEBUG_PACKET
)
1776 zlog_debug("ignore packet comes from myself (VRF %s)",
1781 /* Which interface is this packet comes from. */
1782 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1787 /* RIP packet received */
1788 if (IS_RIP_DEBUG_EVENT
)
1789 zlog_debug("RECV packet from %s port %d on %s (VRF %s)",
1790 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1791 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1793 /* If this packet come from unknown interface, ignore it. */
1796 "rip_read: cannot find interface for packet from %s port %d (VRF %s)",
1797 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1803 p
.u
.prefix4
= from
.sin_addr
;
1804 p
.prefixlen
= IPV4_MAX_BITLEN
;
1806 ifc
= connected_lookup_prefix(ifp
, &p
);
1810 "rip_read: cannot find connected address for packet from %s "
1811 "port %d on interface %s (VRF %s)",
1812 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1813 ifp
->name
, rip
->vrf_name
);
1817 /* Packet length check. */
1818 if (len
< RIP_PACKET_MINSIZ
) {
1819 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1821 rip_peer_bad_packet(rip
, &from
);
1824 if (len
> RIP_PACKET_MAXSIZ
) {
1825 zlog_warn("packet size %d is larger than max size %d", len
,
1827 rip_peer_bad_packet(rip
, &from
);
1831 /* Packet alignment check. */
1832 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1833 zlog_warn("packet size %d is wrong for RIP packet alignment",
1835 rip_peer_bad_packet(rip
, &from
);
1839 /* Set RTE number. */
1840 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1842 /* For easy to handle. */
1843 packet
= &rip_buf
.rip_packet
;
1845 /* RIP version check. */
1846 if (packet
->version
== 0) {
1847 zlog_info("version 0 with command %d received.",
1849 rip_peer_bad_packet(rip
, &from
);
1853 /* Dump RIP packet. */
1854 if (IS_RIP_DEBUG_RECV
)
1855 rip_packet_dump(packet
, len
, "RECV");
1857 /* RIP version adjust. This code should rethink now. RFC1058 says
1858 that "Version 1 implementations are to ignore this extra data and
1859 process only the fields specified in this document.". So RIPv3
1860 packet should be treated as RIPv1 ignoring must be zero field. */
1861 if (packet
->version
> RIPv2
)
1862 packet
->version
= RIPv2
;
1864 /* Is RIP running or is this RIP neighbor ?*/
1866 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1867 if (IS_RIP_DEBUG_EVENT
)
1868 zlog_debug("RIP is not enabled on interface %s.",
1870 rip_peer_bad_packet(rip
, &from
);
1874 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1875 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1877 if (vrecv
== RI_RIP_VERSION_NONE
1878 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1879 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1880 if (IS_RIP_DEBUG_PACKET
)
1882 " packet's v%d doesn't fit to if version spec",
1884 rip_peer_bad_packet(rip
, &from
);
1888 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1889 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1890 accepted; authenticated RIP-2 messages shall be discarded. */
1891 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1892 && (packet
->version
== RIPv2
)
1893 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1894 if (IS_RIP_DEBUG_EVENT
)
1896 "packet RIPv%d is dropped because authentication disabled",
1898 ripd_notif_send_auth_type_failure(ifp
->name
);
1899 rip_peer_bad_packet(rip
, &from
);
1904 If the router is configured to authenticate RIP-2 messages, then
1905 RIP-1 messages and RIP-2 messages which pass authentication
1906 testing shall be accepted; unauthenticated and failed
1907 authentication RIP-2 messages shall be discarded. For maximum
1908 security, RIP-1 messages should be ignored when authentication is
1909 in use (see section 4.1); otherwise, the routing information from
1910 authenticated messages will be propagated by RIP-1 routers in an
1911 unauthenticated manner.
1913 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1914 * always reply regardless of authentication settings, because:
1916 * - if there other authorised routers on-link, the REQUESTor can
1917 * passively obtain the routing updates anyway
1918 * - if there are no other authorised routers on-link, RIP can
1919 * easily be disabled for the link to prevent giving out information
1920 * on state of this routers RIP routing table..
1922 * I.e. if RIPv1 has any place anymore these days, it's as a very
1923 * simple way to distribute routing information (e.g. to embedded
1924 * hosts / appliances) and the ability to give out RIPv1
1925 * routing-information freely, while still requiring RIPv2
1926 * authentication for any RESPONSEs might be vaguely useful.
1928 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1929 /* Discard RIPv1 messages other than REQUESTs */
1930 if (packet
->command
!= RIP_REQUEST
) {
1931 if (IS_RIP_DEBUG_PACKET
)
1934 " dropped because authentication enabled");
1935 ripd_notif_send_auth_type_failure(ifp
->name
);
1936 rip_peer_bad_packet(rip
, &from
);
1939 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1940 const char *auth_desc
;
1943 /* There definitely is no authentication in the packet.
1945 if (IS_RIP_DEBUG_PACKET
)
1947 "RIPv2 authentication failed: no auth RTE in packet");
1948 ripd_notif_send_auth_type_failure(ifp
->name
);
1949 rip_peer_bad_packet(rip
, &from
);
1953 /* First RTE must be an Authentication Family RTE */
1954 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1955 if (IS_RIP_DEBUG_PACKET
)
1958 " dropped because authentication enabled");
1959 ripd_notif_send_auth_type_failure(ifp
->name
);
1960 rip_peer_bad_packet(rip
, &from
);
1964 /* Check RIPv2 authentication. */
1965 switch (ntohs(packet
->rte
->tag
)) {
1966 case RIP_AUTH_SIMPLE_PASSWORD
:
1967 auth_desc
= "simple";
1968 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1973 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1974 /* Reset RIP packet length to trim MD5 data. */
1980 auth_desc
= "unknown type";
1981 if (IS_RIP_DEBUG_PACKET
)
1983 "RIPv2 Unknown authentication type %d",
1984 ntohs(packet
->rte
->tag
));
1988 if (IS_RIP_DEBUG_PACKET
)
1989 zlog_debug("RIPv2 %s authentication success",
1992 if (IS_RIP_DEBUG_PACKET
)
1993 zlog_debug("RIPv2 %s authentication failure",
1995 ripd_notif_send_auth_failure(ifp
->name
);
1996 rip_peer_bad_packet(rip
, &from
);
2001 /* Process each command. */
2002 switch (packet
->command
) {
2004 rip_response_process(packet
, len
, &from
, ifc
);
2008 rip_request_process(packet
, len
, &from
, ifc
);
2013 "Obsolete command %s received, please sent it to routed",
2014 lookup_msg(rip_msg
, packet
->command
, NULL
));
2015 rip_peer_bad_packet(rip
, &from
);
2017 case RIP_POLL_ENTRY
:
2018 zlog_info("Obsolete command %s received",
2019 lookup_msg(rip_msg
, packet
->command
, NULL
));
2020 rip_peer_bad_packet(rip
, &from
);
2023 zlog_info("Unknown RIP command %d received", packet
->command
);
2024 rip_peer_bad_packet(rip
, &from
);
2031 /* Write routing table entry to the stream and return next index of
2032 the routing table entry in the stream. */
2033 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2034 uint8_t version
, struct rip_info
*rinfo
)
2036 struct in_addr mask
;
2038 /* Write routing table entry. */
2039 if (version
== RIPv1
) {
2040 stream_putw(s
, AF_INET
);
2042 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2043 stream_put_ipv4(s
, 0);
2044 stream_put_ipv4(s
, 0);
2045 stream_putl(s
, rinfo
->metric_out
);
2047 masklen2ip(p
->prefixlen
, &mask
);
2049 stream_putw(s
, AF_INET
);
2050 stream_putw(s
, rinfo
->tag_out
);
2051 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2052 stream_put_ipv4(s
, mask
.s_addr
);
2053 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2054 stream_putl(s
, rinfo
->metric_out
);
2060 /* Send update to the ifp or spcified neighbor. */
2061 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2062 int route_type
, uint8_t version
)
2067 struct route_node
*rp
;
2068 struct rip_info
*rinfo
;
2069 struct rip_interface
*ri
;
2070 struct prefix_ipv4
*p
;
2071 struct prefix_ipv4 classfull
;
2072 struct prefix_ipv4 ifaddrclass
;
2073 struct key
*key
= NULL
;
2074 /* this might need to made dynamic if RIP ever supported auth methods
2075 with larger key string sizes */
2076 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2077 size_t doff
= 0; /* offset of digest offset field */
2081 struct list
*list
= NULL
;
2082 struct listnode
*listnode
= NULL
;
2084 /* Logging output event. */
2085 if (IS_RIP_DEBUG_EVENT
) {
2087 zlog_debug("update routes to neighbor %s",
2088 inet_ntoa(to
->sin_addr
));
2090 zlog_debug("update routes on interface %s ifindex %d",
2091 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2094 /* Get RIP interface. */
2095 ri
= ifc
->ifp
->info
;
2098 /* Set output stream. */
2101 /* Reset stream and RTE counter. */
2103 rtemax
= RIP_MAX_RTE
;
2105 /* If output interface is in simple password authentication mode, we
2106 need space for authentication data. */
2107 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2110 /* If output interface is in MD5 authentication mode, we need space
2111 for authentication header and data. */
2112 if (ri
->auth_type
== RIP_AUTH_MD5
)
2115 /* If output interface is in simple password authentication mode
2116 and string or keychain is specified we need space for auth. data */
2117 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2118 if (ri
->key_chain
) {
2119 struct keychain
*keychain
;
2121 keychain
= keychain_lookup(ri
->key_chain
);
2123 key
= key_lookup_for_send(keychain
);
2125 /* to be passed to auth functions later */
2126 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2127 if (strlen(auth_str
) == 0)
2131 if (version
== RIPv1
) {
2132 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2133 apply_classful_mask_ipv4(&ifaddrclass
);
2135 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2139 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2140 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2141 rinfo
= listgetdata(listhead(list
));
2142 /* For RIPv1, if we are subnetted, output subnets in our
2144 /* that have the same mask as the output "interface".
2146 /* networks, only the classfull version is output. */
2148 if (version
== RIPv1
) {
2149 p
= (struct prefix_ipv4
*)&rp
->p
;
2151 if (IS_RIP_DEBUG_PACKET
)
2153 "RIPv1 mask check, %s/%d considered for output",
2154 inet_ntoa(rp
->p
.u
.prefix4
),
2159 (struct prefix
*)&ifaddrclass
,
2161 if ((ifc
->address
->prefixlen
2163 && (rp
->p
.prefixlen
!= 32))
2166 memcpy(&classfull
, &rp
->p
,
2167 sizeof(struct prefix_ipv4
));
2168 apply_classful_mask_ipv4(&classfull
);
2169 if (rp
->p
.u
.prefix4
.s_addr
!= 0
2170 && classfull
.prefixlen
2174 if (IS_RIP_DEBUG_PACKET
)
2176 "RIPv1 mask check, %s/%d made it through",
2177 inet_ntoa(rp
->p
.u
.prefix4
),
2180 p
= (struct prefix_ipv4
*)&rp
->p
;
2182 /* Apply output filters. */
2183 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2187 /* Changed route only output. */
2188 if (route_type
== rip_changed_route
2189 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2192 /* Split horizon. */
2193 /* if (split_horizon == rip_split_horizon) */
2194 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2196 * We perform split horizon for RIP and
2198 * For rip routes, we want to suppress the route
2200 * end up sending the route back on the
2202 * learned it from, with a higher metric. For
2204 * we suppress the route if the prefix is a
2206 * source address that we are going to use for
2208 * (in order to handle the case when multiple
2210 * configured on the same interface).
2213 struct rip_info
*tmp_rinfo
= NULL
;
2214 struct connected
*tmp_ifc
= NULL
;
2216 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2218 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2219 && tmp_rinfo
->nh
.ifindex
2220 == ifc
->ifp
->ifindex
) {
2226 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2227 for (ALL_LIST_ELEMENTS_RO(
2228 ifc
->ifp
->connected
,
2232 tmp_ifc
->address
)) {
2242 /* Preparation for route-map. */
2243 rinfo
->metric_set
= 0;
2244 rinfo
->nexthop_out
.s_addr
= 0;
2245 rinfo
->metric_out
= rinfo
->metric
;
2246 rinfo
->tag_out
= rinfo
->tag
;
2247 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2249 /* In order to avoid some local loops,
2250 * if the RIP route has a nexthop via this interface,
2252 * otherwise set it to 0. The nexthop should not be
2254 * beyond the local broadcast/multicast area in order
2255 * to avoid an IGP multi-level recursive look-up.
2258 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2259 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2261 /* Interface route-map */
2262 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2263 ret
= route_map_apply(
2264 ri
->routemap
[RIP_FILTER_OUT
],
2265 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2267 if (ret
== RMAP_DENYMATCH
) {
2268 if (IS_RIP_DEBUG_PACKET
)
2270 "RIP %s/%d is filtered by route-map out",
2271 inet_ntoa(p
->prefix
),
2277 /* Apply redistribute route map - continue, if deny */
2278 if (rip
->redist
[rinfo
->type
].route_map
.name
2279 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2280 ret
= route_map_apply(
2281 rip
->redist
[rinfo
->type
].route_map
.map
,
2282 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2284 if (ret
== RMAP_DENYMATCH
) {
2285 if (IS_RIP_DEBUG_PACKET
)
2287 "%s/%d is filtered by route-map",
2288 inet_ntoa(p
->prefix
),
2294 /* When route-map does not set metric. */
2295 if (!rinfo
->metric_set
) {
2296 /* If redistribute metric is set. */
2297 if (rip
->redist
[rinfo
->type
].metric_config
2298 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2300 rip
->redist
[rinfo
->type
].metric
;
2302 /* If the route is not connected or
2304 one, use default-metric value*/
2305 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2307 != ZEBRA_ROUTE_CONNECT
2309 != RIP_METRIC_INFINITY
)
2311 rip
->default_metric
;
2315 /* Apply offset-list */
2316 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2317 rip_offset_list_apply_out(p
, ifc
->ifp
,
2318 &rinfo
->metric_out
);
2320 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2321 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2323 /* Perform split-horizon with poisoned reverse
2324 * for RIP and connected routes.
2326 if (ri
->split_horizon
2327 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2329 * We perform split horizon for RIP and
2331 * For rip routes, we want to suppress the route
2333 * end up sending the route back on the
2335 * learned it from, with a higher metric. For
2337 * we suppress the route if the prefix is a
2339 * source address that we are going to use for
2341 * (in order to handle the case when multiple
2343 * configured on the same interface).
2345 struct rip_info
*tmp_rinfo
= NULL
;
2346 struct connected
*tmp_ifc
= NULL
;
2348 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2350 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2351 && tmp_rinfo
->nh
.ifindex
2352 == ifc
->ifp
->ifindex
)
2354 RIP_METRIC_INFINITY
;
2356 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2357 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2358 for (ALL_LIST_ELEMENTS_RO(
2359 ifc
->ifp
->connected
,
2363 tmp_ifc
->address
)) {
2365 RIP_METRIC_INFINITY
;
2371 /* Prepare preamble, auth headers, if needs be */
2373 stream_putc(s
, RIP_RESPONSE
);
2374 stream_putc(s
, version
);
2377 /* auth header for !v1 && !no_auth */
2378 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2379 && (version
!= RIPv1
))
2380 doff
= rip_auth_header_write(
2381 s
, ri
, key
, auth_str
,
2382 RIP_AUTH_SIMPLE_SIZE
);
2385 /* Write RTE to the stream. */
2386 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2387 if (num
== rtemax
) {
2388 if (version
== RIPv2
2389 && ri
->auth_type
== RIP_AUTH_MD5
)
2390 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2391 RIP_AUTH_SIMPLE_SIZE
);
2393 ret
= rip_send_packet(STREAM_DATA(s
),
2394 stream_get_endp(s
), to
,
2397 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2398 rip_packet_dump((struct rip_packet
*)
2407 /* Flush unwritten RTE. */
2409 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2410 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2411 RIP_AUTH_SIMPLE_SIZE
);
2413 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2416 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2417 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2418 stream_get_endp(s
), "SEND");
2422 /* Statistics updates. */
2426 /* Send RIP packet to the interface. */
2427 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2430 struct interface
*ifp
= ifc
->ifp
;
2431 struct rip_interface
*ri
= ifp
->info
;
2432 struct sockaddr_in to
;
2434 /* When RIP version is 2 and multicast enable interface. */
2435 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2436 if (IS_RIP_DEBUG_EVENT
)
2437 zlog_debug("multicast announce on %s ", ifp
->name
);
2439 rip_output_process(ifc
, NULL
, route_type
, version
);
2443 /* If we can't send multicast packet, send it with unicast. */
2444 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2445 if (ifc
->address
->family
== AF_INET
) {
2446 /* Destination address and port setting. */
2447 memset(&to
, 0, sizeof(struct sockaddr_in
));
2448 if (ifc
->destination
)
2449 /* use specified broadcast or peer destination
2451 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2452 else if (ifc
->address
->prefixlen
< IPV4_MAX_PREFIXLEN
)
2453 /* calculate the appropriate broadcast address
2455 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2456 ifc
->address
->u
.prefix4
.s_addr
,
2457 ifc
->address
->prefixlen
);
2459 /* do not know where to send the packet */
2461 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2463 if (IS_RIP_DEBUG_EVENT
)
2464 zlog_debug("%s announce to %s on %s",
2465 CONNECTED_PEER(ifc
) ? "unicast"
2467 inet_ntoa(to
.sin_addr
), ifp
->name
);
2469 rip_output_process(ifc
, &to
, route_type
, version
);
2474 /* Update send to all interface and neighbor. */
2475 static void rip_update_process(struct rip
*rip
, int route_type
)
2477 struct listnode
*ifnode
, *ifnnode
;
2478 struct connected
*connected
;
2479 struct interface
*ifp
;
2480 struct rip_interface
*ri
;
2481 struct route_node
*rp
;
2482 struct sockaddr_in to
;
2485 /* Send RIP update to each interface. */
2486 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2487 if (if_is_loopback(ifp
))
2490 if (!if_is_operative(ifp
))
2493 /* Fetch RIP interface information. */
2496 /* When passive interface is specified, suppress announce to the
2503 * If there is no version configuration in the
2505 * use rip's version setting.
2507 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2511 if (IS_RIP_DEBUG_EVENT
)
2512 zlog_debug("SEND UPDATE to %s ifindex %d",
2513 ifp
->name
, ifp
->ifindex
);
2515 /* send update on each connected network */
2516 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2518 if (connected
->address
->family
== AF_INET
) {
2520 rip_update_interface(
2524 && if_is_multicast(ifp
))
2525 rip_update_interface(
2533 /* RIP send updates to each neighbor. */
2534 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2535 if (rp
->info
!= NULL
) {
2538 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2542 "Neighbor %s doesn't have connected interface!",
2543 inet_ntoa(p
->u
.prefix4
));
2547 /* Set destination address and port */
2548 memset(&to
, 0, sizeof(struct sockaddr_in
));
2549 to
.sin_addr
= p
->u
.prefix4
;
2550 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2552 /* RIP version is rip's configuration. */
2553 rip_output_process(connected
, &to
, route_type
,
2558 /* RIP's periodical timer. */
2559 static int rip_update(struct thread
*t
)
2561 struct rip
*rip
= THREAD_ARG(t
);
2563 /* Clear timer pointer. */
2564 rip
->t_update
= NULL
;
2566 if (IS_RIP_DEBUG_EVENT
)
2567 zlog_debug("update timer fire!");
2569 /* Process update output. */
2570 rip_update_process(rip
, rip_all_route
);
2572 /* Triggered updates may be suppressed if a regular update is due by
2573 the time the triggered update would be sent. */
2574 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2577 /* Register myself. */
2578 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2583 /* Walk down the RIP routing table then clear changed flag. */
2584 static void rip_clear_changed_flag(struct rip
*rip
)
2586 struct route_node
*rp
;
2587 struct rip_info
*rinfo
= NULL
;
2588 struct list
*list
= NULL
;
2589 struct listnode
*listnode
= NULL
;
2591 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2592 if ((list
= rp
->info
) != NULL
)
2593 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2594 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2595 /* This flag can be set only on the first entry.
2601 /* Triggered update interval timer. */
2602 static int rip_triggered_interval(struct thread
*t
)
2604 struct rip
*rip
= THREAD_ARG(t
);
2606 rip
->t_triggered_interval
= NULL
;
2610 rip_triggered_update(t
);
2615 /* Execute triggered update. */
2616 static int rip_triggered_update(struct thread
*t
)
2618 struct rip
*rip
= THREAD_ARG(t
);
2621 /* Clear thred pointer. */
2622 rip
->t_triggered_update
= NULL
;
2624 /* Cancel interval timer. */
2625 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2628 /* Logging triggered update. */
2629 if (IS_RIP_DEBUG_EVENT
)
2630 zlog_debug("triggered update!");
2632 /* Split Horizon processing is done when generating triggered
2633 updates as well as normal updates (see section 2.6). */
2634 rip_update_process(rip
, rip_changed_route
);
2636 /* Once all of the triggered updates have been generated, the route
2637 change flags should be cleared. */
2638 rip_clear_changed_flag(rip
);
2640 /* After a triggered update is sent, a timer should be set for a
2641 random interval between 1 and 5 seconds. If other changes that
2642 would trigger updates occur before the timer expires, a single
2643 update is triggered when the timer expires. */
2644 interval
= (random() % 5) + 1;
2646 rip
->t_triggered_interval
= NULL
;
2647 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2648 &rip
->t_triggered_interval
);
2653 /* Withdraw redistributed route. */
2654 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2656 struct route_node
*rp
;
2657 struct rip_info
*rinfo
= NULL
;
2658 struct list
*list
= NULL
;
2660 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2661 if ((list
= rp
->info
) != NULL
) {
2662 rinfo
= listgetdata(listhead(list
));
2663 if (rinfo
->type
== type
2664 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2665 /* Perform poisoned reverse. */
2666 rinfo
->metric
= RIP_METRIC_INFINITY
;
2667 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2668 rip_garbage_collect
,
2670 RIP_TIMER_OFF(rinfo
->t_timeout
);
2671 rinfo
->flags
|= RIP_RTF_CHANGED
;
2673 if (IS_RIP_DEBUG_EVENT
) {
2674 struct prefix_ipv4
*p
=
2675 (struct prefix_ipv4
*)&rp
->p
;
2678 "Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2679 inet_ntoa(p
->prefix
),
2686 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2691 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2695 vrf
= vrf_lookup_by_id(vrf_id
);
2702 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2706 rip
.vrf_name
= (char *)vrf_name
;
2708 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2711 /* Create new RIP instance and set it to global variable. */
2712 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2716 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2717 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2719 /* Set initial value. */
2720 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2721 rip
->default_metric
=
2722 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2724 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2725 rip
->passive_default
=
2726 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2727 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2729 rip
->timeout_time
= yang_get_default_uint32(
2730 "%s/timers/holddown-interval", RIP_INSTANCE
);
2731 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2734 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2736 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2738 /* Initialize RIP data structures. */
2739 rip
->table
= route_table_init();
2740 route_table_set_info(rip
->table
, rip
);
2741 rip
->neighbor
= route_table_init();
2742 rip
->peer_list
= list_new();
2743 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2744 rip
->peer_list
->del
= rip_peer_list_del
;
2745 rip
->distance_table
= route_table_init();
2746 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2747 rip
->enable_interface
= vector_init(1);
2748 rip
->enable_network
= route_table_init();
2749 rip
->passive_nondefault
= vector_init(1);
2750 rip
->offset_list_master
= list_new();
2751 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2752 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2754 /* Distribute list install. */
2755 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2756 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2757 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2759 /* if rmap install. */
2760 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2761 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2762 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2764 /* Make output stream. */
2765 rip
->obuf
= stream_new(1500);
2767 /* Enable the routing instance if possible. */
2768 if (vrf
&& vrf_is_enabled(vrf
))
2769 rip_instance_enable(rip
, vrf
, socket
);
2775 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2780 /* Sned RIP request to the destination. */
2781 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2782 uint8_t version
, struct connected
*connected
)
2785 struct rip_packet rip_packet
;
2786 struct listnode
*node
, *nnode
;
2788 memset(&rip_packet
, 0, sizeof(rip_packet
));
2790 rip_packet
.command
= RIP_REQUEST
;
2791 rip_packet
.version
= version
;
2792 rte
= rip_packet
.rte
;
2793 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2797 * connected is only sent for ripv1 case, or when
2798 * interface does not support multicast. Caller loops
2799 * over each connected address for this case.
2801 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2803 != sizeof(rip_packet
))
2806 return sizeof(rip_packet
);
2809 /* send request on each connected network */
2810 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2811 struct prefix_ipv4
*p
;
2813 p
= (struct prefix_ipv4
*)connected
->address
;
2815 if (p
->family
!= AF_INET
)
2818 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2820 != sizeof(rip_packet
))
2823 return sizeof(rip_packet
);
2826 static int rip_update_jitter(unsigned long time
)
2828 #define JITTER_BOUND 4
2829 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2830 Given that, we cannot let time be less than JITTER_BOUND seconds.
2831 The RIPv2 RFC says jitter should be small compared to
2832 update_time. We consider 1/JITTER_BOUND to be small.
2835 int jitter_input
= time
;
2838 if (jitter_input
< JITTER_BOUND
)
2839 jitter_input
= JITTER_BOUND
;
2841 jitter
= (((random() % ((jitter_input
* 2) + 1)) - jitter_input
));
2843 return jitter
/ JITTER_BOUND
;
2846 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2853 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2855 case RIP_UPDATE_EVENT
:
2856 RIP_TIMER_OFF(rip
->t_update
);
2857 jitter
= rip_update_jitter(rip
->update_time
);
2858 thread_add_timer(master
, rip_update
, rip
,
2859 sock
? 2 : rip
->update_time
+ jitter
,
2862 case RIP_TRIGGERED_UPDATE
:
2863 if (rip
->t_triggered_interval
)
2866 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2867 &rip
->t_triggered_update
);
2876 rip_update_default_metric (void)
2878 struct route_node
*np
;
2879 struct rip_info
*rinfo
= NULL
;
2880 struct list
*list
= NULL
;
2881 struct listnode
*listnode
= NULL
;
2883 for (np
= route_top (rip
->table
); np
; np
= route_next (np
))
2884 if ((list
= np
->info
) != NULL
)
2885 for (ALL_LIST_ELEMENTS_RO (list
, listnode
, rinfo
))
2886 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&& rinfo
->type
!= ZEBRA_ROUTE_CONNECT
)
2887 rinfo
->metric
= rip
->default_metric
;
2891 struct rip_distance
*rip_distance_new(void)
2893 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2896 void rip_distance_free(struct rip_distance
*rdistance
)
2898 if (rdistance
->access_list
)
2899 free(rdistance
->access_list
);
2900 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2903 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2904 struct route_node
*node
)
2906 struct rip_distance
*rdistance
;
2908 rdistance
= node
->info
;
2910 rip_distance_free(rdistance
);
2913 /* Apply RIP information to distance method. */
2914 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2916 struct route_node
*rn
;
2917 struct prefix_ipv4 p
;
2918 struct rip_distance
*rdistance
;
2919 struct access_list
*alist
;
2921 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2923 p
.prefix
= rinfo
->from
;
2924 p
.prefixlen
= IPV4_MAX_BITLEN
;
2926 /* Check source address. */
2927 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2929 rdistance
= rn
->info
;
2930 route_unlock_node(rn
);
2932 if (rdistance
->access_list
) {
2933 alist
= access_list_lookup(AFI_IP
,
2934 rdistance
->access_list
);
2937 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2941 return rdistance
->distance
;
2943 return rdistance
->distance
;
2947 return rip
->distance
;
2952 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2954 struct route_node
*rn
;
2955 struct rip_distance
*rdistance
;
2959 vty_out(vty
, " Distance: (default is %u)\n",
2960 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2962 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2963 if ((rdistance
= rn
->info
) != NULL
) {
2966 " Address Distance List\n");
2969 sprintf(buf
, "%s/%d", inet_ntoa(rn
->p
.u
.prefix4
),
2971 vty_out(vty
, " %-20s %4d %s\n", buf
,
2972 rdistance
->distance
,
2973 rdistance
->access_list
? rdistance
->access_list
2978 /* Update ECMP routes to zebra when ECMP is disabled. */
2979 void rip_ecmp_disable(struct rip
*rip
)
2981 struct route_node
*rp
;
2982 struct rip_info
*rinfo
, *tmp_rinfo
;
2984 struct listnode
*node
, *nextnode
;
2986 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2987 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2988 rinfo
= listgetdata(listhead(list
));
2989 if (!rip_route_rte(rinfo
))
2992 /* Drop all other entries, except the first one. */
2993 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
2994 if (tmp_rinfo
!= rinfo
) {
2995 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
2997 tmp_rinfo
->t_garbage_collect
);
2998 list_delete_node(list
, node
);
2999 rip_info_free(tmp_rinfo
);
3003 rip_zebra_ipv4_add(rip
, rp
);
3005 /* Set the route change flag. */
3006 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
3008 /* Signal the output process to trigger an update. */
3009 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
3013 /* Print out routes update time. */
3014 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
3019 char timebuf
[TIME_BUF
];
3020 struct thread
*thread
;
3022 if ((thread
= rinfo
->t_timeout
) != NULL
) {
3023 clock
= thread_timer_remain_second(thread
);
3024 tm
= gmtime(&clock
);
3025 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3026 vty_out(vty
, "%5s", timebuf
);
3027 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
3028 clock
= thread_timer_remain_second(thread
);
3029 tm
= gmtime(&clock
);
3030 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3031 vty_out(vty
, "%5s", timebuf
);
3035 static const char *rip_route_type_print(int sub_type
)
3040 case RIP_ROUTE_STATIC
:
3042 case RIP_ROUTE_DEFAULT
:
3044 case RIP_ROUTE_REDISTRIBUTE
:
3046 case RIP_ROUTE_INTERFACE
:
3055 "show ip rip [vrf NAME]",
3062 struct route_node
*np
;
3063 struct rip_info
*rinfo
= NULL
;
3064 struct list
*list
= NULL
;
3065 struct listnode
*listnode
= NULL
;
3066 const char *vrf_name
;
3069 if (argv_find(argv
, argc
, "vrf", &idx
))
3070 vrf_name
= argv
[idx
+ 1]->arg
;
3072 vrf_name
= VRF_DEFAULT_NAME
;
3074 rip
= rip_lookup_by_vrf_name(vrf_name
);
3076 vty_out(vty
, "%% RIP instance not found\n");
3079 if (!rip
->enabled
) {
3080 vty_out(vty
, "%% RIP instance is disabled\n");
3085 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3087 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3088 " (i) - interface\n\n"
3089 " Network Next Hop Metric From Tag Time\n");
3091 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3092 if ((list
= np
->info
) != NULL
)
3093 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3097 vty
, "%c(%s) %s/%d",
3098 /* np->lock, For debugging. */
3099 zebra_route_char(rinfo
->type
),
3100 rip_route_type_print(rinfo
->sub_type
),
3101 inet_ntoa(np
->p
.u
.prefix4
),
3107 vty_out(vty
, "%*s", len
, " ");
3109 switch (rinfo
->nh
.type
) {
3110 case NEXTHOP_TYPE_IPV4
:
3111 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3112 vty_out(vty
, "%-20s %2d ",
3113 inet_ntoa(rinfo
->nh
.gate
.ipv4
),
3116 case NEXTHOP_TYPE_IFINDEX
:
3121 case NEXTHOP_TYPE_BLACKHOLE
:
3126 case NEXTHOP_TYPE_IPV6
:
3127 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3129 "V6 Address Hidden %2d ",
3134 /* Route which exist in kernel routing table. */
3135 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3136 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3137 vty_out(vty
, "%-15s ",
3138 inet_ntoa(rinfo
->from
));
3139 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3140 (route_tag_t
)rinfo
->tag
);
3141 rip_vty_out_uptime(vty
, rinfo
);
3142 } else if (rinfo
->metric
3143 == RIP_METRIC_INFINITY
) {
3144 vty_out(vty
, "self ");
3145 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3146 (route_tag_t
)rinfo
->tag
);
3147 rip_vty_out_uptime(vty
, rinfo
);
3149 if (rinfo
->external_metric
) {
3151 vty
, "self (%s:%d)",
3154 rinfo
->external_metric
);
3157 vty_out(vty
, "%*s", len
,
3162 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3163 (route_tag_t
)rinfo
->tag
);
3171 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3172 DEFUN (show_ip_rip_status
,
3173 show_ip_rip_status_cmd
,
3174 "show ip rip [vrf NAME] status",
3179 "IP routing protocol process parameters and statistics\n")
3182 struct interface
*ifp
;
3183 struct rip_interface
*ri
;
3184 extern const struct message ri_version_msg
[];
3185 const char *send_version
;
3186 const char *receive_version
;
3187 const char *vrf_name
;
3190 if (argv_find(argv
, argc
, "vrf", &idx
))
3191 vrf_name
= argv
[idx
+ 1]->arg
;
3193 vrf_name
= VRF_DEFAULT_NAME
;
3195 rip
= rip_lookup_by_vrf_name(vrf_name
);
3197 vty_out(vty
, "%% RIP instance not found\n");
3200 if (!rip
->enabled
) {
3201 vty_out(vty
, "%% RIP instance is disabled\n");
3205 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3206 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3208 vty_out(vty
, " next due in %lu seconds\n",
3209 thread_timer_remain_second(rip
->t_update
));
3210 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3211 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3213 /* Filtering status show. */
3214 config_show_distribute(vty
, rip
->distribute_ctx
);
3216 /* Default metric information. */
3217 vty_out(vty
, " Default redistribution metric is %u\n",
3218 rip
->default_metric
);
3220 /* Redistribute information. */
3221 vty_out(vty
, " Redistributing:");
3222 rip_show_redistribute_config(vty
, rip
);
3225 vty_out(vty
, " Default version control: send version %s,",
3226 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3227 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3228 vty_out(vty
, " receive any version \n");
3230 vty_out(vty
, " receive version %s \n",
3231 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3233 vty_out(vty
, " Interface Send Recv Key-chain\n");
3235 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3241 if (ri
->enable_network
|| ri
->enable_interface
) {
3242 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3244 lookup_msg(ri_version_msg
,
3245 rip
->version_send
, NULL
);
3247 send_version
= lookup_msg(ri_version_msg
,
3250 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3252 lookup_msg(ri_version_msg
,
3253 rip
->version_recv
, NULL
);
3255 receive_version
= lookup_msg(
3256 ri_version_msg
, ri
->ri_receive
, NULL
);
3258 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3259 send_version
, receive_version
,
3260 ri
->key_chain
? ri
->key_chain
: "");
3264 vty_out(vty
, " Routing for Networks:\n");
3265 rip_show_network_config(vty
, rip
);
3268 int found_passive
= 0;
3269 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3272 if ((ri
->enable_network
|| ri
->enable_interface
)
3274 if (!found_passive
) {
3276 " Passive Interface(s):\n");
3279 vty_out(vty
, " %s\n", ifp
->name
);
3284 vty_out(vty
, " Routing Information Sources:\n");
3286 " Gateway BadPackets BadRoutes Distance Last Update\n");
3287 rip_peer_display(vty
, rip
);
3289 rip_distance_show(vty
, rip
);
3294 /* RIP configuration write function. */
3295 static int config_write_rip(struct vty
*vty
)
3300 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3301 char xpath
[XPATH_MAXLEN
];
3302 struct lyd_node
*dnode
;
3304 snprintf(xpath
, sizeof(xpath
),
3305 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3307 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3310 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3312 /* Distribute configuration. */
3313 config_write_distribute(vty
, rip
->distribute_ctx
);
3315 /* Interface routemap configuration */
3316 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3324 /* RIP node structure. */
3325 static struct cmd_node rip_node
= {RIP_NODE
, "%s(config-router)# ", 1};
3327 /* Distribute-list update functions. */
3328 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3329 struct distribute
*dist
)
3331 struct interface
*ifp
;
3332 struct rip_interface
*ri
;
3333 struct access_list
*alist
;
3334 struct prefix_list
*plist
;
3336 if (!ctx
->vrf
|| !dist
->ifname
)
3339 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3345 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3346 alist
= access_list_lookup(AFI_IP
,
3347 dist
->list
[DISTRIBUTE_V4_IN
]);
3349 ri
->list
[RIP_FILTER_IN
] = alist
;
3351 ri
->list
[RIP_FILTER_IN
] = NULL
;
3353 ri
->list
[RIP_FILTER_IN
] = NULL
;
3355 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3356 alist
= access_list_lookup(AFI_IP
,
3357 dist
->list
[DISTRIBUTE_V4_OUT
]);
3359 ri
->list
[RIP_FILTER_OUT
] = alist
;
3361 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3363 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3365 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3366 plist
= prefix_list_lookup(AFI_IP
,
3367 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3369 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3371 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3373 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3375 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3376 plist
= prefix_list_lookup(AFI_IP
,
3377 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3379 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3381 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3383 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3386 void rip_distribute_update_interface(struct interface
*ifp
)
3388 struct rip_interface
*ri
= ifp
->info
;
3389 struct rip
*rip
= ri
->rip
;
3390 struct distribute
*dist
;
3394 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3396 rip_distribute_update(rip
->distribute_ctx
, dist
);
3399 /* Update all interface's distribute list. */
3401 static void rip_distribute_update_all(struct prefix_list
*notused
)
3403 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3404 struct interface
*ifp
;
3406 FOR_ALL_INTERFACES (vrf
, ifp
)
3407 rip_distribute_update_interface(ifp
);
3410 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3412 rip_distribute_update_all(NULL
);
3415 /* Delete all added rip route. */
3416 void rip_clean(struct rip
*rip
)
3419 rip_instance_disable(rip
);
3421 stream_free(rip
->obuf
);
3423 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3424 if (rip
->redist
[i
].route_map
.name
)
3425 free(rip
->redist
[i
].route_map
.name
);
3427 route_table_finish(rip
->table
);
3428 route_table_finish(rip
->neighbor
);
3429 list_delete(&rip
->peer_list
);
3430 distribute_list_delete(&rip
->distribute_ctx
);
3431 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3433 rip_clean_network(rip
);
3434 rip_passive_nondefault_clean(rip
);
3435 vector_free(rip
->enable_interface
);
3436 route_table_finish(rip
->enable_network
);
3437 vector_free(rip
->passive_nondefault
);
3438 list_delete(&rip
->offset_list_master
);
3439 rip_interfaces_clean(rip
);
3440 route_table_finish(rip
->distance_table
);
3442 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3443 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3444 XFREE(MTYPE_RIP
, rip
);
3447 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3448 struct if_rmap
*if_rmap
)
3450 struct interface
*ifp
= NULL
;
3451 struct rip_interface
*ri
;
3452 struct route_map
*rmap
;
3453 struct vrf
*vrf
= NULL
;
3456 vrf
= vrf_lookup_by_name(ctx
->name
);
3458 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3463 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3464 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3466 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3468 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3470 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3472 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3473 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3475 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3477 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3479 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3482 void rip_if_rmap_update_interface(struct interface
*ifp
)
3484 struct rip_interface
*ri
= ifp
->info
;
3485 struct rip
*rip
= ri
->rip
;
3486 struct if_rmap
*if_rmap
;
3487 struct if_rmap_ctx
*ctx
;
3491 ctx
= rip
->if_rmap_ctx
;
3494 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3496 rip_if_rmap_update(ctx
, if_rmap
);
3499 static void rip_routemap_update_redistribute(struct rip
*rip
)
3501 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3502 if (rip
->redist
[i
].route_map
.name
) {
3503 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3504 rip
->redist
[i
].route_map
.name
);
3505 route_map_counter_increment(
3506 rip
->redist
[i
].route_map
.map
);
3512 static void rip_routemap_update(const char *notused
)
3514 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3516 struct interface
*ifp
;
3518 FOR_ALL_INTERFACES (vrf
, ifp
)
3519 rip_if_rmap_update_interface(ifp
);
3523 rip_routemap_update_redistribute(rip
);
3526 /* Link RIP instance to VRF. */
3527 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3529 struct interface
*ifp
;
3532 rip
->distribute_ctx
->vrf
= vrf
;
3535 FOR_ALL_INTERFACES (vrf
, ifp
)
3536 rip_interface_sync(ifp
);
3539 /* Unlink RIP instance from VRF. */
3540 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3542 struct interface
*ifp
;
3545 rip
->distribute_ctx
->vrf
= NULL
;
3548 FOR_ALL_INTERFACES (vrf
, ifp
)
3549 rip_interface_sync(ifp
);
3552 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3556 rip_vrf_link(rip
, vrf
);
3557 rip
->enabled
= true;
3559 /* Resend all redistribute requests. */
3560 rip_redistribute_enable(rip
);
3562 /* Create read and timer thread. */
3563 rip_event(rip
, RIP_READ
, rip
->sock
);
3564 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3566 rip_zebra_vrf_register(vrf
);
3569 static void rip_instance_disable(struct rip
*rip
)
3571 struct vrf
*vrf
= rip
->vrf
;
3572 struct route_node
*rp
;
3574 /* Clear RIP routes */
3575 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3576 struct rip_info
*rinfo
;
3578 struct listnode
*listnode
;
3580 if ((list
= rp
->info
) == NULL
)
3583 rinfo
= listgetdata(listhead(list
));
3584 if (rip_route_rte(rinfo
))
3585 rip_zebra_ipv4_delete(rip
, rp
);
3587 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3588 RIP_TIMER_OFF(rinfo
->t_timeout
);
3589 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3590 rip_info_free(rinfo
);
3594 route_unlock_node(rp
);
3597 /* Flush all redistribute requests. */
3598 rip_redistribute_disable(rip
);
3600 /* Cancel RIP related timers. */
3601 RIP_TIMER_OFF(rip
->t_update
);
3602 RIP_TIMER_OFF(rip
->t_triggered_update
);
3603 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3605 /* Cancel read thread. */
3606 THREAD_READ_OFF(rip
->t_read
);
3608 /* Close RIP socket. */
3612 /* Clear existing peers. */
3613 list_delete_all_node(rip
->peer_list
);
3615 rip_zebra_vrf_deregister(vrf
);
3617 rip_vrf_unlink(rip
, vrf
);
3618 rip
->enabled
= false;
3621 static int rip_vrf_new(struct vrf
*vrf
)
3623 if (IS_RIP_DEBUG_EVENT
)
3624 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3630 static int rip_vrf_delete(struct vrf
*vrf
)
3632 if (IS_RIP_DEBUG_EVENT
)
3633 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3639 static int rip_vrf_enable(struct vrf
*vrf
)
3644 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3646 char *old_vrf_name
= NULL
;
3648 rip
= (struct rip
*)vrf
->info
;
3651 /* update vrf name */
3653 old_vrf_name
= rip
->vrf_name
;
3654 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf
->name
);
3656 * HACK: Change the RIP VRF in the running configuration directly,
3657 * bypassing the northbound layer. This is necessary to avoid deleting
3658 * the RIP and readding it in the new VRF, which would have
3659 * several implications.
3661 if (yang_module_find("frr-ripd") && old_vrf_name
) {
3662 struct lyd_node
*rip_dnode
;
3664 rip_dnode
= yang_dnode_get(
3665 running_config
->dnode
,
3666 "/frr-ripd:ripd/instance[vrf='%s']/vrf",
3669 yang_dnode_change_leaf(rip_dnode
, vrf
->name
);
3670 running_config
->version
++;
3673 XFREE(MTYPE_RIP_VRF_NAME
, old_vrf_name
);
3675 if (!rip
|| rip
->enabled
)
3678 if (IS_RIP_DEBUG_EVENT
)
3679 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3682 /* Activate the VRF RIP instance. */
3683 if (!rip
->enabled
) {
3684 socket
= rip_create_socket(vrf
);
3688 rip_instance_enable(rip
, vrf
, socket
);
3694 static int rip_vrf_disable(struct vrf
*vrf
)
3698 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3699 if (!rip
|| !rip
->enabled
)
3702 if (IS_RIP_DEBUG_EVENT
)
3703 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3706 /* Deactivate the VRF RIP instance. */
3708 rip_instance_disable(rip
);
3713 void rip_vrf_init(void)
3715 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3719 void rip_vrf_terminate(void)
3724 /* Allocate new rip structure and set default value. */
3727 /* Install top nodes. */
3728 install_node(&rip_node
, config_write_rip
);
3730 /* Install rip commands. */
3731 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3732 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3734 install_default(RIP_NODE
);
3736 /* Debug related init. */
3739 /* Access list install. */
3741 access_list_add_hook(rip_distribute_update_all_wrapper
);
3742 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3744 /* Prefix list initialize.*/
3746 prefix_list_add_hook(rip_distribute_update_all
);
3747 prefix_list_delete_hook(rip_distribute_update_all
);
3749 /* Distribute list install. */
3750 distribute_list_init(RIP_NODE
);
3753 rip_route_map_init();
3755 route_map_add_hook(rip_routemap_update
);
3756 route_map_delete_hook(rip_routemap_update
);
3758 if_rmap_init(RIP_NODE
);