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
1227 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1228 zlog_info("RIPv1 packet with nexthop value %s",
1229 inet_ntoa(rte
->nexthop
));
1230 rip_peer_bad_route(rip
, from
);
1234 /* That is, if the provided information is ignored, a possibly
1235 sub-optimal, but absolutely valid, route may be taken. If
1236 the received Next Hop is not directly reachable, it should be
1237 treated as 0.0.0.0. */
1238 if (packet
->version
== RIPv2
1239 && rte
->nexthop
.s_addr
!= INADDR_ANY
) {
1242 /* Multicast address check. */
1243 addrval
= ntohl(rte
->nexthop
.s_addr
);
1244 if (IN_CLASSD(addrval
)) {
1246 "Nexthop %s is multicast address, skip this rte",
1247 inet_ntoa(rte
->nexthop
));
1251 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1252 rip
->vrf
->vrf_id
)) {
1253 struct route_node
*rn
;
1254 struct rip_info
*rinfo
;
1256 rn
= route_node_match_ipv4(rip
->table
,
1262 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1265 if (IS_RIP_DEBUG_EVENT
)
1267 "Next hop %s is on RIP network. Set nexthop to the packet's originator",
1270 rte
->nexthop
= rinfo
->from
;
1272 if (IS_RIP_DEBUG_EVENT
)
1274 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1277 rte
->nexthop
.s_addr
=
1281 route_unlock_node(rn
);
1283 if (IS_RIP_DEBUG_EVENT
)
1285 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1288 rte
->nexthop
.s_addr
= INADDR_ANY
;
1293 /* For RIPv1, there won't be a valid netmask.
1295 This is a best guess at the masks. If everyone was using old
1296 Ciscos before the 'ip subnet zero' option, it would be almost
1299 Cisco summarize ripv1 advertisements to the classful boundary
1300 (/16 for class B's) except when the RIP packet does to inside
1301 the classful network in question. */
1303 if ((packet
->version
== RIPv1
1304 && rte
->prefix
.s_addr
!= INADDR_ANY
)
1305 || (packet
->version
== RIPv2
1306 && (rte
->prefix
.s_addr
!= INADDR_ANY
1307 && rte
->mask
.s_addr
== INADDR_ANY
))) {
1308 uint32_t destination
;
1310 if (subnetted
== -1) {
1311 memcpy(&ifaddr
, ifc
->address
,
1312 sizeof(struct prefix_ipv4
));
1313 memcpy(&ifaddrclass
, &ifaddr
,
1314 sizeof(struct prefix_ipv4
));
1315 apply_classful_mask_ipv4(&ifaddrclass
);
1317 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1321 destination
= ntohl(rte
->prefix
.s_addr
);
1323 if (IN_CLASSA(destination
))
1324 masklen2ip(8, &rte
->mask
);
1325 else if (IN_CLASSB(destination
))
1326 masklen2ip(16, &rte
->mask
);
1327 else if (IN_CLASSC(destination
))
1328 masklen2ip(24, &rte
->mask
);
1331 masklen2ip(ifaddrclass
.prefixlen
,
1332 (struct in_addr
*)&destination
);
1333 if ((subnetted
== 1)
1334 && ((rte
->prefix
.s_addr
& destination
)
1335 == ifaddrclass
.prefix
.s_addr
)) {
1336 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1337 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1338 != rte
->prefix
.s_addr
)
1339 masklen2ip(32, &rte
->mask
);
1340 if (IS_RIP_DEBUG_EVENT
)
1341 zlog_debug("Subnetted route %s",
1342 inet_ntoa(rte
->prefix
));
1344 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1345 != rte
->prefix
.s_addr
)
1349 if (IS_RIP_DEBUG_EVENT
) {
1350 zlog_debug("Resultant route %s",
1351 inet_ntoa(rte
->prefix
));
1352 zlog_debug("Resultant mask %s",
1353 inet_ntoa(rte
->mask
));
1357 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1358 ignore the entry. */
1359 if ((packet
->version
== RIPv2
)
1360 && (rte
->mask
.s_addr
!= INADDR_ANY
)
1361 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1362 != rte
->prefix
.s_addr
)) {
1364 "RIPv2 address %s is not mask /%d applied one",
1365 inet_ntoa(rte
->prefix
), ip_masklen(rte
->mask
));
1366 rip_peer_bad_route(rip
, from
);
1370 /* Default route's netmask is ignored. */
1371 if (packet
->version
== RIPv2
1372 && (rte
->prefix
.s_addr
== INADDR_ANY
)
1373 && (rte
->mask
.s_addr
!= INADDR_ANY
)) {
1374 if (IS_RIP_DEBUG_EVENT
)
1376 "Default route with non-zero netmask. Set zero to netmask");
1377 rte
->mask
.s_addr
= INADDR_ANY
;
1380 /* Routing table updates. */
1381 rip_rte_process(rte
, from
, ifc
->ifp
);
1385 /* Make socket for RIP protocol. */
1386 int rip_create_socket(struct vrf
*vrf
)
1390 struct sockaddr_in addr
;
1391 const char *vrf_dev
= NULL
;
1393 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1394 addr
.sin_family
= AF_INET
;
1395 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1396 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1397 addr
.sin_len
= sizeof(struct sockaddr_in
);
1398 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1399 /* sending port must always be the RIP port */
1400 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1402 /* Make datagram socket. */
1403 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1404 vrf_dev
= vrf
->name
;
1405 frr_with_privs(&ripd_privs
) {
1406 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1409 flog_err_sys(EC_LIB_SOCKET
,
1410 "Cannot create UDP socket: %s",
1411 safe_strerror(errno
));
1416 sockopt_broadcast(sock
);
1417 sockopt_reuseaddr(sock
);
1418 sockopt_reuseport(sock
);
1419 setsockopt_ipv4_multicast_loop(sock
, 0);
1420 #ifdef IPTOS_PREC_INTERNETCONTROL
1421 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1423 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1425 frr_with_privs(&ripd_privs
) {
1426 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1428 zlog_err("%s: Can't bind socket %d to %s port %d: %s",
1429 __func__
, sock
, inet_ntoa(addr
.sin_addr
),
1430 (int)ntohs(addr
.sin_port
),
1431 safe_strerror(errno
));
1441 /* RIP packet send to destination address, on interface denoted by
1442 * by connected argument. NULL to argument denotes destination should be
1443 * should be RIP multicast group
1445 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1446 struct connected
*ifc
)
1448 struct rip_interface
*ri
;
1451 struct sockaddr_in sin
;
1455 struct cmsghdr
*cmsgptr
;
1456 char adata
[256] = {};
1457 struct in_pktinfo
*pkt
;
1458 #endif /* GNU_LINUX */
1460 assert(ifc
!= NULL
);
1461 ri
= ifc
->ifp
->info
;
1464 if (IS_RIP_DEBUG_PACKET
) {
1465 #define ADDRESS_SIZE 20
1466 char dst
[ADDRESS_SIZE
];
1469 strlcpy(dst
, inet_ntoa(to
->sin_addr
), sizeof(dst
));
1471 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1472 strlcpy(dst
, inet_ntoa(sin
.sin_addr
), sizeof(dst
));
1475 zlog_debug("rip_send_packet %s > %s (%s)",
1476 inet_ntoa(ifc
->address
->u
.prefix4
), dst
,
1480 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1482 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1484 * with multiple addresses on the same subnet: the first address
1485 * on the subnet is configured "primary", and all subsequent
1487 * on that subnet are treated as "secondary" addresses.
1488 * In order to avoid routing-table bloat on other rip listeners,
1489 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1491 * XXX Since Linux is the only system for which the
1492 * ZEBRA_IFA_SECONDARY
1493 * flag is set, we would end up sending a packet for a
1495 * source address on non-linux systems.
1497 if (IS_RIP_DEBUG_PACKET
)
1498 zlog_debug("duplicate dropped");
1502 /* Make destination address. */
1503 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1504 sin
.sin_family
= AF_INET
;
1505 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1506 sin
.sin_len
= sizeof(struct sockaddr_in
);
1507 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1509 /* When destination is specified, use it's port and address. */
1511 sin
.sin_port
= to
->sin_port
;
1512 sin
.sin_addr
= to
->sin_addr
;
1514 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1515 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1517 rip_interface_multicast_set(rip
->sock
, ifc
);
1520 memset(&msg
, 0, sizeof(msg
));
1521 msg
.msg_name
= (void *)&sin
;
1522 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1529 msg
.msg_control
= (void *)adata
;
1530 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1532 cmsgptr
= (struct cmsghdr
*)adata
;
1533 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1534 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1535 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1536 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1537 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1538 #endif /* GNU_LINUX */
1540 ret
= sendmsg(rip
->sock
, &msg
, 0);
1542 if (IS_RIP_DEBUG_EVENT
)
1543 zlog_debug("SEND to %s.%d", inet_ntoa(sin
.sin_addr
),
1544 ntohs(sin
.sin_port
));
1547 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1552 /* Add redistributed route to RIP table. */
1553 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1554 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1555 unsigned int metric
, unsigned char distance
,
1559 struct route_node
*rp
= NULL
;
1560 struct rip_info
*rinfo
= NULL
, newinfo
;
1561 struct list
*list
= NULL
;
1563 /* Redistribute route */
1564 ret
= rip_destination_check(p
->prefix
);
1568 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1570 memset(&newinfo
, 0, sizeof(struct rip_info
));
1571 newinfo
.type
= type
;
1572 newinfo
.sub_type
= sub_type
;
1574 newinfo
.external_metric
= metric
;
1575 newinfo
.distance
= distance
;
1576 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1581 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1582 rinfo
= listgetdata(listhead(list
));
1584 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1585 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1586 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1587 route_unlock_node(rp
);
1591 /* Manually configured RIP route check. */
1592 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1593 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1594 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1595 if (type
!= ZEBRA_ROUTE_RIP
1596 || ((sub_type
!= RIP_ROUTE_STATIC
)
1597 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1598 route_unlock_node(rp
);
1603 (void)rip_ecmp_replace(rip
, &newinfo
);
1604 route_unlock_node(rp
);
1606 (void)rip_ecmp_add(rip
, &newinfo
);
1608 if (IS_RIP_DEBUG_EVENT
) {
1609 zlog_debug("Redistribute new prefix %s/%d",
1610 inet_ntoa(p
->prefix
), p
->prefixlen
);
1613 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1616 /* Delete redistributed route from RIP table. */
1617 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1618 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1621 struct route_node
*rp
;
1622 struct rip_info
*rinfo
;
1624 ret
= rip_destination_check(p
->prefix
);
1628 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1630 struct list
*list
= rp
->info
;
1632 if (list
!= NULL
&& listcount(list
) != 0) {
1633 rinfo
= listgetdata(listhead(list
));
1634 if (rinfo
!= NULL
&& rinfo
->type
== type
1635 && rinfo
->sub_type
== sub_type
1636 && rinfo
->nh
.ifindex
== ifindex
) {
1637 /* Perform poisoned reverse. */
1638 rinfo
->metric
= RIP_METRIC_INFINITY
;
1639 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1640 rip_garbage_collect
,
1642 RIP_TIMER_OFF(rinfo
->t_timeout
);
1643 rinfo
->flags
|= RIP_RTF_CHANGED
;
1645 if (IS_RIP_DEBUG_EVENT
)
1647 "Poison %s/%d on the interface %s with an "
1648 "infinity metric [delete]",
1649 inet_ntoa(p
->prefix
),
1655 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1658 route_unlock_node(rp
);
1662 /* Response to request called from rip_read ().*/
1663 static void rip_request_process(struct rip_packet
*packet
, int size
,
1664 struct sockaddr_in
*from
, struct connected
*ifc
)
1669 struct prefix_ipv4 p
;
1670 struct route_node
*rp
;
1671 struct rip_info
*rinfo
;
1672 struct rip_interface
*ri
;
1674 /* Does not reponse to the requests on the loopback interfaces */
1675 if (if_is_loopback(ifc
->ifp
))
1678 /* Check RIP process is enabled on this interface. */
1679 ri
= ifc
->ifp
->info
;
1684 /* When passive interface is specified, suppress responses */
1688 /* RIP peer update. */
1689 rip_peer_update(rip
, from
, packet
->version
);
1691 lim
= ((caddr_t
)packet
) + size
;
1694 /* The Request is processed entry by entry. If there are no
1695 entries, no response is given. */
1696 if (lim
== (caddr_t
)rte
)
1699 /* There is one special case. If there is exactly one entry in the
1700 request, and it has an address family identifier of zero and a
1701 metric of infinity (i.e., 16), then this is a request to send the
1702 entire routing table. */
1703 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1704 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1705 /* All route with split horizon */
1706 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1708 if (ntohs(rte
->family
) != AF_INET
)
1711 /* Examine the list of RTEs in the Request one by one. For each
1712 entry, look up the destination in the router's routing
1713 database and, if there is a route, put that route's metric in
1714 the metric field of the RTE. If there is no explicit route
1715 to the specified destination, put infinity in the metric
1716 field. Once all the entries have been filled in, change the
1717 command from Request to Response and send the datagram back
1718 to the requestor. */
1721 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1722 p
.prefix
= rte
->prefix
;
1723 p
.prefixlen
= ip_masklen(rte
->mask
);
1724 apply_mask_ipv4(&p
);
1726 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1728 rinfo
= listgetdata(
1729 listhead((struct list
*)rp
->info
));
1730 rte
->metric
= htonl(rinfo
->metric
);
1731 route_unlock_node(rp
);
1733 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1735 packet
->command
= RIP_RESPONSE
;
1737 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1739 rip
->counters
.queries
++;
1742 /* First entry point of RIP packet. */
1743 static int rip_read(struct thread
*t
)
1745 struct rip
*rip
= THREAD_ARG(t
);
1749 union rip_buf rip_buf
;
1750 struct rip_packet
*packet
;
1751 struct sockaddr_in from
;
1755 struct interface
*ifp
= NULL
;
1756 struct connected
*ifc
;
1757 struct rip_interface
*ri
;
1760 /* Fetch socket then register myself. */
1761 sock
= THREAD_FD(t
);
1764 /* Add myself to tne next event */
1765 rip_event(rip
, RIP_READ
, sock
);
1767 /* RIPd manages only IPv4. */
1768 memset(&from
, 0, sizeof(struct sockaddr_in
));
1769 fromlen
= sizeof(struct sockaddr_in
);
1771 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1772 (struct sockaddr
*)&from
, &fromlen
);
1774 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1775 safe_strerror(errno
));
1779 /* Check is this packet comming from myself? */
1780 if (if_check_address(rip
, from
.sin_addr
)) {
1781 if (IS_RIP_DEBUG_PACKET
)
1782 zlog_debug("ignore packet comes from myself (VRF %s)",
1787 /* Which interface is this packet comes from. */
1788 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1793 /* RIP packet received */
1794 if (IS_RIP_DEBUG_EVENT
)
1795 zlog_debug("RECV packet from %s port %d on %s (VRF %s)",
1796 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1797 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1799 /* If this packet come from unknown interface, ignore it. */
1802 "rip_read: cannot find interface for packet from %s port %d (VRF %s)",
1803 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1809 p
.u
.prefix4
= from
.sin_addr
;
1810 p
.prefixlen
= IPV4_MAX_BITLEN
;
1812 ifc
= connected_lookup_prefix(ifp
, &p
);
1816 "rip_read: cannot find connected address for packet from %s "
1817 "port %d on interface %s (VRF %s)",
1818 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1819 ifp
->name
, rip
->vrf_name
);
1823 /* Packet length check. */
1824 if (len
< RIP_PACKET_MINSIZ
) {
1825 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1827 rip_peer_bad_packet(rip
, &from
);
1830 if (len
> RIP_PACKET_MAXSIZ
) {
1831 zlog_warn("packet size %d is larger than max size %d", len
,
1833 rip_peer_bad_packet(rip
, &from
);
1837 /* Packet alignment check. */
1838 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1839 zlog_warn("packet size %d is wrong for RIP packet alignment",
1841 rip_peer_bad_packet(rip
, &from
);
1845 /* Set RTE number. */
1846 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1848 /* For easy to handle. */
1849 packet
= &rip_buf
.rip_packet
;
1851 /* RIP version check. */
1852 if (packet
->version
== 0) {
1853 zlog_info("version 0 with command %d received.",
1855 rip_peer_bad_packet(rip
, &from
);
1859 /* Dump RIP packet. */
1860 if (IS_RIP_DEBUG_RECV
)
1861 rip_packet_dump(packet
, len
, "RECV");
1863 /* RIP version adjust. This code should rethink now. RFC1058 says
1864 that "Version 1 implementations are to ignore this extra data and
1865 process only the fields specified in this document.". So RIPv3
1866 packet should be treated as RIPv1 ignoring must be zero field. */
1867 if (packet
->version
> RIPv2
)
1868 packet
->version
= RIPv2
;
1870 /* Is RIP running or is this RIP neighbor ?*/
1872 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1873 if (IS_RIP_DEBUG_EVENT
)
1874 zlog_debug("RIP is not enabled on interface %s.",
1876 rip_peer_bad_packet(rip
, &from
);
1880 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1881 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1883 if (vrecv
== RI_RIP_VERSION_NONE
1884 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1885 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1886 if (IS_RIP_DEBUG_PACKET
)
1888 " packet's v%d doesn't fit to if version spec",
1890 rip_peer_bad_packet(rip
, &from
);
1894 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1895 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1896 accepted; authenticated RIP-2 messages shall be discarded. */
1897 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1898 && (packet
->version
== RIPv2
)
1899 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1900 if (IS_RIP_DEBUG_EVENT
)
1902 "packet RIPv%d is dropped because authentication disabled",
1904 ripd_notif_send_auth_type_failure(ifp
->name
);
1905 rip_peer_bad_packet(rip
, &from
);
1910 If the router is configured to authenticate RIP-2 messages, then
1911 RIP-1 messages and RIP-2 messages which pass authentication
1912 testing shall be accepted; unauthenticated and failed
1913 authentication RIP-2 messages shall be discarded. For maximum
1914 security, RIP-1 messages should be ignored when authentication is
1915 in use (see section 4.1); otherwise, the routing information from
1916 authenticated messages will be propagated by RIP-1 routers in an
1917 unauthenticated manner.
1919 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1920 * always reply regardless of authentication settings, because:
1922 * - if there other authorised routers on-link, the REQUESTor can
1923 * passively obtain the routing updates anyway
1924 * - if there are no other authorised routers on-link, RIP can
1925 * easily be disabled for the link to prevent giving out information
1926 * on state of this routers RIP routing table..
1928 * I.e. if RIPv1 has any place anymore these days, it's as a very
1929 * simple way to distribute routing information (e.g. to embedded
1930 * hosts / appliances) and the ability to give out RIPv1
1931 * routing-information freely, while still requiring RIPv2
1932 * authentication for any RESPONSEs might be vaguely useful.
1934 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1935 /* Discard RIPv1 messages other than REQUESTs */
1936 if (packet
->command
!= RIP_REQUEST
) {
1937 if (IS_RIP_DEBUG_PACKET
)
1940 " dropped because authentication enabled");
1941 ripd_notif_send_auth_type_failure(ifp
->name
);
1942 rip_peer_bad_packet(rip
, &from
);
1945 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1946 const char *auth_desc
;
1949 /* There definitely is no authentication in the packet.
1951 if (IS_RIP_DEBUG_PACKET
)
1953 "RIPv2 authentication failed: no auth RTE in packet");
1954 ripd_notif_send_auth_type_failure(ifp
->name
);
1955 rip_peer_bad_packet(rip
, &from
);
1959 /* First RTE must be an Authentication Family RTE */
1960 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1961 if (IS_RIP_DEBUG_PACKET
)
1964 " dropped because authentication enabled");
1965 ripd_notif_send_auth_type_failure(ifp
->name
);
1966 rip_peer_bad_packet(rip
, &from
);
1970 /* Check RIPv2 authentication. */
1971 switch (ntohs(packet
->rte
->tag
)) {
1972 case RIP_AUTH_SIMPLE_PASSWORD
:
1973 auth_desc
= "simple";
1974 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1979 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1980 /* Reset RIP packet length to trim MD5 data. */
1986 auth_desc
= "unknown type";
1987 if (IS_RIP_DEBUG_PACKET
)
1989 "RIPv2 Unknown authentication type %d",
1990 ntohs(packet
->rte
->tag
));
1994 if (IS_RIP_DEBUG_PACKET
)
1995 zlog_debug("RIPv2 %s authentication success",
1998 if (IS_RIP_DEBUG_PACKET
)
1999 zlog_debug("RIPv2 %s authentication failure",
2001 ripd_notif_send_auth_failure(ifp
->name
);
2002 rip_peer_bad_packet(rip
, &from
);
2007 /* Process each command. */
2008 switch (packet
->command
) {
2010 rip_response_process(packet
, len
, &from
, ifc
);
2014 rip_request_process(packet
, len
, &from
, ifc
);
2019 "Obsolete command %s received, please sent it to routed",
2020 lookup_msg(rip_msg
, packet
->command
, NULL
));
2021 rip_peer_bad_packet(rip
, &from
);
2023 case RIP_POLL_ENTRY
:
2024 zlog_info("Obsolete command %s received",
2025 lookup_msg(rip_msg
, packet
->command
, NULL
));
2026 rip_peer_bad_packet(rip
, &from
);
2029 zlog_info("Unknown RIP command %d received", packet
->command
);
2030 rip_peer_bad_packet(rip
, &from
);
2037 /* Write routing table entry to the stream and return next index of
2038 the routing table entry in the stream. */
2039 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2040 uint8_t version
, struct rip_info
*rinfo
)
2042 struct in_addr mask
;
2044 /* Write routing table entry. */
2045 if (version
== RIPv1
) {
2046 stream_putw(s
, AF_INET
);
2048 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2049 stream_put_ipv4(s
, 0);
2050 stream_put_ipv4(s
, 0);
2051 stream_putl(s
, rinfo
->metric_out
);
2053 masklen2ip(p
->prefixlen
, &mask
);
2055 stream_putw(s
, AF_INET
);
2056 stream_putw(s
, rinfo
->tag_out
);
2057 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2058 stream_put_ipv4(s
, mask
.s_addr
);
2059 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2060 stream_putl(s
, rinfo
->metric_out
);
2066 /* Send update to the ifp or spcified neighbor. */
2067 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2068 int route_type
, uint8_t version
)
2073 struct route_node
*rp
;
2074 struct rip_info
*rinfo
;
2075 struct rip_interface
*ri
;
2076 struct prefix_ipv4
*p
;
2077 struct prefix_ipv4 classfull
;
2078 struct prefix_ipv4 ifaddrclass
;
2079 struct key
*key
= NULL
;
2080 /* this might need to made dynamic if RIP ever supported auth methods
2081 with larger key string sizes */
2082 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2083 size_t doff
= 0; /* offset of digest offset field */
2087 struct list
*list
= NULL
;
2088 struct listnode
*listnode
= NULL
;
2090 /* Logging output event. */
2091 if (IS_RIP_DEBUG_EVENT
) {
2093 zlog_debug("update routes to neighbor %s",
2094 inet_ntoa(to
->sin_addr
));
2096 zlog_debug("update routes on interface %s ifindex %d",
2097 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2100 /* Get RIP interface. */
2101 ri
= ifc
->ifp
->info
;
2104 /* Set output stream. */
2107 /* Reset stream and RTE counter. */
2109 rtemax
= RIP_MAX_RTE
;
2111 /* If output interface is in simple password authentication mode, we
2112 need space for authentication data. */
2113 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2116 /* If output interface is in MD5 authentication mode, we need space
2117 for authentication header and data. */
2118 if (ri
->auth_type
== RIP_AUTH_MD5
)
2121 /* If output interface is in simple password authentication mode
2122 and string or keychain is specified we need space for auth. data */
2123 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2124 if (ri
->key_chain
) {
2125 struct keychain
*keychain
;
2127 keychain
= keychain_lookup(ri
->key_chain
);
2129 key
= key_lookup_for_send(keychain
);
2131 /* to be passed to auth functions later */
2132 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2133 if (strlen(auth_str
) == 0)
2137 if (version
== RIPv1
) {
2138 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2139 apply_classful_mask_ipv4(&ifaddrclass
);
2141 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2145 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2146 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2147 rinfo
= listgetdata(listhead(list
));
2148 /* For RIPv1, if we are subnetted, output subnets in our
2150 /* that have the same mask as the output "interface".
2152 /* networks, only the classfull version is output. */
2154 if (version
== RIPv1
) {
2155 p
= (struct prefix_ipv4
*)&rp
->p
;
2157 if (IS_RIP_DEBUG_PACKET
)
2159 "RIPv1 mask check, %s/%d considered for output",
2160 inet_ntoa(rp
->p
.u
.prefix4
),
2165 (struct prefix
*)&ifaddrclass
,
2167 if ((ifc
->address
->prefixlen
2169 && (rp
->p
.prefixlen
!= 32))
2172 memcpy(&classfull
, &rp
->p
,
2173 sizeof(struct prefix_ipv4
));
2174 apply_classful_mask_ipv4(&classfull
);
2175 if (rp
->p
.u
.prefix4
.s_addr
!= 0
2176 && classfull
.prefixlen
2180 if (IS_RIP_DEBUG_PACKET
)
2182 "RIPv1 mask check, %s/%d made it through",
2183 inet_ntoa(rp
->p
.u
.prefix4
),
2186 p
= (struct prefix_ipv4
*)&rp
->p
;
2188 /* Apply output filters. */
2189 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2193 /* Changed route only output. */
2194 if (route_type
== rip_changed_route
2195 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2198 /* Split horizon. */
2199 /* if (split_horizon == rip_split_horizon) */
2200 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2202 * We perform split horizon for RIP and
2204 * For rip routes, we want to suppress the route
2206 * end up sending the route back on the
2208 * learned it from, with a higher metric. For
2210 * we suppress the route if the prefix is a
2212 * source address that we are going to use for
2214 * (in order to handle the case when multiple
2216 * configured on the same interface).
2219 struct rip_info
*tmp_rinfo
= NULL
;
2220 struct connected
*tmp_ifc
= NULL
;
2222 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2224 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2225 && tmp_rinfo
->nh
.ifindex
2226 == ifc
->ifp
->ifindex
) {
2232 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2233 for (ALL_LIST_ELEMENTS_RO(
2234 ifc
->ifp
->connected
,
2238 tmp_ifc
->address
)) {
2248 /* Preparation for route-map. */
2249 rinfo
->metric_set
= 0;
2250 rinfo
->nexthop_out
.s_addr
= 0;
2251 rinfo
->metric_out
= rinfo
->metric
;
2252 rinfo
->tag_out
= rinfo
->tag
;
2253 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2255 /* In order to avoid some local loops,
2256 * if the RIP route has a nexthop via this interface,
2258 * otherwise set it to 0. The nexthop should not be
2260 * beyond the local broadcast/multicast area in order
2261 * to avoid an IGP multi-level recursive look-up.
2264 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2265 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2267 /* Interface route-map */
2268 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2269 ret
= route_map_apply(
2270 ri
->routemap
[RIP_FILTER_OUT
],
2271 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2273 if (ret
== RMAP_DENYMATCH
) {
2274 if (IS_RIP_DEBUG_PACKET
)
2276 "RIP %s/%d is filtered by route-map out",
2277 inet_ntoa(p
->prefix
),
2283 /* Apply redistribute route map - continue, if deny */
2284 if (rip
->redist
[rinfo
->type
].route_map
.name
2285 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2286 ret
= route_map_apply(
2287 rip
->redist
[rinfo
->type
].route_map
.map
,
2288 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2290 if (ret
== RMAP_DENYMATCH
) {
2291 if (IS_RIP_DEBUG_PACKET
)
2293 "%s/%d is filtered by route-map",
2294 inet_ntoa(p
->prefix
),
2300 /* When route-map does not set metric. */
2301 if (!rinfo
->metric_set
) {
2302 /* If redistribute metric is set. */
2303 if (rip
->redist
[rinfo
->type
].metric_config
2304 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2306 rip
->redist
[rinfo
->type
].metric
;
2308 /* If the route is not connected or
2310 one, use default-metric value*/
2311 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2313 != ZEBRA_ROUTE_CONNECT
2315 != RIP_METRIC_INFINITY
)
2317 rip
->default_metric
;
2321 /* Apply offset-list */
2322 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2323 rip_offset_list_apply_out(p
, ifc
->ifp
,
2324 &rinfo
->metric_out
);
2326 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2327 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2329 /* Perform split-horizon with poisoned reverse
2330 * for RIP and connected routes.
2332 if (ri
->split_horizon
2333 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2335 * We perform split horizon for RIP and
2337 * For rip routes, we want to suppress the route
2339 * end up sending the route back on the
2341 * learned it from, with a higher metric. For
2343 * we suppress the route if the prefix is a
2345 * source address that we are going to use for
2347 * (in order to handle the case when multiple
2349 * configured on the same interface).
2351 struct rip_info
*tmp_rinfo
= NULL
;
2352 struct connected
*tmp_ifc
= NULL
;
2354 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2356 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2357 && tmp_rinfo
->nh
.ifindex
2358 == ifc
->ifp
->ifindex
)
2360 RIP_METRIC_INFINITY
;
2362 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2363 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2364 for (ALL_LIST_ELEMENTS_RO(
2365 ifc
->ifp
->connected
,
2369 tmp_ifc
->address
)) {
2371 RIP_METRIC_INFINITY
;
2377 /* Prepare preamble, auth headers, if needs be */
2379 stream_putc(s
, RIP_RESPONSE
);
2380 stream_putc(s
, version
);
2383 /* auth header for !v1 && !no_auth */
2384 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2385 && (version
!= RIPv1
))
2386 doff
= rip_auth_header_write(
2387 s
, ri
, key
, auth_str
,
2388 RIP_AUTH_SIMPLE_SIZE
);
2391 /* Write RTE to the stream. */
2392 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2393 if (num
== rtemax
) {
2394 if (version
== RIPv2
2395 && ri
->auth_type
== RIP_AUTH_MD5
)
2396 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2397 RIP_AUTH_SIMPLE_SIZE
);
2399 ret
= rip_send_packet(STREAM_DATA(s
),
2400 stream_get_endp(s
), to
,
2403 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2404 rip_packet_dump((struct rip_packet
*)
2413 /* Flush unwritten RTE. */
2415 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2416 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2417 RIP_AUTH_SIMPLE_SIZE
);
2419 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2422 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2423 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2424 stream_get_endp(s
), "SEND");
2428 /* Statistics updates. */
2432 /* Send RIP packet to the interface. */
2433 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2436 struct interface
*ifp
= ifc
->ifp
;
2437 struct rip_interface
*ri
= ifp
->info
;
2438 struct sockaddr_in to
;
2440 /* When RIP version is 2 and multicast enable interface. */
2441 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2442 if (IS_RIP_DEBUG_EVENT
)
2443 zlog_debug("multicast announce on %s ", ifp
->name
);
2445 rip_output_process(ifc
, NULL
, route_type
, version
);
2449 /* If we can't send multicast packet, send it with unicast. */
2450 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2451 if (ifc
->address
->family
== AF_INET
) {
2452 /* Destination address and port setting. */
2453 memset(&to
, 0, sizeof(struct sockaddr_in
));
2454 if (ifc
->destination
)
2455 /* use specified broadcast or peer destination
2457 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2458 else if (ifc
->address
->prefixlen
< IPV4_MAX_PREFIXLEN
)
2459 /* calculate the appropriate broadcast address
2461 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2462 ifc
->address
->u
.prefix4
.s_addr
,
2463 ifc
->address
->prefixlen
);
2465 /* do not know where to send the packet */
2467 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2469 if (IS_RIP_DEBUG_EVENT
)
2470 zlog_debug("%s announce to %s on %s",
2471 CONNECTED_PEER(ifc
) ? "unicast"
2473 inet_ntoa(to
.sin_addr
), ifp
->name
);
2475 rip_output_process(ifc
, &to
, route_type
, version
);
2480 /* Update send to all interface and neighbor. */
2481 static void rip_update_process(struct rip
*rip
, int route_type
)
2483 struct listnode
*ifnode
, *ifnnode
;
2484 struct connected
*connected
;
2485 struct interface
*ifp
;
2486 struct rip_interface
*ri
;
2487 struct route_node
*rp
;
2488 struct sockaddr_in to
;
2491 /* Send RIP update to each interface. */
2492 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2493 if (if_is_loopback(ifp
))
2496 if (!if_is_operative(ifp
))
2499 /* Fetch RIP interface information. */
2502 /* When passive interface is specified, suppress announce to the
2509 * If there is no version configuration in the
2511 * use rip's version setting.
2513 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2517 if (IS_RIP_DEBUG_EVENT
)
2518 zlog_debug("SEND UPDATE to %s ifindex %d",
2519 ifp
->name
, ifp
->ifindex
);
2521 /* send update on each connected network */
2522 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2524 if (connected
->address
->family
== AF_INET
) {
2526 rip_update_interface(
2530 && if_is_multicast(ifp
))
2531 rip_update_interface(
2539 /* RIP send updates to each neighbor. */
2540 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2541 if (rp
->info
!= NULL
) {
2544 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2548 "Neighbor %s doesn't have connected interface!",
2549 inet_ntoa(p
->u
.prefix4
));
2553 /* Set destination address and port */
2554 memset(&to
, 0, sizeof(struct sockaddr_in
));
2555 to
.sin_addr
= p
->u
.prefix4
;
2556 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2558 /* RIP version is rip's configuration. */
2559 rip_output_process(connected
, &to
, route_type
,
2564 /* RIP's periodical timer. */
2565 static int rip_update(struct thread
*t
)
2567 struct rip
*rip
= THREAD_ARG(t
);
2569 /* Clear timer pointer. */
2570 rip
->t_update
= NULL
;
2572 if (IS_RIP_DEBUG_EVENT
)
2573 zlog_debug("update timer fire!");
2575 /* Process update output. */
2576 rip_update_process(rip
, rip_all_route
);
2578 /* Triggered updates may be suppressed if a regular update is due by
2579 the time the triggered update would be sent. */
2580 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2583 /* Register myself. */
2584 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2589 /* Walk down the RIP routing table then clear changed flag. */
2590 static void rip_clear_changed_flag(struct rip
*rip
)
2592 struct route_node
*rp
;
2593 struct rip_info
*rinfo
= NULL
;
2594 struct list
*list
= NULL
;
2595 struct listnode
*listnode
= NULL
;
2597 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2598 if ((list
= rp
->info
) != NULL
)
2599 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2600 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2601 /* This flag can be set only on the first entry.
2607 /* Triggered update interval timer. */
2608 static int rip_triggered_interval(struct thread
*t
)
2610 struct rip
*rip
= THREAD_ARG(t
);
2612 rip
->t_triggered_interval
= NULL
;
2616 rip_triggered_update(t
);
2621 /* Execute triggered update. */
2622 static int rip_triggered_update(struct thread
*t
)
2624 struct rip
*rip
= THREAD_ARG(t
);
2627 /* Clear thred pointer. */
2628 rip
->t_triggered_update
= NULL
;
2630 /* Cancel interval timer. */
2631 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2634 /* Logging triggered update. */
2635 if (IS_RIP_DEBUG_EVENT
)
2636 zlog_debug("triggered update!");
2638 /* Split Horizon processing is done when generating triggered
2639 updates as well as normal updates (see section 2.6). */
2640 rip_update_process(rip
, rip_changed_route
);
2642 /* Once all of the triggered updates have been generated, the route
2643 change flags should be cleared. */
2644 rip_clear_changed_flag(rip
);
2646 /* After a triggered update is sent, a timer should be set for a
2647 random interval between 1 and 5 seconds. If other changes that
2648 would trigger updates occur before the timer expires, a single
2649 update is triggered when the timer expires. */
2650 interval
= (random() % 5) + 1;
2652 rip
->t_triggered_interval
= NULL
;
2653 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2654 &rip
->t_triggered_interval
);
2659 /* Withdraw redistributed route. */
2660 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2662 struct route_node
*rp
;
2663 struct rip_info
*rinfo
= NULL
;
2664 struct list
*list
= NULL
;
2666 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2667 if ((list
= rp
->info
) != NULL
) {
2668 rinfo
= listgetdata(listhead(list
));
2669 if (rinfo
->type
== type
2670 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2671 /* Perform poisoned reverse. */
2672 rinfo
->metric
= RIP_METRIC_INFINITY
;
2673 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2674 rip_garbage_collect
,
2676 RIP_TIMER_OFF(rinfo
->t_timeout
);
2677 rinfo
->flags
|= RIP_RTF_CHANGED
;
2679 if (IS_RIP_DEBUG_EVENT
) {
2680 struct prefix_ipv4
*p
=
2681 (struct prefix_ipv4
*)&rp
->p
;
2684 "Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2685 inet_ntoa(p
->prefix
),
2692 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2697 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2701 vrf
= vrf_lookup_by_id(vrf_id
);
2708 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2712 rip
.vrf_name
= (char *)vrf_name
;
2714 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2717 /* Create new RIP instance and set it to global variable. */
2718 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2722 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2723 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2725 /* Set initial value. */
2726 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2727 rip
->default_metric
=
2728 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2730 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2731 rip
->passive_default
=
2732 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2733 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2735 rip
->timeout_time
= yang_get_default_uint32(
2736 "%s/timers/holddown-interval", RIP_INSTANCE
);
2737 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2740 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2742 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2744 /* Initialize RIP data structures. */
2745 rip
->table
= route_table_init();
2746 route_table_set_info(rip
->table
, rip
);
2747 rip
->neighbor
= route_table_init();
2748 rip
->peer_list
= list_new();
2749 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2750 rip
->peer_list
->del
= rip_peer_list_del
;
2751 rip
->distance_table
= route_table_init();
2752 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2753 rip
->enable_interface
= vector_init(1);
2754 rip
->enable_network
= route_table_init();
2755 rip
->passive_nondefault
= vector_init(1);
2756 rip
->offset_list_master
= list_new();
2757 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2758 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2760 /* Distribute list install. */
2761 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2762 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2763 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2765 /* if rmap install. */
2766 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2767 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2768 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2770 /* Make output stream. */
2771 rip
->obuf
= stream_new(1500);
2773 /* Enable the routing instance if possible. */
2774 if (vrf
&& vrf_is_enabled(vrf
))
2775 rip_instance_enable(rip
, vrf
, socket
);
2781 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2786 /* Sned RIP request to the destination. */
2787 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2788 uint8_t version
, struct connected
*connected
)
2791 struct rip_packet rip_packet
;
2792 struct listnode
*node
, *nnode
;
2794 memset(&rip_packet
, 0, sizeof(rip_packet
));
2796 rip_packet
.command
= RIP_REQUEST
;
2797 rip_packet
.version
= version
;
2798 rte
= rip_packet
.rte
;
2799 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2803 * connected is only sent for ripv1 case, or when
2804 * interface does not support multicast. Caller loops
2805 * over each connected address for this case.
2807 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2809 != sizeof(rip_packet
))
2812 return sizeof(rip_packet
);
2815 /* send request on each connected network */
2816 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2817 struct prefix_ipv4
*p
;
2819 p
= (struct prefix_ipv4
*)connected
->address
;
2821 if (p
->family
!= AF_INET
)
2824 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2826 != sizeof(rip_packet
))
2829 return sizeof(rip_packet
);
2832 static int rip_update_jitter(unsigned long time
)
2834 #define JITTER_BOUND 4
2835 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2836 Given that, we cannot let time be less than JITTER_BOUND seconds.
2837 The RIPv2 RFC says jitter should be small compared to
2838 update_time. We consider 1/JITTER_BOUND to be small.
2841 int jitter_input
= time
;
2844 if (jitter_input
< JITTER_BOUND
)
2845 jitter_input
= JITTER_BOUND
;
2847 jitter
= (((random() % ((jitter_input
* 2) + 1)) - jitter_input
));
2849 return jitter
/ JITTER_BOUND
;
2852 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2859 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2861 case RIP_UPDATE_EVENT
:
2862 RIP_TIMER_OFF(rip
->t_update
);
2863 jitter
= rip_update_jitter(rip
->update_time
);
2864 thread_add_timer(master
, rip_update
, rip
,
2865 sock
? 2 : rip
->update_time
+ jitter
,
2868 case RIP_TRIGGERED_UPDATE
:
2869 if (rip
->t_triggered_interval
)
2872 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2873 &rip
->t_triggered_update
);
2882 rip_update_default_metric (void)
2884 struct route_node
*np
;
2885 struct rip_info
*rinfo
= NULL
;
2886 struct list
*list
= NULL
;
2887 struct listnode
*listnode
= NULL
;
2889 for (np
= route_top (rip
->table
); np
; np
= route_next (np
))
2890 if ((list
= np
->info
) != NULL
)
2891 for (ALL_LIST_ELEMENTS_RO (list
, listnode
, rinfo
))
2892 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&& rinfo
->type
!= ZEBRA_ROUTE_CONNECT
)
2893 rinfo
->metric
= rip
->default_metric
;
2897 struct rip_distance
*rip_distance_new(void)
2899 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2902 void rip_distance_free(struct rip_distance
*rdistance
)
2904 if (rdistance
->access_list
)
2905 free(rdistance
->access_list
);
2906 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2909 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2910 struct route_node
*node
)
2912 struct rip_distance
*rdistance
;
2914 rdistance
= node
->info
;
2916 rip_distance_free(rdistance
);
2919 /* Apply RIP information to distance method. */
2920 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2922 struct route_node
*rn
;
2923 struct prefix_ipv4 p
;
2924 struct rip_distance
*rdistance
;
2925 struct access_list
*alist
;
2927 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2929 p
.prefix
= rinfo
->from
;
2930 p
.prefixlen
= IPV4_MAX_BITLEN
;
2932 /* Check source address. */
2933 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2935 rdistance
= rn
->info
;
2936 route_unlock_node(rn
);
2938 if (rdistance
->access_list
) {
2939 alist
= access_list_lookup(AFI_IP
,
2940 rdistance
->access_list
);
2943 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2947 return rdistance
->distance
;
2949 return rdistance
->distance
;
2953 return rip
->distance
;
2958 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2960 struct route_node
*rn
;
2961 struct rip_distance
*rdistance
;
2965 vty_out(vty
, " Distance: (default is %u)\n",
2966 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2968 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2969 if ((rdistance
= rn
->info
) != NULL
) {
2972 " Address Distance List\n");
2975 sprintf(buf
, "%s/%d", inet_ntoa(rn
->p
.u
.prefix4
),
2977 vty_out(vty
, " %-20s %4d %s\n", buf
,
2978 rdistance
->distance
,
2979 rdistance
->access_list
? rdistance
->access_list
2984 /* Update ECMP routes to zebra when ECMP is disabled. */
2985 void rip_ecmp_disable(struct rip
*rip
)
2987 struct route_node
*rp
;
2988 struct rip_info
*rinfo
, *tmp_rinfo
;
2990 struct listnode
*node
, *nextnode
;
2992 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2993 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2994 rinfo
= listgetdata(listhead(list
));
2995 if (!rip_route_rte(rinfo
))
2998 /* Drop all other entries, except the first one. */
2999 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
3000 if (tmp_rinfo
!= rinfo
) {
3001 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
3003 tmp_rinfo
->t_garbage_collect
);
3004 list_delete_node(list
, node
);
3005 rip_info_free(tmp_rinfo
);
3009 rip_zebra_ipv4_add(rip
, rp
);
3011 /* Set the route change flag. */
3012 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
3014 /* Signal the output process to trigger an update. */
3015 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
3019 /* Print out routes update time. */
3020 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
3025 char timebuf
[TIME_BUF
];
3026 struct thread
*thread
;
3028 if ((thread
= rinfo
->t_timeout
) != NULL
) {
3029 clock
= thread_timer_remain_second(thread
);
3030 tm
= gmtime(&clock
);
3031 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3032 vty_out(vty
, "%5s", timebuf
);
3033 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
3034 clock
= thread_timer_remain_second(thread
);
3035 tm
= gmtime(&clock
);
3036 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3037 vty_out(vty
, "%5s", timebuf
);
3041 static const char *rip_route_type_print(int sub_type
)
3046 case RIP_ROUTE_STATIC
:
3048 case RIP_ROUTE_DEFAULT
:
3050 case RIP_ROUTE_REDISTRIBUTE
:
3052 case RIP_ROUTE_INTERFACE
:
3061 "show ip rip [vrf NAME]",
3068 struct route_node
*np
;
3069 struct rip_info
*rinfo
= NULL
;
3070 struct list
*list
= NULL
;
3071 struct listnode
*listnode
= NULL
;
3072 const char *vrf_name
;
3075 if (argv_find(argv
, argc
, "vrf", &idx
))
3076 vrf_name
= argv
[idx
+ 1]->arg
;
3078 vrf_name
= VRF_DEFAULT_NAME
;
3080 rip
= rip_lookup_by_vrf_name(vrf_name
);
3082 vty_out(vty
, "%% RIP instance not found\n");
3085 if (!rip
->enabled
) {
3086 vty_out(vty
, "%% RIP instance is disabled\n");
3091 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3093 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3094 " (i) - interface\n\n"
3095 " Network Next Hop Metric From Tag Time\n");
3097 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3098 if ((list
= np
->info
) != NULL
)
3099 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3103 vty
, "%c(%s) %s/%d",
3104 /* np->lock, For debugging. */
3105 zebra_route_char(rinfo
->type
),
3106 rip_route_type_print(rinfo
->sub_type
),
3107 inet_ntoa(np
->p
.u
.prefix4
),
3113 vty_out(vty
, "%*s", len
, " ");
3115 switch (rinfo
->nh
.type
) {
3116 case NEXTHOP_TYPE_IPV4
:
3117 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3118 vty_out(vty
, "%-20s %2d ",
3119 inet_ntoa(rinfo
->nh
.gate
.ipv4
),
3122 case NEXTHOP_TYPE_IFINDEX
:
3127 case NEXTHOP_TYPE_BLACKHOLE
:
3132 case NEXTHOP_TYPE_IPV6
:
3133 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3135 "V6 Address Hidden %2d ",
3140 /* Route which exist in kernel routing table. */
3141 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3142 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3143 vty_out(vty
, "%-15s ",
3144 inet_ntoa(rinfo
->from
));
3145 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3146 (route_tag_t
)rinfo
->tag
);
3147 rip_vty_out_uptime(vty
, rinfo
);
3148 } else if (rinfo
->metric
3149 == RIP_METRIC_INFINITY
) {
3150 vty_out(vty
, "self ");
3151 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3152 (route_tag_t
)rinfo
->tag
);
3153 rip_vty_out_uptime(vty
, rinfo
);
3155 if (rinfo
->external_metric
) {
3157 vty
, "self (%s:%d)",
3160 rinfo
->external_metric
);
3163 vty_out(vty
, "%*s", len
,
3168 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3169 (route_tag_t
)rinfo
->tag
);
3177 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3178 DEFUN (show_ip_rip_status
,
3179 show_ip_rip_status_cmd
,
3180 "show ip rip [vrf NAME] status",
3185 "IP routing protocol process parameters and statistics\n")
3188 struct interface
*ifp
;
3189 struct rip_interface
*ri
;
3190 extern const struct message ri_version_msg
[];
3191 const char *send_version
;
3192 const char *receive_version
;
3193 const char *vrf_name
;
3196 if (argv_find(argv
, argc
, "vrf", &idx
))
3197 vrf_name
= argv
[idx
+ 1]->arg
;
3199 vrf_name
= VRF_DEFAULT_NAME
;
3201 rip
= rip_lookup_by_vrf_name(vrf_name
);
3203 vty_out(vty
, "%% RIP instance not found\n");
3206 if (!rip
->enabled
) {
3207 vty_out(vty
, "%% RIP instance is disabled\n");
3211 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3212 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3214 vty_out(vty
, " next due in %lu seconds\n",
3215 thread_timer_remain_second(rip
->t_update
));
3216 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3217 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3219 /* Filtering status show. */
3220 config_show_distribute(vty
, rip
->distribute_ctx
);
3222 /* Default metric information. */
3223 vty_out(vty
, " Default redistribution metric is %u\n",
3224 rip
->default_metric
);
3226 /* Redistribute information. */
3227 vty_out(vty
, " Redistributing:");
3228 rip_show_redistribute_config(vty
, rip
);
3231 vty_out(vty
, " Default version control: send version %s,",
3232 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3233 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3234 vty_out(vty
, " receive any version \n");
3236 vty_out(vty
, " receive version %s \n",
3237 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3239 vty_out(vty
, " Interface Send Recv Key-chain\n");
3241 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3247 if (ri
->enable_network
|| ri
->enable_interface
) {
3248 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3250 lookup_msg(ri_version_msg
,
3251 rip
->version_send
, NULL
);
3253 send_version
= lookup_msg(ri_version_msg
,
3256 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3258 lookup_msg(ri_version_msg
,
3259 rip
->version_recv
, NULL
);
3261 receive_version
= lookup_msg(
3262 ri_version_msg
, ri
->ri_receive
, NULL
);
3264 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3265 send_version
, receive_version
,
3266 ri
->key_chain
? ri
->key_chain
: "");
3270 vty_out(vty
, " Routing for Networks:\n");
3271 rip_show_network_config(vty
, rip
);
3274 int found_passive
= 0;
3275 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3278 if ((ri
->enable_network
|| ri
->enable_interface
)
3280 if (!found_passive
) {
3282 " Passive Interface(s):\n");
3285 vty_out(vty
, " %s\n", ifp
->name
);
3290 vty_out(vty
, " Routing Information Sources:\n");
3292 " Gateway BadPackets BadRoutes Distance Last Update\n");
3293 rip_peer_display(vty
, rip
);
3295 rip_distance_show(vty
, rip
);
3300 /* RIP configuration write function. */
3301 static int config_write_rip(struct vty
*vty
)
3306 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3307 char xpath
[XPATH_MAXLEN
];
3308 struct lyd_node
*dnode
;
3310 snprintf(xpath
, sizeof(xpath
),
3311 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3313 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3316 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3318 /* Distribute configuration. */
3319 config_write_distribute(vty
, rip
->distribute_ctx
);
3321 /* Interface routemap configuration */
3322 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3330 /* RIP node structure. */
3331 static struct cmd_node rip_node
= {RIP_NODE
, "%s(config-router)# ", 1};
3333 /* Distribute-list update functions. */
3334 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3335 struct distribute
*dist
)
3337 struct interface
*ifp
;
3338 struct rip_interface
*ri
;
3339 struct access_list
*alist
;
3340 struct prefix_list
*plist
;
3342 if (!ctx
->vrf
|| !dist
->ifname
)
3345 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3351 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3352 alist
= access_list_lookup(AFI_IP
,
3353 dist
->list
[DISTRIBUTE_V4_IN
]);
3355 ri
->list
[RIP_FILTER_IN
] = alist
;
3357 ri
->list
[RIP_FILTER_IN
] = NULL
;
3359 ri
->list
[RIP_FILTER_IN
] = NULL
;
3361 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3362 alist
= access_list_lookup(AFI_IP
,
3363 dist
->list
[DISTRIBUTE_V4_OUT
]);
3365 ri
->list
[RIP_FILTER_OUT
] = alist
;
3367 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3369 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3371 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3372 plist
= prefix_list_lookup(AFI_IP
,
3373 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3375 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3377 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3379 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3381 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3382 plist
= prefix_list_lookup(AFI_IP
,
3383 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3385 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3387 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3389 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3392 void rip_distribute_update_interface(struct interface
*ifp
)
3394 struct rip_interface
*ri
= ifp
->info
;
3395 struct rip
*rip
= ri
->rip
;
3396 struct distribute
*dist
;
3400 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3402 rip_distribute_update(rip
->distribute_ctx
, dist
);
3405 /* Update all interface's distribute list. */
3407 static void rip_distribute_update_all(struct prefix_list
*notused
)
3409 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3410 struct interface
*ifp
;
3412 FOR_ALL_INTERFACES (vrf
, ifp
)
3413 rip_distribute_update_interface(ifp
);
3416 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3418 rip_distribute_update_all(NULL
);
3421 /* Delete all added rip route. */
3422 void rip_clean(struct rip
*rip
)
3425 rip_instance_disable(rip
);
3427 stream_free(rip
->obuf
);
3429 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3430 if (rip
->redist
[i
].route_map
.name
)
3431 free(rip
->redist
[i
].route_map
.name
);
3433 route_table_finish(rip
->table
);
3434 route_table_finish(rip
->neighbor
);
3435 list_delete(&rip
->peer_list
);
3436 distribute_list_delete(&rip
->distribute_ctx
);
3437 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3439 rip_clean_network(rip
);
3440 rip_passive_nondefault_clean(rip
);
3441 vector_free(rip
->enable_interface
);
3442 route_table_finish(rip
->enable_network
);
3443 vector_free(rip
->passive_nondefault
);
3444 list_delete(&rip
->offset_list_master
);
3445 rip_interfaces_clean(rip
);
3446 route_table_finish(rip
->distance_table
);
3448 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3449 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3450 XFREE(MTYPE_RIP
, rip
);
3453 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3454 struct if_rmap
*if_rmap
)
3456 struct interface
*ifp
= NULL
;
3457 struct rip_interface
*ri
;
3458 struct route_map
*rmap
;
3459 struct vrf
*vrf
= NULL
;
3462 vrf
= vrf_lookup_by_name(ctx
->name
);
3464 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3469 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3470 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3472 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3474 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3476 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3478 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3479 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3481 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3483 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3485 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3488 void rip_if_rmap_update_interface(struct interface
*ifp
)
3490 struct rip_interface
*ri
= ifp
->info
;
3491 struct rip
*rip
= ri
->rip
;
3492 struct if_rmap
*if_rmap
;
3493 struct if_rmap_ctx
*ctx
;
3497 ctx
= rip
->if_rmap_ctx
;
3500 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3502 rip_if_rmap_update(ctx
, if_rmap
);
3505 static void rip_routemap_update_redistribute(struct rip
*rip
)
3507 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3508 if (rip
->redist
[i
].route_map
.name
) {
3509 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3510 rip
->redist
[i
].route_map
.name
);
3511 route_map_counter_increment(
3512 rip
->redist
[i
].route_map
.map
);
3518 static void rip_routemap_update(const char *notused
)
3520 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3522 struct interface
*ifp
;
3524 FOR_ALL_INTERFACES (vrf
, ifp
)
3525 rip_if_rmap_update_interface(ifp
);
3529 rip_routemap_update_redistribute(rip
);
3532 /* Link RIP instance to VRF. */
3533 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3535 struct interface
*ifp
;
3538 rip
->distribute_ctx
->vrf
= vrf
;
3541 FOR_ALL_INTERFACES (vrf
, ifp
)
3542 rip_interface_sync(ifp
);
3545 /* Unlink RIP instance from VRF. */
3546 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3548 struct interface
*ifp
;
3551 rip
->distribute_ctx
->vrf
= NULL
;
3554 FOR_ALL_INTERFACES (vrf
, ifp
)
3555 rip_interface_sync(ifp
);
3558 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3562 rip_vrf_link(rip
, vrf
);
3563 rip
->enabled
= true;
3565 /* Resend all redistribute requests. */
3566 rip_redistribute_enable(rip
);
3568 /* Create read and timer thread. */
3569 rip_event(rip
, RIP_READ
, rip
->sock
);
3570 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3572 rip_zebra_vrf_register(vrf
);
3575 static void rip_instance_disable(struct rip
*rip
)
3577 struct vrf
*vrf
= rip
->vrf
;
3578 struct route_node
*rp
;
3580 /* Clear RIP routes */
3581 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3582 struct rip_info
*rinfo
;
3584 struct listnode
*listnode
;
3586 if ((list
= rp
->info
) == NULL
)
3589 rinfo
= listgetdata(listhead(list
));
3590 if (rip_route_rte(rinfo
))
3591 rip_zebra_ipv4_delete(rip
, rp
);
3593 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3594 RIP_TIMER_OFF(rinfo
->t_timeout
);
3595 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3596 rip_info_free(rinfo
);
3600 route_unlock_node(rp
);
3603 /* Flush all redistribute requests. */
3604 rip_redistribute_disable(rip
);
3606 /* Cancel RIP related timers. */
3607 RIP_TIMER_OFF(rip
->t_update
);
3608 RIP_TIMER_OFF(rip
->t_triggered_update
);
3609 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3611 /* Cancel read thread. */
3612 THREAD_READ_OFF(rip
->t_read
);
3614 /* Close RIP socket. */
3618 /* Clear existing peers. */
3619 list_delete_all_node(rip
->peer_list
);
3621 rip_zebra_vrf_deregister(vrf
);
3623 rip_vrf_unlink(rip
, vrf
);
3624 rip
->enabled
= false;
3627 static int rip_vrf_new(struct vrf
*vrf
)
3629 if (IS_RIP_DEBUG_EVENT
)
3630 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3636 static int rip_vrf_delete(struct vrf
*vrf
)
3638 if (IS_RIP_DEBUG_EVENT
)
3639 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3645 static int rip_vrf_enable(struct vrf
*vrf
)
3650 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3652 char *old_vrf_name
= NULL
;
3654 rip
= (struct rip
*)vrf
->info
;
3657 /* update vrf name */
3659 old_vrf_name
= rip
->vrf_name
;
3660 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf
->name
);
3662 * HACK: Change the RIP VRF in the running configuration directly,
3663 * bypassing the northbound layer. This is necessary to avoid deleting
3664 * the RIP and readding it in the new VRF, which would have
3665 * several implications.
3667 if (yang_module_find("frr-ripd") && old_vrf_name
) {
3668 struct lyd_node
*rip_dnode
;
3670 rip_dnode
= yang_dnode_get(
3671 running_config
->dnode
,
3672 "/frr-ripd:ripd/instance[vrf='%s']/vrf",
3675 yang_dnode_change_leaf(rip_dnode
, vrf
->name
);
3676 running_config
->version
++;
3679 XFREE(MTYPE_RIP_VRF_NAME
, old_vrf_name
);
3681 if (!rip
|| rip
->enabled
)
3684 if (IS_RIP_DEBUG_EVENT
)
3685 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3688 /* Activate the VRF RIP instance. */
3689 if (!rip
->enabled
) {
3690 socket
= rip_create_socket(vrf
);
3694 rip_instance_enable(rip
, vrf
, socket
);
3700 static int rip_vrf_disable(struct vrf
*vrf
)
3704 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3705 if (!rip
|| !rip
->enabled
)
3708 if (IS_RIP_DEBUG_EVENT
)
3709 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3712 /* Deactivate the VRF RIP instance. */
3714 rip_instance_disable(rip
);
3719 void rip_vrf_init(void)
3721 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3725 void rip_vrf_terminate(void)
3730 /* Allocate new rip structure and set default value. */
3733 /* Install top nodes. */
3734 install_node(&rip_node
, config_write_rip
);
3736 /* Install rip commands. */
3737 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3738 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3740 install_default(RIP_NODE
);
3742 /* Debug related init. */
3745 /* Access list install. */
3747 access_list_add_hook(rip_distribute_update_all_wrapper
);
3748 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3750 /* Prefix list initialize.*/
3752 prefix_list_add_hook(rip_distribute_update_all
);
3753 prefix_list_delete_hook(rip_distribute_update_all
);
3755 /* Distribute list install. */
3756 distribute_list_init(RIP_NODE
);
3759 rip_route_map_init();
3761 route_map_add_hook(rip_routemap_update
);
3762 route_map_delete_hook(rip_routemap_update
);
3764 if_rmap_init(RIP_NODE
);