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_debug.h"
50 #include "ripd/rip_errors.h"
51 #include "ripd/rip_interface.h"
53 /* UDP receive buffer size */
54 #define RIP_UDP_RCV_BUF 41600
56 DEFINE_MGROUP(RIPD
, "ripd")
57 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure")
58 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name")
59 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info")
60 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance")
63 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
65 static int rip_triggered_update(struct thread
*);
66 static int rip_update_jitter(unsigned long);
67 static void rip_distance_table_node_cleanup(struct route_table
*table
,
68 struct route_node
*node
);
69 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
70 static void rip_instance_disable(struct rip
*rip
);
72 static void rip_distribute_update(struct distribute_ctx
*ctx
,
73 struct distribute
*dist
);
75 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
76 struct if_rmap
*if_rmap
);
78 /* RIP output routes type. */
79 enum { rip_all_route
, rip_changed_route
};
81 /* RIP command strings. */
82 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
83 {RIP_RESPONSE
, "RESPONSE"},
84 {RIP_TRACEON
, "TRACEON"},
85 {RIP_TRACEOFF
, "TRACEOFF"},
87 {RIP_POLL_ENTRY
, "POLL ENTRY"},
90 /* Generate rb-tree of RIP instances. */
91 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
93 return strcmp(a
->vrf_name
, b
->vrf_name
);
95 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
97 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
99 /* Utility function to set boradcast option to the socket. */
100 static int sockopt_broadcast(int sock
)
105 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
108 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
114 int rip_route_rte(struct rip_info
*rinfo
)
116 return (rinfo
->type
== ZEBRA_ROUTE_RIP
117 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
120 static struct rip_info
*rip_info_new(void)
122 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
125 void rip_info_free(struct rip_info
*rinfo
)
127 XFREE(MTYPE_RIP_INFO
, rinfo
);
130 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
132 return route_table_get_info(rinfo
->rp
->table
);
135 /* RIP route garbage collect timer. */
136 static int rip_garbage_collect(struct thread
*t
)
138 struct rip_info
*rinfo
;
139 struct route_node
*rp
;
141 rinfo
= THREAD_ARG(t
);
142 rinfo
->t_garbage_collect
= NULL
;
144 /* Off timeout timer. */
145 RIP_TIMER_OFF(rinfo
->t_timeout
);
147 /* Get route_node pointer. */
150 /* Unlock route_node. */
151 listnode_delete(rp
->info
, rinfo
);
152 if (list_isempty((struct list
*)rp
->info
)) {
153 list_delete((struct list
**)&rp
->info
);
154 route_unlock_node(rp
);
157 /* Free RIP routing information. */
158 rip_info_free(rinfo
);
163 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
165 /* Add new route to the ECMP list.
166 * RETURN: the new entry added in the list, or NULL if it is not the first
167 * entry and ECMP is not allowed.
169 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
171 struct route_node
*rp
= rinfo_new
->rp
;
172 struct rip_info
*rinfo
= NULL
;
173 struct list
*list
= NULL
;
175 if (rp
->info
== NULL
)
176 rp
->info
= list_new();
177 list
= (struct list
*)rp
->info
;
179 /* If ECMP is not allowed and some entry already exists in the list,
181 if (listcount(list
) && !rip
->ecmp
)
184 rinfo
= rip_info_new();
185 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
186 listnode_add(list
, rinfo
);
188 if (rip_route_rte(rinfo
)) {
189 rip_timeout_update(rip
, rinfo
);
190 rip_zebra_ipv4_add(rip
, rp
);
193 /* Set the route change flag on the first entry. */
194 rinfo
= listgetdata(listhead(list
));
195 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
197 /* Signal the output process to trigger an update (see section 2.5). */
198 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
203 /* Replace the ECMP list with the new route.
204 * RETURN: the new entry added in the list
206 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
208 struct route_node
*rp
= rinfo_new
->rp
;
209 struct list
*list
= (struct list
*)rp
->info
;
210 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
211 struct listnode
*node
= NULL
, *nextnode
= NULL
;
213 if (list
== NULL
|| listcount(list
) == 0)
214 return rip_ecmp_add(rip
, rinfo_new
);
216 /* Get the first entry */
217 rinfo
= listgetdata(listhead(list
));
219 /* Learnt route replaced by a local one. Delete it from zebra. */
220 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
221 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
222 rip_zebra_ipv4_delete(rip
, rp
);
224 /* Re-use the first entry, and delete the others. */
225 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
226 if (tmp_rinfo
!= rinfo
) {
227 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
228 RIP_TIMER_OFF(tmp_rinfo
->t_garbage_collect
);
229 list_delete_node(list
, node
);
230 rip_info_free(tmp_rinfo
);
233 RIP_TIMER_OFF(rinfo
->t_timeout
);
234 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
235 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
237 if (rip_route_rte(rinfo
)) {
238 rip_timeout_update(rip
, rinfo
);
239 /* The ADD message implies an update. */
240 rip_zebra_ipv4_add(rip
, rp
);
243 /* Set the route change flag. */
244 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
246 /* Signal the output process to trigger an update (see section 2.5). */
247 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
252 /* Delete one route from the ECMP list.
254 * null - the entry is freed, and other entries exist in the list
255 * the entry - the entry is the last one in the list; its metric is set
256 * to INFINITY, and the garbage collector is started for it
258 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
260 struct route_node
*rp
= rinfo
->rp
;
261 struct list
*list
= (struct list
*)rp
->info
;
263 RIP_TIMER_OFF(rinfo
->t_timeout
);
265 if (listcount(list
) > 1) {
266 /* Some other ECMP entries still exist. Just delete this entry.
268 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
269 listnode_delete(list
, rinfo
);
270 if (rip_route_rte(rinfo
)
271 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
272 /* The ADD message implies the update. */
273 rip_zebra_ipv4_add(rip
, rp
);
274 rip_info_free(rinfo
);
277 assert(rinfo
== listgetdata(listhead(list
)));
279 /* This is the only entry left in the list. We must keep it in
280 * the list for garbage collection time, with INFINITY metric.
283 rinfo
->metric
= RIP_METRIC_INFINITY
;
284 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
287 if (rip_route_rte(rinfo
)
288 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
289 rip_zebra_ipv4_delete(rip
, rp
);
292 /* Set the route change flag on the first entry. */
293 rinfo
= listgetdata(listhead(list
));
294 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
296 /* Signal the output process to trigger an update (see section 2.5). */
297 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
302 /* Timeout RIP routes. */
303 static int rip_timeout(struct thread
*t
)
305 struct rip_info
*rinfo
= THREAD_ARG(t
);
306 struct rip
*rip
= rip_info_get_instance(rinfo
);
308 rip_ecmp_delete(rip
, rinfo
);
313 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
315 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
316 RIP_TIMER_OFF(rinfo
->t_timeout
);
317 thread_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
322 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
323 struct rip_interface
*ri
)
325 struct distribute
*dist
;
326 struct access_list
*alist
;
327 struct prefix_list
*plist
;
328 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
330 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
332 /* Input distribute-list filtering. */
333 if (ri
->list
[rip_distribute
]) {
334 if (access_list_apply(ri
->list
[rip_distribute
],
337 if (IS_RIP_DEBUG_PACKET
)
338 zlog_debug("%s/%d filtered by distribute %s",
339 inet_ntoa(p
->prefix
), p
->prefixlen
,
344 if (ri
->prefix
[rip_distribute
]) {
345 if (prefix_list_apply(ri
->prefix
[rip_distribute
],
348 if (IS_RIP_DEBUG_PACKET
)
349 zlog_debug("%s/%d filtered by prefix-list %s",
350 inet_ntoa(p
->prefix
), p
->prefixlen
,
356 /* All interface filter check. */
357 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
359 if (dist
->list
[distribute
]) {
360 alist
= access_list_lookup(AFI_IP
,
361 dist
->list
[distribute
]);
364 if (access_list_apply(alist
, (struct prefix
*)p
)
366 if (IS_RIP_DEBUG_PACKET
)
368 "%s/%d filtered by distribute %s",
369 inet_ntoa(p
->prefix
),
370 p
->prefixlen
, inout
);
375 if (dist
->prefix
[distribute
]) {
376 plist
= prefix_list_lookup(AFI_IP
,
377 dist
->prefix
[distribute
]);
380 if (prefix_list_apply(plist
, (struct prefix
*)p
)
382 if (IS_RIP_DEBUG_PACKET
)
384 "%s/%d filtered by prefix-list %s",
385 inet_ntoa(p
->prefix
),
386 p
->prefixlen
, inout
);
395 /* Check nexthop address validity. */
396 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
398 struct interface
*ifp
;
399 struct listnode
*cnode
;
400 struct connected
*ifc
;
403 /* If nexthop address matches local configured address then it is
406 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
407 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
410 if (p
->family
== AF_INET
411 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
418 /* RIP add route to routing table. */
419 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
420 struct interface
*ifp
)
424 struct prefix_ipv4 p
;
425 struct route_node
*rp
;
426 struct rip_info
*rinfo
= NULL
, newinfo
;
427 struct rip_interface
*ri
;
428 struct in_addr
*nexthop
;
430 unsigned char old_dist
, new_dist
;
431 struct list
*list
= NULL
;
432 struct listnode
*node
= NULL
;
434 /* Make prefix structure. */
435 memset(&p
, 0, sizeof(struct prefix_ipv4
));
437 p
.prefix
= rte
->prefix
;
438 p
.prefixlen
= ip_masklen(rte
->mask
);
440 /* Make sure mask is applied. */
446 /* Apply input filters. */
447 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
451 memset(&newinfo
, 0, sizeof(newinfo
));
452 newinfo
.type
= ZEBRA_ROUTE_RIP
;
453 newinfo
.sub_type
= RIP_ROUTE_RTE
;
454 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
455 newinfo
.from
= from
->sin_addr
;
456 newinfo
.nh
.ifindex
= ifp
->ifindex
;
457 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
458 newinfo
.metric
= rte
->metric
;
459 newinfo
.metric_out
= rte
->metric
; /* XXX */
460 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
462 /* Modify entry according to the interface routemap. */
463 if (ri
->routemap
[RIP_FILTER_IN
]) {
464 /* The object should be of the type of rip_info */
465 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
466 (struct prefix
*)&p
, RMAP_RIP
, &newinfo
);
468 if (ret
== RMAP_DENYMATCH
) {
469 if (IS_RIP_DEBUG_PACKET
)
471 "RIP %s/%d is filtered by route-map in",
472 inet_ntoa(p
.prefix
), p
.prefixlen
);
476 /* Get back the object */
477 rte
->nexthop
= newinfo
.nexthop_out
;
478 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
479 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
483 /* Once the entry has been validated, update the metric by
484 adding the cost of the network on wich the message
485 arrived. If the result is greater than infinity, use infinity
486 (RFC2453 Sec. 3.9.2) */
487 /* Zebra ripd can handle offset-list in. */
488 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
490 /* If offset-list does not modify the metric use interface's
493 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
495 if (rte
->metric
> RIP_METRIC_INFINITY
)
496 rte
->metric
= RIP_METRIC_INFINITY
;
498 /* Set nexthop pointer. */
499 if (rte
->nexthop
.s_addr
== 0)
500 nexthop
= &from
->sin_addr
;
502 nexthop
= &rte
->nexthop
;
504 /* Check if nexthop address is myself, then do nothing. */
505 if (rip_nexthop_check(rip
, nexthop
) < 0) {
506 if (IS_RIP_DEBUG_PACKET
)
507 zlog_debug("Nexthop address %s is myself",
508 inet_ntoa(*nexthop
));
512 /* Get index for the prefix. */
513 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
516 newinfo
.nh
.gate
.ipv4
= *nexthop
;
517 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
518 newinfo
.metric
= rte
->metric
;
519 newinfo
.tag
= ntohs(rte
->tag
);
520 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
522 new_dist
= newinfo
.distance
? newinfo
.distance
523 : ZEBRA_RIP_DISTANCE_DEFAULT
;
525 /* Check to see whether there is already RIP route on the table. */
526 if ((list
= rp
->info
) != NULL
)
527 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
528 /* Need to compare with redistributed entry or local
530 if (!rip_route_rte(rinfo
))
533 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
534 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
537 if (!listnextnode(node
)) {
538 /* Not found in the list */
540 if (rte
->metric
> rinfo
->metric
) {
541 /* New route has a greater metric.
543 route_unlock_node(rp
);
547 if (rte
->metric
< rinfo
->metric
)
548 /* New route has a smaller metric.
549 * Replace the ECMP list
550 * with the new one in below. */
553 /* Metrics are same. We compare the distances.
555 old_dist
= rinfo
->distance
557 : ZEBRA_RIP_DISTANCE_DEFAULT
;
559 if (new_dist
> old_dist
) {
560 /* New route has a greater distance.
562 route_unlock_node(rp
);
566 if (new_dist
< old_dist
)
567 /* New route has a smaller distance.
568 * Replace the ECMP list
569 * with the new one in below. */
572 /* Metrics and distances are both same. Keep
574 * the new route is added in the ECMP list in
580 /* Local static route. */
581 if (rinfo
->type
== ZEBRA_ROUTE_RIP
582 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
583 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
584 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
585 route_unlock_node(rp
);
589 /* Redistributed route check. */
590 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
591 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
592 old_dist
= rinfo
->distance
;
593 /* Only routes directly connected to an interface
595 * may have a valid NULL distance */
596 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= 0)
599 : ZEBRA_RIP_DISTANCE_DEFAULT
;
600 /* If imported route does not have STRICT precedence,
601 mark it as a ghost */
602 if (new_dist
<= old_dist
603 && rte
->metric
!= RIP_METRIC_INFINITY
)
604 rip_ecmp_replace(rip
, &newinfo
);
606 route_unlock_node(rp
);
613 route_unlock_node(rp
);
615 /* Now, check to see whether there is already an explicit route
616 for the destination prefix. If there is no such route, add
617 this route to the routing table, unless the metric is
618 infinity (there is no point in adding a route which
620 if (rte
->metric
!= RIP_METRIC_INFINITY
)
621 rip_ecmp_add(rip
, &newinfo
);
623 /* Route is there but we are not sure the route is RIP or not.
626 /* If there is an existing route, compare the next hop address
627 to the address of the router from which the datagram came.
628 If this datagram is from the same router as the existing
629 route, reinitialize the timeout. */
630 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
631 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
633 old_dist
= rinfo
->distance
? rinfo
->distance
634 : ZEBRA_RIP_DISTANCE_DEFAULT
;
636 /* Next, compare the metrics. If the datagram is from the same
637 router as the existing route, and the new metric is different
638 than the old one; or, if the new metric is lower than the old
639 one, or if the tag has been changed; or if there is a route
640 with a lower administrave distance; or an update of the
641 distance on the actual route; do the following actions: */
642 if ((same
&& rinfo
->metric
!= rte
->metric
)
643 || (rte
->metric
< rinfo
->metric
)
644 || ((same
) && (rinfo
->metric
== rte
->metric
)
645 && (newinfo
.tag
!= rinfo
->tag
))
646 || (old_dist
> new_dist
)
647 || ((old_dist
!= new_dist
) && same
)) {
648 if (listcount(list
) == 1) {
649 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
650 rip_ecmp_replace(rip
, &newinfo
);
652 rip_ecmp_delete(rip
, rinfo
);
654 if (newinfo
.metric
< rinfo
->metric
)
655 rip_ecmp_replace(rip
, &newinfo
);
656 else if (newinfo
.metric
> rinfo
->metric
)
657 rip_ecmp_delete(rip
, rinfo
);
658 else if (new_dist
< old_dist
)
659 rip_ecmp_replace(rip
, &newinfo
);
660 else if (new_dist
> old_dist
)
661 rip_ecmp_delete(rip
, rinfo
);
663 int update
= CHECK_FLAG(rinfo
->flags
,
668 assert(newinfo
.metric
669 != RIP_METRIC_INFINITY
);
671 RIP_TIMER_OFF(rinfo
->t_timeout
);
672 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
673 memcpy(rinfo
, &newinfo
,
674 sizeof(struct rip_info
));
675 rip_timeout_update(rip
, rinfo
);
678 rip_zebra_ipv4_add(rip
, rp
);
680 /* - Set the route change flag on the
682 rinfo
= listgetdata(listhead(list
));
683 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
684 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
687 } else /* same & no change */
688 rip_timeout_update(rip
, rinfo
);
690 /* Unlock tempolary lock of the route. */
691 route_unlock_node(rp
);
695 /* Dump RIP packet */
696 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
701 const char *command_str
;
702 char pbuf
[BUFSIZ
], nbuf
[BUFSIZ
];
706 /* Set command string. */
707 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
708 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
710 command_str
= "unknown";
712 /* Dump packet header. */
713 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
714 packet
->version
, size
);
716 /* Dump each routing table entry. */
719 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
720 if (packet
->version
== RIPv2
) {
721 netmask
= ip_masklen(rte
->mask
);
723 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
725 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
726 p
= (uint8_t *)&rte
->prefix
;
729 " family 0x%X type %d auth string: %s",
732 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
733 struct rip_md5_info
*md5
;
735 md5
= (struct rip_md5_info
*)&packet
739 " family 0x%X type %d (MD5 authentication)",
743 " RIP-2 packet len %d Key ID %d"
745 ntohs(md5
->packet_len
),
746 md5
->keyid
, md5
->auth_len
);
747 zlog_debug(" Sequence Number %ld",
748 (unsigned long)ntohl(
750 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
751 p
= (uint8_t *)&rte
->prefix
;
754 " family 0x%X type %d (MD5 data)",
758 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
759 "%02X%02X%02X%02X%02X%02X%02X%02X",
760 p
[0], p
[1], p
[2], p
[3], p
[4],
761 p
[5], p
[6], p
[7], p
[8], p
[9],
762 p
[10], p
[11], p
[12], p
[13],
766 " family 0x%X type %d (Unknown auth type)",
772 " %s/%d -> %s family %d tag %" ROUTE_TAG_PRI
774 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
,
777 inet_ntop(AF_INET
, &rte
->nexthop
, nbuf
,
780 (route_tag_t
)ntohs(rte
->tag
),
781 (unsigned long)ntohl(rte
->metric
));
784 " %s family %d tag %" ROUTE_TAG_PRI
786 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
, BUFSIZ
),
788 (route_tag_t
)ntohs(rte
->tag
),
789 (unsigned long)ntohl(rte
->metric
));
794 /* Check if the destination address is valid (unicast; not net 0
795 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
796 check net 0 because we accept default route. */
797 static int rip_destination_check(struct in_addr addr
)
799 uint32_t destination
;
801 /* Convert to host byte order. */
802 destination
= ntohl(addr
.s_addr
);
804 if (IPV4_NET127(destination
))
807 /* Net 0 may match to the default route. */
808 if (IPV4_NET0(destination
) && destination
!= 0)
811 /* Unicast address must belong to class A, B, C. */
812 if (IN_CLASSA(destination
))
814 if (IN_CLASSB(destination
))
816 if (IN_CLASSC(destination
))
822 /* RIP version 2 authentication. */
823 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
824 struct interface
*ifp
)
826 struct rip_interface
*ri
;
827 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
830 /* reject passwords with zeros in the middle of the string */
831 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
832 if (auth_str
[i
] != '\0')
836 if (IS_RIP_DEBUG_EVENT
)
837 zlog_debug("RIPv2 simple password authentication from %s",
838 inet_ntoa(from
->sin_addr
));
842 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
843 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
846 /* Simple password authentication. */
848 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
852 struct keychain
*keychain
;
855 keychain
= keychain_lookup(ri
->key_chain
);
856 if (keychain
== NULL
|| keychain
->key
== NULL
)
859 key
= key_match_for_accept(keychain
, auth_str
);
866 /* RIP version 2 authentication with MD5. */
867 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
868 int length
, struct interface
*ifp
)
870 struct rip_interface
*ri
;
871 struct rip_md5_info
*md5
;
872 struct rip_md5_data
*md5data
;
873 struct keychain
*keychain
;
875 #ifdef CRYPTO_OPENSSL
877 #elif CRYPTO_INTERNAL
880 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
882 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
884 if (IS_RIP_DEBUG_EVENT
)
885 zlog_debug("RIPv2 MD5 authentication from %s",
886 inet_ntoa(from
->sin_addr
));
889 md5
= (struct rip_md5_info
*)&packet
->rte
;
891 /* Check auth type. */
892 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
895 /* If the authentication length is less than 16, then it must be wrong
897 * any interpretation of rfc2082. Some implementations also interpret
898 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
899 * RIP_AUTH_MD5_COMPAT_SIZE.
901 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
902 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
903 if (IS_RIP_DEBUG_EVENT
)
905 "RIPv2 MD5 authentication, strange authentication "
911 /* grab and verify check packet length */
912 packet_len
= ntohs(md5
->packet_len
);
914 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
915 if (IS_RIP_DEBUG_EVENT
)
917 "RIPv2 MD5 authentication, packet length field %d "
918 "greater than received length %d!",
919 md5
->packet_len
, length
);
923 /* retrieve authentication data */
924 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
927 keychain
= keychain_lookup(ri
->key_chain
);
928 if (keychain
== NULL
)
931 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
932 if (key
== NULL
|| key
->string
== NULL
)
935 strlcpy(auth_str
, key
->string
, sizeof(auth_str
));
936 } else if (ri
->auth_str
)
937 strlcpy(auth_str
, ri
->auth_str
, sizeof(auth_str
));
939 if (auth_str
[0] == 0)
942 /* MD5 digest authentication. */
943 #ifdef CRYPTO_OPENSSL
944 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
945 ctx
= EVP_MD_CTX_new();
946 EVP_DigestInit(ctx
, EVP_md5());
947 EVP_DigestUpdate(ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
948 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
949 EVP_DigestFinal(ctx
, digest
, &md5_size
);
950 EVP_MD_CTX_free(ctx
);
951 #elif CRYPTO_INTERNAL
952 memset(&ctx
, 0, sizeof(ctx
));
954 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
955 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
956 MD5Final(digest
, &ctx
);
959 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
965 /* Pick correct auth string for sends, prepare auth_str buffer for use.
966 * (left justified and padded).
968 * presumes one of ri or key is valid, and that the auth strings they point
969 * to are nul terminated. If neither are present, auth_str will be fully
973 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
974 char *auth_str
, int len
)
978 memset(auth_str
, 0, len
);
979 if (key
&& key
->string
)
980 strlcpy(auth_str
, key
->string
, len
);
981 else if (ri
->auth_str
)
982 strlcpy(auth_str
, ri
->auth_str
, len
);
987 /* Write RIPv2 simple password authentication information
989 * auth_str is presumed to be 2 bytes and correctly prepared
990 * (left justified and zero padded).
992 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
994 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
996 stream_putw(s
, RIP_FAMILY_AUTH
);
997 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
998 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
1003 /* write RIPv2 MD5 "authentication header"
1004 * (uses the auth key data field)
1006 * Digest offset field is set to 0.
1008 * returns: offset of the digest offset field, which must be set when
1009 * length to the auth-data MD5 digest is known.
1011 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1016 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1018 /* MD5 authentication. */
1019 stream_putw(s
, RIP_FAMILY_AUTH
);
1020 stream_putw(s
, RIP_AUTH_MD5
);
1022 /* MD5 AH digest offset field.
1024 * Set to placeholder value here, to true value when RIP-2 Packet length
1025 * is known. Actual value is set in .....().
1027 doff
= stream_get_endp(s
);
1032 stream_putc(s
, key
->index
% 256);
1036 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1037 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1039 * to be configurable.
1041 stream_putc(s
, ri
->md5_auth_len
);
1043 /* Sequence Number (non-decreasing). */
1044 /* RFC2080: The value used in the sequence number is
1045 arbitrary, but two suggestions are the time of the
1046 message's creation or a simple message counter. */
1047 stream_putl(s
, time(NULL
));
1049 /* Reserved field must be zero. */
1056 /* If authentication is in used, write the appropriate header
1057 * returns stream offset to which length must later be written
1058 * or 0 if this is not required
1060 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1061 struct key
*key
, char *auth_str
, int len
)
1063 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1065 switch (ri
->auth_type
) {
1066 case RIP_AUTH_SIMPLE_PASSWORD
:
1067 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1068 rip_auth_simple_write(s
, auth_str
, len
);
1071 return rip_auth_md5_ah_write(s
, ri
, key
);
1077 /* Write RIPv2 MD5 authentication data trailer */
1078 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1079 size_t doff
, char *auth_str
, int authlen
)
1082 #ifdef CRYPTO_OPENSSL
1084 #elif CRYPTO_INTERNAL
1087 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1089 /* Make it sure this interface is configured as MD5
1091 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1092 && (authlen
== RIP_AUTH_MD5_SIZE
));
1095 /* Get packet length. */
1096 len
= stream_get_endp(s
);
1098 /* Check packet length. */
1099 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1102 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1107 /* Set the digest offset length in the header */
1108 stream_putw_at(s
, doff
, len
);
1110 /* Set authentication data. */
1111 stream_putw(s
, RIP_FAMILY_AUTH
);
1112 stream_putw(s
, RIP_AUTH_DATA
);
1114 /* Generate a digest for the RIP packet. */
1115 #ifdef CRYPTO_OPENSSL
1116 unsigned int md5_size
= RIP_AUTH_MD5_SIZE
;
1117 ctx
= EVP_MD_CTX_new();
1118 EVP_DigestInit(ctx
, EVP_md5());
1119 EVP_DigestUpdate(ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1120 EVP_DigestUpdate(ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1121 EVP_DigestFinal(ctx
, digest
, &md5_size
);
1122 EVP_MD_CTX_free(ctx
);
1123 #elif CRYPTO_INTERNAL
1124 memset(&ctx
, 0, sizeof(ctx
));
1126 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1127 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1128 MD5Final(digest
, &ctx
);
1131 /* Copy the digest to the packet. */
1132 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1135 /* RIP routing information. */
1136 static void rip_response_process(struct rip_packet
*packet
, int size
,
1137 struct sockaddr_in
*from
,
1138 struct connected
*ifc
)
1140 struct rip_interface
*ri
= ifc
->ifp
->info
;
1141 struct rip
*rip
= ri
->rip
;
1144 struct prefix_ipv4 ifaddr
;
1145 struct prefix_ipv4 ifaddrclass
;
1148 memset(&ifaddr
, 0, sizeof(ifaddr
));
1149 /* We don't know yet. */
1152 /* The Response must be ignored if it is not from the RIP
1153 port. (RFC2453 - Sec. 3.9.2)*/
1154 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1155 zlog_info("response doesn't come from RIP port: %d",
1157 rip_peer_bad_packet(rip
, from
);
1161 /* The datagram's IPv4 source address should be checked to see
1162 whether the datagram is from a valid neighbor; the source of the
1163 datagram must be on a directly connected network (RFC2453 - Sec.
1165 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1169 "This datagram doesn't came from a valid neighbor: %s",
1170 inet_ntoa(from
->sin_addr
));
1171 rip_peer_bad_packet(rip
, from
);
1175 /* It is also worth checking to see whether the response is from one
1176 of the router's own addresses. */
1178 ; /* Alredy done in rip_read () */
1180 /* Update RIP peer. */
1181 rip_peer_update(rip
, from
, packet
->version
);
1183 /* Set RTE pointer. */
1186 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1187 /* RIPv2 authentication check. */
1188 /* If the Address Family Identifier of the first (and only the
1189 first) entry in the message is 0xFFFF, then the remainder of
1190 the entry contains the authentication. */
1191 /* If the packet gets here it means authentication enabled */
1192 /* Check is done in rip_read(). So, just skipping it */
1193 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1194 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1197 if (rte
->family
!= htons(AF_INET
)) {
1198 /* Address family check. RIP only supports AF_INET. */
1199 zlog_info("Unsupported family %d from %s.",
1201 inet_ntoa(from
->sin_addr
));
1205 /* - is the destination address valid (e.g., unicast; not net 0
1207 if (!rip_destination_check(rte
->prefix
)) {
1209 "Network is net 0 or net 127 or it is not unicast network");
1210 rip_peer_bad_route(rip
, from
);
1214 /* Convert metric value to host byte order. */
1215 rte
->metric
= ntohl(rte
->metric
);
1217 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1218 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1219 zlog_info("Route's metric is not in the 1-16 range.");
1220 rip_peer_bad_route(rip
, from
);
1224 /* RIPv1 does not have nexthop value. */
1225 if (packet
->version
== RIPv1
&& rte
->nexthop
.s_addr
!= 0) {
1226 zlog_info("RIPv1 packet with nexthop value %s",
1227 inet_ntoa(rte
->nexthop
));
1228 rip_peer_bad_route(rip
, from
);
1232 /* That is, if the provided information is ignored, a possibly
1233 sub-optimal, but absolutely valid, route may be taken. If
1234 the received Next Hop is not directly reachable, it should be
1235 treated as 0.0.0.0. */
1236 if (packet
->version
== RIPv2
&& rte
->nexthop
.s_addr
!= 0) {
1239 /* Multicast address check. */
1240 addrval
= ntohl(rte
->nexthop
.s_addr
);
1241 if (IN_CLASSD(addrval
)) {
1243 "Nexthop %s is multicast address, skip this rte",
1244 inet_ntoa(rte
->nexthop
));
1248 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1249 rip
->vrf
->vrf_id
)) {
1250 struct route_node
*rn
;
1251 struct rip_info
*rinfo
;
1253 rn
= route_node_match_ipv4(rip
->table
,
1259 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1262 if (IS_RIP_DEBUG_EVENT
)
1264 "Next hop %s is on RIP network. Set nexthop to the packet's originator",
1267 rte
->nexthop
= rinfo
->from
;
1269 if (IS_RIP_DEBUG_EVENT
)
1271 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1274 rte
->nexthop
.s_addr
= 0;
1277 route_unlock_node(rn
);
1279 if (IS_RIP_DEBUG_EVENT
)
1281 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1284 rte
->nexthop
.s_addr
= 0;
1289 /* For RIPv1, there won't be a valid netmask.
1291 This is a best guess at the masks. If everyone was using old
1292 Ciscos before the 'ip subnet zero' option, it would be almost
1295 Cisco summarize ripv1 advertisements to the classful boundary
1296 (/16 for class B's) except when the RIP packet does to inside
1297 the classful network in question. */
1299 if ((packet
->version
== RIPv1
&& rte
->prefix
.s_addr
!= 0)
1300 || (packet
->version
== RIPv2
1301 && (rte
->prefix
.s_addr
!= 0
1302 && rte
->mask
.s_addr
== 0))) {
1303 uint32_t destination
;
1305 if (subnetted
== -1) {
1306 memcpy(&ifaddr
, ifc
->address
,
1307 sizeof(struct prefix_ipv4
));
1308 memcpy(&ifaddrclass
, &ifaddr
,
1309 sizeof(struct prefix_ipv4
));
1310 apply_classful_mask_ipv4(&ifaddrclass
);
1312 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1316 destination
= ntohl(rte
->prefix
.s_addr
);
1318 if (IN_CLASSA(destination
))
1319 masklen2ip(8, &rte
->mask
);
1320 else if (IN_CLASSB(destination
))
1321 masklen2ip(16, &rte
->mask
);
1322 else if (IN_CLASSC(destination
))
1323 masklen2ip(24, &rte
->mask
);
1326 masklen2ip(ifaddrclass
.prefixlen
,
1327 (struct in_addr
*)&destination
);
1328 if ((subnetted
== 1)
1329 && ((rte
->prefix
.s_addr
& destination
)
1330 == ifaddrclass
.prefix
.s_addr
)) {
1331 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1332 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1333 != rte
->prefix
.s_addr
)
1334 masklen2ip(32, &rte
->mask
);
1335 if (IS_RIP_DEBUG_EVENT
)
1336 zlog_debug("Subnetted route %s",
1337 inet_ntoa(rte
->prefix
));
1339 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1340 != rte
->prefix
.s_addr
)
1344 if (IS_RIP_DEBUG_EVENT
) {
1345 zlog_debug("Resultant route %s",
1346 inet_ntoa(rte
->prefix
));
1347 zlog_debug("Resultant mask %s",
1348 inet_ntoa(rte
->mask
));
1352 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1353 ignore the entry. */
1354 if ((packet
->version
== RIPv2
) && (rte
->mask
.s_addr
!= 0)
1355 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1356 != rte
->prefix
.s_addr
)) {
1358 "RIPv2 address %s is not mask /%d applied one",
1359 inet_ntoa(rte
->prefix
), ip_masklen(rte
->mask
));
1360 rip_peer_bad_route(rip
, from
);
1364 /* Default route's netmask is ignored. */
1365 if (packet
->version
== RIPv2
&& (rte
->prefix
.s_addr
== 0)
1366 && (rte
->mask
.s_addr
!= 0)) {
1367 if (IS_RIP_DEBUG_EVENT
)
1369 "Default route with non-zero netmask. Set zero to netmask");
1370 rte
->mask
.s_addr
= 0;
1373 /* Routing table updates. */
1374 rip_rte_process(rte
, from
, ifc
->ifp
);
1378 /* Make socket for RIP protocol. */
1379 int rip_create_socket(struct vrf
*vrf
)
1383 struct sockaddr_in addr
;
1384 const char *vrf_dev
= NULL
;
1386 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1387 addr
.sin_family
= AF_INET
;
1388 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1389 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1390 addr
.sin_len
= sizeof(struct sockaddr_in
);
1391 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1392 /* sending port must always be the RIP port */
1393 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1395 /* Make datagram socket. */
1396 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1397 vrf_dev
= vrf
->name
;
1398 frr_with_privs(&ripd_privs
) {
1399 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1402 flog_err_sys(EC_LIB_SOCKET
,
1403 "Cannot create UDP socket: %s",
1404 safe_strerror(errno
));
1409 sockopt_broadcast(sock
);
1410 sockopt_reuseaddr(sock
);
1411 sockopt_reuseport(sock
);
1412 setsockopt_ipv4_multicast_loop(sock
, 0);
1413 #ifdef IPTOS_PREC_INTERNETCONTROL
1414 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1416 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1418 frr_with_privs(&ripd_privs
) {
1419 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1421 zlog_err("%s: Can't bind socket %d to %s port %d: %s",
1422 __func__
, sock
, inet_ntoa(addr
.sin_addr
),
1423 (int)ntohs(addr
.sin_port
),
1424 safe_strerror(errno
));
1434 /* RIP packet send to destination address, on interface denoted by
1435 * by connected argument. NULL to argument denotes destination should be
1436 * should be RIP multicast group
1438 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1439 struct connected
*ifc
)
1441 struct rip_interface
*ri
;
1444 struct sockaddr_in sin
;
1448 struct cmsghdr
*cmsgptr
;
1449 char adata
[256] = {};
1450 struct in_pktinfo
*pkt
;
1451 #endif /* GNU_LINUX */
1453 assert(ifc
!= NULL
);
1454 ri
= ifc
->ifp
->info
;
1457 if (IS_RIP_DEBUG_PACKET
) {
1458 #define ADDRESS_SIZE 20
1459 char dst
[ADDRESS_SIZE
];
1462 strlcpy(dst
, inet_ntoa(to
->sin_addr
), sizeof(dst
));
1464 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1465 strlcpy(dst
, inet_ntoa(sin
.sin_addr
), sizeof(dst
));
1468 zlog_debug("rip_send_packet %s > %s (%s)",
1469 inet_ntoa(ifc
->address
->u
.prefix4
), dst
,
1473 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1475 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1477 * with multiple addresses on the same subnet: the first address
1478 * on the subnet is configured "primary", and all subsequent
1480 * on that subnet are treated as "secondary" addresses.
1481 * In order to avoid routing-table bloat on other rip listeners,
1482 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1484 * XXX Since Linux is the only system for which the
1485 * ZEBRA_IFA_SECONDARY
1486 * flag is set, we would end up sending a packet for a
1488 * source address on non-linux systems.
1490 if (IS_RIP_DEBUG_PACKET
)
1491 zlog_debug("duplicate dropped");
1495 /* Make destination address. */
1496 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1497 sin
.sin_family
= AF_INET
;
1498 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1499 sin
.sin_len
= sizeof(struct sockaddr_in
);
1500 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1502 /* When destination is specified, use it's port and address. */
1504 sin
.sin_port
= to
->sin_port
;
1505 sin
.sin_addr
= to
->sin_addr
;
1507 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1508 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1510 rip_interface_multicast_set(rip
->sock
, ifc
);
1513 memset(&msg
, 0, sizeof(msg
));
1514 msg
.msg_name
= (void *)&sin
;
1515 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1522 msg
.msg_control
= (void *)adata
;
1523 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1525 cmsgptr
= (struct cmsghdr
*)adata
;
1526 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1527 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1528 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1529 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1530 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1531 #endif /* GNU_LINUX */
1533 ret
= sendmsg(rip
->sock
, &msg
, 0);
1535 if (IS_RIP_DEBUG_EVENT
)
1536 zlog_debug("SEND to %s.%d", inet_ntoa(sin
.sin_addr
),
1537 ntohs(sin
.sin_port
));
1540 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1545 /* Add redistributed route to RIP table. */
1546 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1547 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1548 unsigned int metric
, unsigned char distance
,
1552 struct route_node
*rp
= NULL
;
1553 struct rip_info
*rinfo
= NULL
, newinfo
;
1554 struct list
*list
= NULL
;
1556 /* Redistribute route */
1557 ret
= rip_destination_check(p
->prefix
);
1561 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1563 memset(&newinfo
, 0, sizeof(struct rip_info
));
1564 newinfo
.type
= type
;
1565 newinfo
.sub_type
= sub_type
;
1567 newinfo
.external_metric
= metric
;
1568 newinfo
.distance
= distance
;
1569 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1574 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1575 rinfo
= listgetdata(listhead(list
));
1577 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1578 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1579 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1580 route_unlock_node(rp
);
1584 /* Manually configured RIP route check. */
1585 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1586 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1587 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1588 if (type
!= ZEBRA_ROUTE_RIP
1589 || ((sub_type
!= RIP_ROUTE_STATIC
)
1590 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1591 route_unlock_node(rp
);
1596 (void)rip_ecmp_replace(rip
, &newinfo
);
1597 route_unlock_node(rp
);
1599 (void)rip_ecmp_add(rip
, &newinfo
);
1601 if (IS_RIP_DEBUG_EVENT
) {
1602 zlog_debug("Redistribute new prefix %s/%d",
1603 inet_ntoa(p
->prefix
), p
->prefixlen
);
1606 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1609 /* Delete redistributed route from RIP table. */
1610 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1611 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1614 struct route_node
*rp
;
1615 struct rip_info
*rinfo
;
1617 ret
= rip_destination_check(p
->prefix
);
1621 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1623 struct list
*list
= rp
->info
;
1625 if (list
!= NULL
&& listcount(list
) != 0) {
1626 rinfo
= listgetdata(listhead(list
));
1627 if (rinfo
!= NULL
&& rinfo
->type
== type
1628 && rinfo
->sub_type
== sub_type
1629 && rinfo
->nh
.ifindex
== ifindex
) {
1630 /* Perform poisoned reverse. */
1631 rinfo
->metric
= RIP_METRIC_INFINITY
;
1632 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1633 rip_garbage_collect
,
1635 RIP_TIMER_OFF(rinfo
->t_timeout
);
1636 rinfo
->flags
|= RIP_RTF_CHANGED
;
1638 if (IS_RIP_DEBUG_EVENT
)
1640 "Poison %s/%d on the interface %s with an "
1641 "infinity metric [delete]",
1642 inet_ntoa(p
->prefix
),
1648 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1651 route_unlock_node(rp
);
1655 /* Response to request called from rip_read ().*/
1656 static void rip_request_process(struct rip_packet
*packet
, int size
,
1657 struct sockaddr_in
*from
, struct connected
*ifc
)
1662 struct prefix_ipv4 p
;
1663 struct route_node
*rp
;
1664 struct rip_info
*rinfo
;
1665 struct rip_interface
*ri
;
1667 /* Does not reponse to the requests on the loopback interfaces */
1668 if (if_is_loopback(ifc
->ifp
))
1671 /* Check RIP process is enabled on this interface. */
1672 ri
= ifc
->ifp
->info
;
1677 /* When passive interface is specified, suppress responses */
1681 /* RIP peer update. */
1682 rip_peer_update(rip
, from
, packet
->version
);
1684 lim
= ((caddr_t
)packet
) + size
;
1687 /* The Request is processed entry by entry. If there are no
1688 entries, no response is given. */
1689 if (lim
== (caddr_t
)rte
)
1692 /* There is one special case. If there is exactly one entry in the
1693 request, and it has an address family identifier of zero and a
1694 metric of infinity (i.e., 16), then this is a request to send the
1695 entire routing table. */
1696 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1697 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1698 /* All route with split horizon */
1699 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1701 if (ntohs(rte
->family
) != AF_INET
)
1704 /* Examine the list of RTEs in the Request one by one. For each
1705 entry, look up the destination in the router's routing
1706 database and, if there is a route, put that route's metric in
1707 the metric field of the RTE. If there is no explicit route
1708 to the specified destination, put infinity in the metric
1709 field. Once all the entries have been filled in, change the
1710 command from Request to Response and send the datagram back
1711 to the requestor. */
1714 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1715 p
.prefix
= rte
->prefix
;
1716 p
.prefixlen
= ip_masklen(rte
->mask
);
1717 apply_mask_ipv4(&p
);
1719 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1721 rinfo
= listgetdata(
1722 listhead((struct list
*)rp
->info
));
1723 rte
->metric
= htonl(rinfo
->metric
);
1724 route_unlock_node(rp
);
1726 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1728 packet
->command
= RIP_RESPONSE
;
1730 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1732 rip
->counters
.queries
++;
1735 /* First entry point of RIP packet. */
1736 static int rip_read(struct thread
*t
)
1738 struct rip
*rip
= THREAD_ARG(t
);
1742 union rip_buf rip_buf
;
1743 struct rip_packet
*packet
;
1744 struct sockaddr_in from
;
1748 struct interface
*ifp
= NULL
;
1749 struct connected
*ifc
;
1750 struct rip_interface
*ri
;
1753 /* Fetch socket then register myself. */
1754 sock
= THREAD_FD(t
);
1757 /* Add myself to tne next event */
1758 rip_event(rip
, RIP_READ
, sock
);
1760 /* RIPd manages only IPv4. */
1761 memset(&from
, 0, sizeof(struct sockaddr_in
));
1762 fromlen
= sizeof(struct sockaddr_in
);
1764 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1765 (struct sockaddr
*)&from
, &fromlen
);
1767 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1768 safe_strerror(errno
));
1772 /* Check is this packet comming from myself? */
1773 if (if_check_address(rip
, from
.sin_addr
)) {
1774 if (IS_RIP_DEBUG_PACKET
)
1775 zlog_debug("ignore packet comes from myself (VRF %s)",
1780 /* Which interface is this packet comes from. */
1781 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1786 /* RIP packet received */
1787 if (IS_RIP_DEBUG_EVENT
)
1788 zlog_debug("RECV packet from %s port %d on %s (VRF %s)",
1789 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1790 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1792 /* If this packet come from unknown interface, ignore it. */
1795 "rip_read: cannot find interface for packet from %s port %d (VRF %s)",
1796 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1802 p
.u
.prefix4
= from
.sin_addr
;
1803 p
.prefixlen
= IPV4_MAX_BITLEN
;
1805 ifc
= connected_lookup_prefix(ifp
, &p
);
1809 "rip_read: cannot find connected address for packet from %s "
1810 "port %d on interface %s (VRF %s)",
1811 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1812 ifp
->name
, rip
->vrf_name
);
1816 /* Packet length check. */
1817 if (len
< RIP_PACKET_MINSIZ
) {
1818 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1820 rip_peer_bad_packet(rip
, &from
);
1823 if (len
> RIP_PACKET_MAXSIZ
) {
1824 zlog_warn("packet size %d is larger than max size %d", len
,
1826 rip_peer_bad_packet(rip
, &from
);
1830 /* Packet alignment check. */
1831 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1832 zlog_warn("packet size %d is wrong for RIP packet alignment",
1834 rip_peer_bad_packet(rip
, &from
);
1838 /* Set RTE number. */
1839 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1841 /* For easy to handle. */
1842 packet
= &rip_buf
.rip_packet
;
1844 /* RIP version check. */
1845 if (packet
->version
== 0) {
1846 zlog_info("version 0 with command %d received.",
1848 rip_peer_bad_packet(rip
, &from
);
1852 /* Dump RIP packet. */
1853 if (IS_RIP_DEBUG_RECV
)
1854 rip_packet_dump(packet
, len
, "RECV");
1856 /* RIP version adjust. This code should rethink now. RFC1058 says
1857 that "Version 1 implementations are to ignore this extra data and
1858 process only the fields specified in this document.". So RIPv3
1859 packet should be treated as RIPv1 ignoring must be zero field. */
1860 if (packet
->version
> RIPv2
)
1861 packet
->version
= RIPv2
;
1863 /* Is RIP running or is this RIP neighbor ?*/
1865 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1866 if (IS_RIP_DEBUG_EVENT
)
1867 zlog_debug("RIP is not enabled on interface %s.",
1869 rip_peer_bad_packet(rip
, &from
);
1873 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1874 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1876 if (vrecv
== RI_RIP_VERSION_NONE
1877 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1878 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1879 if (IS_RIP_DEBUG_PACKET
)
1881 " packet's v%d doesn't fit to if version spec",
1883 rip_peer_bad_packet(rip
, &from
);
1887 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1888 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1889 accepted; authenticated RIP-2 messages shall be discarded. */
1890 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1891 && (packet
->version
== RIPv2
)
1892 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1893 if (IS_RIP_DEBUG_EVENT
)
1895 "packet RIPv%d is dropped because authentication disabled",
1897 ripd_notif_send_auth_type_failure(ifp
->name
);
1898 rip_peer_bad_packet(rip
, &from
);
1903 If the router is configured to authenticate RIP-2 messages, then
1904 RIP-1 messages and RIP-2 messages which pass authentication
1905 testing shall be accepted; unauthenticated and failed
1906 authentication RIP-2 messages shall be discarded. For maximum
1907 security, RIP-1 messages should be ignored when authentication is
1908 in use (see section 4.1); otherwise, the routing information from
1909 authenticated messages will be propagated by RIP-1 routers in an
1910 unauthenticated manner.
1912 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1913 * always reply regardless of authentication settings, because:
1915 * - if there other authorised routers on-link, the REQUESTor can
1916 * passively obtain the routing updates anyway
1917 * - if there are no other authorised routers on-link, RIP can
1918 * easily be disabled for the link to prevent giving out information
1919 * on state of this routers RIP routing table..
1921 * I.e. if RIPv1 has any place anymore these days, it's as a very
1922 * simple way to distribute routing information (e.g. to embedded
1923 * hosts / appliances) and the ability to give out RIPv1
1924 * routing-information freely, while still requiring RIPv2
1925 * authentication for any RESPONSEs might be vaguely useful.
1927 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1928 /* Discard RIPv1 messages other than REQUESTs */
1929 if (packet
->command
!= RIP_REQUEST
) {
1930 if (IS_RIP_DEBUG_PACKET
)
1933 " dropped because authentication enabled");
1934 ripd_notif_send_auth_type_failure(ifp
->name
);
1935 rip_peer_bad_packet(rip
, &from
);
1938 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1939 const char *auth_desc
;
1942 /* There definitely is no authentication in the packet.
1944 if (IS_RIP_DEBUG_PACKET
)
1946 "RIPv2 authentication failed: no auth RTE in packet");
1947 ripd_notif_send_auth_type_failure(ifp
->name
);
1948 rip_peer_bad_packet(rip
, &from
);
1952 /* First RTE must be an Authentication Family RTE */
1953 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1954 if (IS_RIP_DEBUG_PACKET
)
1957 " dropped because authentication enabled");
1958 ripd_notif_send_auth_type_failure(ifp
->name
);
1959 rip_peer_bad_packet(rip
, &from
);
1963 /* Check RIPv2 authentication. */
1964 switch (ntohs(packet
->rte
->tag
)) {
1965 case RIP_AUTH_SIMPLE_PASSWORD
:
1966 auth_desc
= "simple";
1967 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1972 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1973 /* Reset RIP packet length to trim MD5 data. */
1979 auth_desc
= "unknown type";
1980 if (IS_RIP_DEBUG_PACKET
)
1982 "RIPv2 Unknown authentication type %d",
1983 ntohs(packet
->rte
->tag
));
1987 if (IS_RIP_DEBUG_PACKET
)
1988 zlog_debug("RIPv2 %s authentication success",
1991 if (IS_RIP_DEBUG_PACKET
)
1992 zlog_debug("RIPv2 %s authentication failure",
1994 ripd_notif_send_auth_failure(ifp
->name
);
1995 rip_peer_bad_packet(rip
, &from
);
2000 /* Process each command. */
2001 switch (packet
->command
) {
2003 rip_response_process(packet
, len
, &from
, ifc
);
2007 rip_request_process(packet
, len
, &from
, ifc
);
2012 "Obsolete command %s received, please sent it to routed",
2013 lookup_msg(rip_msg
, packet
->command
, NULL
));
2014 rip_peer_bad_packet(rip
, &from
);
2016 case RIP_POLL_ENTRY
:
2017 zlog_info("Obsolete command %s received",
2018 lookup_msg(rip_msg
, packet
->command
, NULL
));
2019 rip_peer_bad_packet(rip
, &from
);
2022 zlog_info("Unknown RIP command %d received", packet
->command
);
2023 rip_peer_bad_packet(rip
, &from
);
2030 /* Write routing table entry to the stream and return next index of
2031 the routing table entry in the stream. */
2032 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2033 uint8_t version
, struct rip_info
*rinfo
)
2035 struct in_addr mask
;
2037 /* Write routing table entry. */
2038 if (version
== RIPv1
) {
2039 stream_putw(s
, AF_INET
);
2041 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2042 stream_put_ipv4(s
, 0);
2043 stream_put_ipv4(s
, 0);
2044 stream_putl(s
, rinfo
->metric_out
);
2046 masklen2ip(p
->prefixlen
, &mask
);
2048 stream_putw(s
, AF_INET
);
2049 stream_putw(s
, rinfo
->tag_out
);
2050 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2051 stream_put_ipv4(s
, mask
.s_addr
);
2052 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2053 stream_putl(s
, rinfo
->metric_out
);
2059 /* Send update to the ifp or spcified neighbor. */
2060 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2061 int route_type
, uint8_t version
)
2066 struct route_node
*rp
;
2067 struct rip_info
*rinfo
;
2068 struct rip_interface
*ri
;
2069 struct prefix_ipv4
*p
;
2070 struct prefix_ipv4 classfull
;
2071 struct prefix_ipv4 ifaddrclass
;
2072 struct key
*key
= NULL
;
2073 /* this might need to made dynamic if RIP ever supported auth methods
2074 with larger key string sizes */
2075 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2076 size_t doff
= 0; /* offset of digest offset field */
2080 struct list
*list
= NULL
;
2081 struct listnode
*listnode
= NULL
;
2083 /* Logging output event. */
2084 if (IS_RIP_DEBUG_EVENT
) {
2086 zlog_debug("update routes to neighbor %s",
2087 inet_ntoa(to
->sin_addr
));
2089 zlog_debug("update routes on interface %s ifindex %d",
2090 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2093 /* Get RIP interface. */
2094 ri
= ifc
->ifp
->info
;
2097 /* Set output stream. */
2100 /* Reset stream and RTE counter. */
2102 rtemax
= RIP_MAX_RTE
;
2104 /* If output interface is in simple password authentication mode, we
2105 need space for authentication data. */
2106 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2109 /* If output interface is in MD5 authentication mode, we need space
2110 for authentication header and data. */
2111 if (ri
->auth_type
== RIP_AUTH_MD5
)
2114 /* If output interface is in simple password authentication mode
2115 and string or keychain is specified we need space for auth. data */
2116 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2117 if (ri
->key_chain
) {
2118 struct keychain
*keychain
;
2120 keychain
= keychain_lookup(ri
->key_chain
);
2122 key
= key_lookup_for_send(keychain
);
2124 /* to be passed to auth functions later */
2125 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2126 if (strlen(auth_str
) == 0)
2130 if (version
== RIPv1
) {
2131 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2132 apply_classful_mask_ipv4(&ifaddrclass
);
2134 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2138 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2139 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2140 rinfo
= listgetdata(listhead(list
));
2141 /* For RIPv1, if we are subnetted, output subnets in our
2143 /* that have the same mask as the output "interface".
2145 /* networks, only the classfull version is output. */
2147 if (version
== RIPv1
) {
2148 p
= (struct prefix_ipv4
*)&rp
->p
;
2150 if (IS_RIP_DEBUG_PACKET
)
2152 "RIPv1 mask check, %s/%d considered for output",
2153 inet_ntoa(rp
->p
.u
.prefix4
),
2158 (struct prefix
*)&ifaddrclass
,
2160 if ((ifc
->address
->prefixlen
2162 && (rp
->p
.prefixlen
!= 32))
2165 memcpy(&classfull
, &rp
->p
,
2166 sizeof(struct prefix_ipv4
));
2167 apply_classful_mask_ipv4(&classfull
);
2168 if (rp
->p
.u
.prefix4
.s_addr
!= 0
2169 && classfull
.prefixlen
2173 if (IS_RIP_DEBUG_PACKET
)
2175 "RIPv1 mask check, %s/%d made it through",
2176 inet_ntoa(rp
->p
.u
.prefix4
),
2179 p
= (struct prefix_ipv4
*)&rp
->p
;
2181 /* Apply output filters. */
2182 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2186 /* Changed route only output. */
2187 if (route_type
== rip_changed_route
2188 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2191 /* Split horizon. */
2192 /* if (split_horizon == rip_split_horizon) */
2193 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2195 * We perform split horizon for RIP and
2197 * For rip routes, we want to suppress the route
2199 * end up sending the route back on the
2201 * learned it from, with a higher metric. For
2203 * we suppress the route if the prefix is a
2205 * source address that we are going to use for
2207 * (in order to handle the case when multiple
2209 * configured on the same interface).
2212 struct rip_info
*tmp_rinfo
= NULL
;
2213 struct connected
*tmp_ifc
= NULL
;
2215 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2217 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2218 && tmp_rinfo
->nh
.ifindex
2219 == ifc
->ifp
->ifindex
) {
2225 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2226 for (ALL_LIST_ELEMENTS_RO(
2227 ifc
->ifp
->connected
,
2231 tmp_ifc
->address
)) {
2241 /* Preparation for route-map. */
2242 rinfo
->metric_set
= 0;
2243 rinfo
->nexthop_out
.s_addr
= 0;
2244 rinfo
->metric_out
= rinfo
->metric
;
2245 rinfo
->tag_out
= rinfo
->tag
;
2246 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2248 /* In order to avoid some local loops,
2249 * if the RIP route has a nexthop via this interface,
2251 * otherwise set it to 0. The nexthop should not be
2253 * beyond the local broadcast/multicast area in order
2254 * to avoid an IGP multi-level recursive look-up.
2257 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2258 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2260 /* Interface route-map */
2261 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2262 ret
= route_map_apply(
2263 ri
->routemap
[RIP_FILTER_OUT
],
2264 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2266 if (ret
== RMAP_DENYMATCH
) {
2267 if (IS_RIP_DEBUG_PACKET
)
2269 "RIP %s/%d is filtered by route-map out",
2270 inet_ntoa(p
->prefix
),
2276 /* Apply redistribute route map - continue, if deny */
2277 if (rip
->redist
[rinfo
->type
].route_map
.name
2278 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2279 ret
= route_map_apply(
2280 rip
->redist
[rinfo
->type
].route_map
.map
,
2281 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2283 if (ret
== RMAP_DENYMATCH
) {
2284 if (IS_RIP_DEBUG_PACKET
)
2286 "%s/%d is filtered by route-map",
2287 inet_ntoa(p
->prefix
),
2293 /* When route-map does not set metric. */
2294 if (!rinfo
->metric_set
) {
2295 /* If redistribute metric is set. */
2296 if (rip
->redist
[rinfo
->type
].metric_config
2297 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2299 rip
->redist
[rinfo
->type
].metric
;
2301 /* If the route is not connected or
2303 one, use default-metric value*/
2304 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2306 != ZEBRA_ROUTE_CONNECT
2308 != RIP_METRIC_INFINITY
)
2310 rip
->default_metric
;
2314 /* Apply offset-list */
2315 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2316 rip_offset_list_apply_out(p
, ifc
->ifp
,
2317 &rinfo
->metric_out
);
2319 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2320 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2322 /* Perform split-horizon with poisoned reverse
2323 * for RIP and connected routes.
2325 if (ri
->split_horizon
2326 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2328 * We perform split horizon for RIP and
2330 * For rip routes, we want to suppress the route
2332 * end up sending the route back on the
2334 * learned it from, with a higher metric. For
2336 * we suppress the route if the prefix is a
2338 * source address that we are going to use for
2340 * (in order to handle the case when multiple
2342 * configured on the same interface).
2344 struct rip_info
*tmp_rinfo
= NULL
;
2345 struct connected
*tmp_ifc
= NULL
;
2347 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2349 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2350 && tmp_rinfo
->nh
.ifindex
2351 == ifc
->ifp
->ifindex
)
2353 RIP_METRIC_INFINITY
;
2355 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2356 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2357 for (ALL_LIST_ELEMENTS_RO(
2358 ifc
->ifp
->connected
,
2362 tmp_ifc
->address
)) {
2364 RIP_METRIC_INFINITY
;
2370 /* Prepare preamble, auth headers, if needs be */
2372 stream_putc(s
, RIP_RESPONSE
);
2373 stream_putc(s
, version
);
2376 /* auth header for !v1 && !no_auth */
2377 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2378 && (version
!= RIPv1
))
2379 doff
= rip_auth_header_write(
2380 s
, ri
, key
, auth_str
,
2381 RIP_AUTH_SIMPLE_SIZE
);
2384 /* Write RTE to the stream. */
2385 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2386 if (num
== rtemax
) {
2387 if (version
== RIPv2
2388 && ri
->auth_type
== RIP_AUTH_MD5
)
2389 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2390 RIP_AUTH_SIMPLE_SIZE
);
2392 ret
= rip_send_packet(STREAM_DATA(s
),
2393 stream_get_endp(s
), to
,
2396 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2397 rip_packet_dump((struct rip_packet
*)
2406 /* Flush unwritten RTE. */
2408 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2409 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2410 RIP_AUTH_SIMPLE_SIZE
);
2412 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2415 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2416 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2417 stream_get_endp(s
), "SEND");
2421 /* Statistics updates. */
2425 /* Send RIP packet to the interface. */
2426 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2429 struct interface
*ifp
= ifc
->ifp
;
2430 struct rip_interface
*ri
= ifp
->info
;
2431 struct sockaddr_in to
;
2433 /* When RIP version is 2 and multicast enable interface. */
2434 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2435 if (IS_RIP_DEBUG_EVENT
)
2436 zlog_debug("multicast announce on %s ", ifp
->name
);
2438 rip_output_process(ifc
, NULL
, route_type
, version
);
2442 /* If we can't send multicast packet, send it with unicast. */
2443 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2444 if (ifc
->address
->family
== AF_INET
) {
2445 /* Destination address and port setting. */
2446 memset(&to
, 0, sizeof(struct sockaddr_in
));
2447 if (ifc
->destination
)
2448 /* use specified broadcast or peer destination
2450 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2451 else if (ifc
->address
->prefixlen
< IPV4_MAX_PREFIXLEN
)
2452 /* calculate the appropriate broadcast address
2454 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2455 ifc
->address
->u
.prefix4
.s_addr
,
2456 ifc
->address
->prefixlen
);
2458 /* do not know where to send the packet */
2460 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2462 if (IS_RIP_DEBUG_EVENT
)
2463 zlog_debug("%s announce to %s on %s",
2464 CONNECTED_PEER(ifc
) ? "unicast"
2466 inet_ntoa(to
.sin_addr
), ifp
->name
);
2468 rip_output_process(ifc
, &to
, route_type
, version
);
2473 /* Update send to all interface and neighbor. */
2474 static void rip_update_process(struct rip
*rip
, int route_type
)
2476 struct listnode
*ifnode
, *ifnnode
;
2477 struct connected
*connected
;
2478 struct interface
*ifp
;
2479 struct rip_interface
*ri
;
2480 struct route_node
*rp
;
2481 struct sockaddr_in to
;
2484 /* Send RIP update to each interface. */
2485 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2486 if (if_is_loopback(ifp
))
2489 if (!if_is_operative(ifp
))
2492 /* Fetch RIP interface information. */
2495 /* When passive interface is specified, suppress announce to the
2502 * If there is no version configuration in the
2504 * use rip's version setting.
2506 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2510 if (IS_RIP_DEBUG_EVENT
)
2511 zlog_debug("SEND UPDATE to %s ifindex %d",
2512 ifp
->name
, ifp
->ifindex
);
2514 /* send update on each connected network */
2515 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2517 if (connected
->address
->family
== AF_INET
) {
2519 rip_update_interface(
2523 && if_is_multicast(ifp
))
2524 rip_update_interface(
2532 /* RIP send updates to each neighbor. */
2533 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2534 if (rp
->info
!= NULL
) {
2537 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2541 "Neighbor %s doesn't have connected interface!",
2542 inet_ntoa(p
->u
.prefix4
));
2546 /* Set destination address and port */
2547 memset(&to
, 0, sizeof(struct sockaddr_in
));
2548 to
.sin_addr
= p
->u
.prefix4
;
2549 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2551 /* RIP version is rip's configuration. */
2552 rip_output_process(connected
, &to
, route_type
,
2557 /* RIP's periodical timer. */
2558 static int rip_update(struct thread
*t
)
2560 struct rip
*rip
= THREAD_ARG(t
);
2562 /* Clear timer pointer. */
2563 rip
->t_update
= NULL
;
2565 if (IS_RIP_DEBUG_EVENT
)
2566 zlog_debug("update timer fire!");
2568 /* Process update output. */
2569 rip_update_process(rip
, rip_all_route
);
2571 /* Triggered updates may be suppressed if a regular update is due by
2572 the time the triggered update would be sent. */
2573 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2576 /* Register myself. */
2577 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2582 /* Walk down the RIP routing table then clear changed flag. */
2583 static void rip_clear_changed_flag(struct rip
*rip
)
2585 struct route_node
*rp
;
2586 struct rip_info
*rinfo
= NULL
;
2587 struct list
*list
= NULL
;
2588 struct listnode
*listnode
= NULL
;
2590 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2591 if ((list
= rp
->info
) != NULL
)
2592 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2593 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2594 /* This flag can be set only on the first entry.
2600 /* Triggered update interval timer. */
2601 static int rip_triggered_interval(struct thread
*t
)
2603 struct rip
*rip
= THREAD_ARG(t
);
2605 rip
->t_triggered_interval
= NULL
;
2609 rip_triggered_update(t
);
2614 /* Execute triggered update. */
2615 static int rip_triggered_update(struct thread
*t
)
2617 struct rip
*rip
= THREAD_ARG(t
);
2620 /* Clear thred pointer. */
2621 rip
->t_triggered_update
= NULL
;
2623 /* Cancel interval timer. */
2624 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2627 /* Logging triggered update. */
2628 if (IS_RIP_DEBUG_EVENT
)
2629 zlog_debug("triggered update!");
2631 /* Split Horizon processing is done when generating triggered
2632 updates as well as normal updates (see section 2.6). */
2633 rip_update_process(rip
, rip_changed_route
);
2635 /* Once all of the triggered updates have been generated, the route
2636 change flags should be cleared. */
2637 rip_clear_changed_flag(rip
);
2639 /* After a triggered update is sent, a timer should be set for a
2640 random interval between 1 and 5 seconds. If other changes that
2641 would trigger updates occur before the timer expires, a single
2642 update is triggered when the timer expires. */
2643 interval
= (random() % 5) + 1;
2645 rip
->t_triggered_interval
= NULL
;
2646 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2647 &rip
->t_triggered_interval
);
2652 /* Withdraw redistributed route. */
2653 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2655 struct route_node
*rp
;
2656 struct rip_info
*rinfo
= NULL
;
2657 struct list
*list
= NULL
;
2659 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2660 if ((list
= rp
->info
) != NULL
) {
2661 rinfo
= listgetdata(listhead(list
));
2662 if (rinfo
->type
== type
2663 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2664 /* Perform poisoned reverse. */
2665 rinfo
->metric
= RIP_METRIC_INFINITY
;
2666 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2667 rip_garbage_collect
,
2669 RIP_TIMER_OFF(rinfo
->t_timeout
);
2670 rinfo
->flags
|= RIP_RTF_CHANGED
;
2672 if (IS_RIP_DEBUG_EVENT
) {
2673 struct prefix_ipv4
*p
=
2674 (struct prefix_ipv4
*)&rp
->p
;
2677 "Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2678 inet_ntoa(p
->prefix
),
2685 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2690 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2694 vrf
= vrf_lookup_by_id(vrf_id
);
2701 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2705 rip
.vrf_name
= (char *)vrf_name
;
2707 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2710 /* Create new RIP instance and set it to global variable. */
2711 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2715 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2716 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2718 /* Set initial value. */
2719 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2720 rip
->default_metric
=
2721 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2723 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2724 rip
->passive_default
=
2725 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2726 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2728 rip
->timeout_time
= yang_get_default_uint32(
2729 "%s/timers/holddown-interval", RIP_INSTANCE
);
2730 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2733 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2735 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2737 /* Initialize RIP data structures. */
2738 rip
->table
= route_table_init();
2739 route_table_set_info(rip
->table
, rip
);
2740 rip
->neighbor
= route_table_init();
2741 rip
->peer_list
= list_new();
2742 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2743 rip
->peer_list
->del
= rip_peer_list_del
;
2744 rip
->distance_table
= route_table_init();
2745 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2746 rip
->enable_interface
= vector_init(1);
2747 rip
->enable_network
= route_table_init();
2748 rip
->passive_nondefault
= vector_init(1);
2749 rip
->offset_list_master
= list_new();
2750 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2751 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2753 /* Distribute list install. */
2754 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2755 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2756 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2758 /* if rmap install. */
2759 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2760 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2761 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2763 /* Make output stream. */
2764 rip
->obuf
= stream_new(1500);
2766 /* Enable the routing instance if possible. */
2767 if (vrf
&& vrf_is_enabled(vrf
))
2768 rip_instance_enable(rip
, vrf
, socket
);
2774 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2779 /* Sned RIP request to the destination. */
2780 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2781 uint8_t version
, struct connected
*connected
)
2784 struct rip_packet rip_packet
;
2785 struct listnode
*node
, *nnode
;
2787 memset(&rip_packet
, 0, sizeof(rip_packet
));
2789 rip_packet
.command
= RIP_REQUEST
;
2790 rip_packet
.version
= version
;
2791 rte
= rip_packet
.rte
;
2792 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2796 * connected is only sent for ripv1 case, or when
2797 * interface does not support multicast. Caller loops
2798 * over each connected address for this case.
2800 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2802 != sizeof(rip_packet
))
2805 return sizeof(rip_packet
);
2808 /* send request on each connected network */
2809 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2810 struct prefix_ipv4
*p
;
2812 p
= (struct prefix_ipv4
*)connected
->address
;
2814 if (p
->family
!= AF_INET
)
2817 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2819 != sizeof(rip_packet
))
2822 return sizeof(rip_packet
);
2825 static int rip_update_jitter(unsigned long time
)
2827 #define JITTER_BOUND 4
2828 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2829 Given that, we cannot let time be less than JITTER_BOUND seconds.
2830 The RIPv2 RFC says jitter should be small compared to
2831 update_time. We consider 1/JITTER_BOUND to be small.
2834 int jitter_input
= time
;
2837 if (jitter_input
< JITTER_BOUND
)
2838 jitter_input
= JITTER_BOUND
;
2840 jitter
= (((random() % ((jitter_input
* 2) + 1)) - jitter_input
));
2842 return jitter
/ JITTER_BOUND
;
2845 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2852 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2854 case RIP_UPDATE_EVENT
:
2855 RIP_TIMER_OFF(rip
->t_update
);
2856 jitter
= rip_update_jitter(rip
->update_time
);
2857 thread_add_timer(master
, rip_update
, rip
,
2858 sock
? 2 : rip
->update_time
+ jitter
,
2861 case RIP_TRIGGERED_UPDATE
:
2862 if (rip
->t_triggered_interval
)
2865 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2866 &rip
->t_triggered_update
);
2875 rip_update_default_metric (void)
2877 struct route_node
*np
;
2878 struct rip_info
*rinfo
= NULL
;
2879 struct list
*list
= NULL
;
2880 struct listnode
*listnode
= NULL
;
2882 for (np
= route_top (rip
->table
); np
; np
= route_next (np
))
2883 if ((list
= np
->info
) != NULL
)
2884 for (ALL_LIST_ELEMENTS_RO (list
, listnode
, rinfo
))
2885 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&& rinfo
->type
!= ZEBRA_ROUTE_CONNECT
)
2886 rinfo
->metric
= rip
->default_metric
;
2890 struct rip_distance
*rip_distance_new(void)
2892 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2895 void rip_distance_free(struct rip_distance
*rdistance
)
2897 if (rdistance
->access_list
)
2898 free(rdistance
->access_list
);
2899 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2902 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2903 struct route_node
*node
)
2905 struct rip_distance
*rdistance
;
2907 rdistance
= node
->info
;
2909 rip_distance_free(rdistance
);
2912 /* Apply RIP information to distance method. */
2913 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2915 struct route_node
*rn
;
2916 struct prefix_ipv4 p
;
2917 struct rip_distance
*rdistance
;
2918 struct access_list
*alist
;
2920 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2922 p
.prefix
= rinfo
->from
;
2923 p
.prefixlen
= IPV4_MAX_BITLEN
;
2925 /* Check source address. */
2926 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2928 rdistance
= rn
->info
;
2929 route_unlock_node(rn
);
2931 if (rdistance
->access_list
) {
2932 alist
= access_list_lookup(AFI_IP
,
2933 rdistance
->access_list
);
2936 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2940 return rdistance
->distance
;
2942 return rdistance
->distance
;
2946 return rip
->distance
;
2951 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2953 struct route_node
*rn
;
2954 struct rip_distance
*rdistance
;
2958 vty_out(vty
, " Distance: (default is %u)\n",
2959 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2961 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2962 if ((rdistance
= rn
->info
) != NULL
) {
2965 " Address Distance List\n");
2968 sprintf(buf
, "%s/%d", inet_ntoa(rn
->p
.u
.prefix4
),
2970 vty_out(vty
, " %-20s %4d %s\n", buf
,
2971 rdistance
->distance
,
2972 rdistance
->access_list
? rdistance
->access_list
2977 /* Update ECMP routes to zebra when ECMP is disabled. */
2978 void rip_ecmp_disable(struct rip
*rip
)
2980 struct route_node
*rp
;
2981 struct rip_info
*rinfo
, *tmp_rinfo
;
2983 struct listnode
*node
, *nextnode
;
2985 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2986 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2987 rinfo
= listgetdata(listhead(list
));
2988 if (!rip_route_rte(rinfo
))
2991 /* Drop all other entries, except the first one. */
2992 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
2993 if (tmp_rinfo
!= rinfo
) {
2994 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
2996 tmp_rinfo
->t_garbage_collect
);
2997 list_delete_node(list
, node
);
2998 rip_info_free(tmp_rinfo
);
3002 rip_zebra_ipv4_add(rip
, rp
);
3004 /* Set the route change flag. */
3005 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
3007 /* Signal the output process to trigger an update. */
3008 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
3012 /* Print out routes update time. */
3013 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
3018 char timebuf
[TIME_BUF
];
3019 struct thread
*thread
;
3021 if ((thread
= rinfo
->t_timeout
) != NULL
) {
3022 clock
= thread_timer_remain_second(thread
);
3023 tm
= gmtime(&clock
);
3024 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3025 vty_out(vty
, "%5s", timebuf
);
3026 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
3027 clock
= thread_timer_remain_second(thread
);
3028 tm
= gmtime(&clock
);
3029 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3030 vty_out(vty
, "%5s", timebuf
);
3034 static const char *rip_route_type_print(int sub_type
)
3039 case RIP_ROUTE_STATIC
:
3041 case RIP_ROUTE_DEFAULT
:
3043 case RIP_ROUTE_REDISTRIBUTE
:
3045 case RIP_ROUTE_INTERFACE
:
3054 "show ip rip [vrf NAME]",
3061 struct route_node
*np
;
3062 struct rip_info
*rinfo
= NULL
;
3063 struct list
*list
= NULL
;
3064 struct listnode
*listnode
= NULL
;
3065 const char *vrf_name
;
3068 if (argv_find(argv
, argc
, "vrf", &idx
))
3069 vrf_name
= argv
[idx
+ 1]->arg
;
3071 vrf_name
= VRF_DEFAULT_NAME
;
3073 rip
= rip_lookup_by_vrf_name(vrf_name
);
3075 vty_out(vty
, "%% RIP instance not found\n");
3078 if (!rip
->enabled
) {
3079 vty_out(vty
, "%% RIP instance is disabled\n");
3084 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3086 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3087 " (i) - interface\n\n"
3088 " Network Next Hop Metric From Tag Time\n");
3090 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3091 if ((list
= np
->info
) != NULL
)
3092 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3096 vty
, "%c(%s) %s/%d",
3097 /* np->lock, For debugging. */
3098 zebra_route_char(rinfo
->type
),
3099 rip_route_type_print(rinfo
->sub_type
),
3100 inet_ntoa(np
->p
.u
.prefix4
),
3106 vty_out(vty
, "%*s", len
, " ");
3108 switch (rinfo
->nh
.type
) {
3109 case NEXTHOP_TYPE_IPV4
:
3110 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3111 vty_out(vty
, "%-20s %2d ",
3112 inet_ntoa(rinfo
->nh
.gate
.ipv4
),
3115 case NEXTHOP_TYPE_IFINDEX
:
3120 case NEXTHOP_TYPE_BLACKHOLE
:
3125 case NEXTHOP_TYPE_IPV6
:
3126 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3128 "V6 Address Hidden %2d ",
3133 /* Route which exist in kernel routing table. */
3134 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3135 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3136 vty_out(vty
, "%-15s ",
3137 inet_ntoa(rinfo
->from
));
3138 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3139 (route_tag_t
)rinfo
->tag
);
3140 rip_vty_out_uptime(vty
, rinfo
);
3141 } else if (rinfo
->metric
3142 == RIP_METRIC_INFINITY
) {
3143 vty_out(vty
, "self ");
3144 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3145 (route_tag_t
)rinfo
->tag
);
3146 rip_vty_out_uptime(vty
, rinfo
);
3148 if (rinfo
->external_metric
) {
3150 vty
, "self (%s:%d)",
3153 rinfo
->external_metric
);
3156 vty_out(vty
, "%*s", len
,
3161 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3162 (route_tag_t
)rinfo
->tag
);
3170 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3171 DEFUN (show_ip_rip_status
,
3172 show_ip_rip_status_cmd
,
3173 "show ip rip [vrf NAME] status",
3178 "IP routing protocol process parameters and statistics\n")
3181 struct interface
*ifp
;
3182 struct rip_interface
*ri
;
3183 extern const struct message ri_version_msg
[];
3184 const char *send_version
;
3185 const char *receive_version
;
3186 const char *vrf_name
;
3189 if (argv_find(argv
, argc
, "vrf", &idx
))
3190 vrf_name
= argv
[idx
+ 1]->arg
;
3192 vrf_name
= VRF_DEFAULT_NAME
;
3194 rip
= rip_lookup_by_vrf_name(vrf_name
);
3196 vty_out(vty
, "%% RIP instance not found\n");
3199 if (!rip
->enabled
) {
3200 vty_out(vty
, "%% RIP instance is disabled\n");
3204 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3205 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3207 vty_out(vty
, " next due in %lu seconds\n",
3208 thread_timer_remain_second(rip
->t_update
));
3209 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3210 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3212 /* Filtering status show. */
3213 config_show_distribute(vty
, rip
->distribute_ctx
);
3215 /* Default metric information. */
3216 vty_out(vty
, " Default redistribution metric is %u\n",
3217 rip
->default_metric
);
3219 /* Redistribute information. */
3220 vty_out(vty
, " Redistributing:");
3221 rip_show_redistribute_config(vty
, rip
);
3224 vty_out(vty
, " Default version control: send version %s,",
3225 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3226 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3227 vty_out(vty
, " receive any version \n");
3229 vty_out(vty
, " receive version %s \n",
3230 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3232 vty_out(vty
, " Interface Send Recv Key-chain\n");
3234 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3240 if (ri
->enable_network
|| ri
->enable_interface
) {
3241 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3243 lookup_msg(ri_version_msg
,
3244 rip
->version_send
, NULL
);
3246 send_version
= lookup_msg(ri_version_msg
,
3249 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3251 lookup_msg(ri_version_msg
,
3252 rip
->version_recv
, NULL
);
3254 receive_version
= lookup_msg(
3255 ri_version_msg
, ri
->ri_receive
, NULL
);
3257 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3258 send_version
, receive_version
,
3259 ri
->key_chain
? ri
->key_chain
: "");
3263 vty_out(vty
, " Routing for Networks:\n");
3264 rip_show_network_config(vty
, rip
);
3267 int found_passive
= 0;
3268 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3271 if ((ri
->enable_network
|| ri
->enable_interface
)
3273 if (!found_passive
) {
3275 " Passive Interface(s):\n");
3278 vty_out(vty
, " %s\n", ifp
->name
);
3283 vty_out(vty
, " Routing Information Sources:\n");
3285 " Gateway BadPackets BadRoutes Distance Last Update\n");
3286 rip_peer_display(vty
, rip
);
3288 rip_distance_show(vty
, rip
);
3293 /* RIP configuration write function. */
3294 static int config_write_rip(struct vty
*vty
)
3299 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3300 char xpath
[XPATH_MAXLEN
];
3301 struct lyd_node
*dnode
;
3303 snprintf(xpath
, sizeof(xpath
),
3304 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3306 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3309 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3311 /* Distribute configuration. */
3312 config_write_distribute(vty
, rip
->distribute_ctx
);
3314 /* Interface routemap configuration */
3315 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3323 /* RIP node structure. */
3324 static struct cmd_node rip_node
= {RIP_NODE
, "%s(config-router)# ", 1};
3326 /* Distribute-list update functions. */
3327 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3328 struct distribute
*dist
)
3330 struct interface
*ifp
;
3331 struct rip_interface
*ri
;
3332 struct access_list
*alist
;
3333 struct prefix_list
*plist
;
3335 if (!ctx
->vrf
|| !dist
->ifname
)
3338 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3344 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3345 alist
= access_list_lookup(AFI_IP
,
3346 dist
->list
[DISTRIBUTE_V4_IN
]);
3348 ri
->list
[RIP_FILTER_IN
] = alist
;
3350 ri
->list
[RIP_FILTER_IN
] = NULL
;
3352 ri
->list
[RIP_FILTER_IN
] = NULL
;
3354 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3355 alist
= access_list_lookup(AFI_IP
,
3356 dist
->list
[DISTRIBUTE_V4_OUT
]);
3358 ri
->list
[RIP_FILTER_OUT
] = alist
;
3360 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3362 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3364 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3365 plist
= prefix_list_lookup(AFI_IP
,
3366 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3368 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3370 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3372 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3374 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3375 plist
= prefix_list_lookup(AFI_IP
,
3376 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3378 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3380 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3382 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3385 void rip_distribute_update_interface(struct interface
*ifp
)
3387 struct rip_interface
*ri
= ifp
->info
;
3388 struct rip
*rip
= ri
->rip
;
3389 struct distribute
*dist
;
3393 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3395 rip_distribute_update(rip
->distribute_ctx
, dist
);
3398 /* Update all interface's distribute list. */
3400 static void rip_distribute_update_all(struct prefix_list
*notused
)
3402 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3403 struct interface
*ifp
;
3405 FOR_ALL_INTERFACES (vrf
, ifp
)
3406 rip_distribute_update_interface(ifp
);
3409 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3411 rip_distribute_update_all(NULL
);
3414 /* Delete all added rip route. */
3415 void rip_clean(struct rip
*rip
)
3418 rip_instance_disable(rip
);
3420 stream_free(rip
->obuf
);
3422 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3423 if (rip
->redist
[i
].route_map
.name
)
3424 free(rip
->redist
[i
].route_map
.name
);
3426 route_table_finish(rip
->table
);
3427 route_table_finish(rip
->neighbor
);
3428 list_delete(&rip
->peer_list
);
3429 distribute_list_delete(&rip
->distribute_ctx
);
3430 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3432 rip_clean_network(rip
);
3433 rip_passive_nondefault_clean(rip
);
3434 vector_free(rip
->enable_interface
);
3435 route_table_finish(rip
->enable_network
);
3436 vector_free(rip
->passive_nondefault
);
3437 list_delete(&rip
->offset_list_master
);
3438 rip_interfaces_clean(rip
);
3439 route_table_finish(rip
->distance_table
);
3441 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3442 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3443 XFREE(MTYPE_RIP
, rip
);
3446 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3447 struct if_rmap
*if_rmap
)
3449 struct interface
*ifp
= NULL
;
3450 struct rip_interface
*ri
;
3451 struct route_map
*rmap
;
3452 struct vrf
*vrf
= NULL
;
3455 vrf
= vrf_lookup_by_name(ctx
->name
);
3457 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3462 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3463 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3465 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3467 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3469 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3471 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3472 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3474 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3476 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3478 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3481 void rip_if_rmap_update_interface(struct interface
*ifp
)
3483 struct rip_interface
*ri
= ifp
->info
;
3484 struct rip
*rip
= ri
->rip
;
3485 struct if_rmap
*if_rmap
;
3486 struct if_rmap_ctx
*ctx
;
3490 ctx
= rip
->if_rmap_ctx
;
3493 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3495 rip_if_rmap_update(ctx
, if_rmap
);
3498 static void rip_routemap_update_redistribute(struct rip
*rip
)
3500 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3501 if (rip
->redist
[i
].route_map
.name
) {
3502 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3503 rip
->redist
[i
].route_map
.name
);
3504 route_map_counter_increment(
3505 rip
->redist
[i
].route_map
.map
);
3511 static void rip_routemap_update(const char *notused
)
3513 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3515 struct interface
*ifp
;
3517 FOR_ALL_INTERFACES (vrf
, ifp
)
3518 rip_if_rmap_update_interface(ifp
);
3522 rip_routemap_update_redistribute(rip
);
3525 /* Link RIP instance to VRF. */
3526 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3528 struct interface
*ifp
;
3531 rip
->distribute_ctx
->vrf
= vrf
;
3534 FOR_ALL_INTERFACES (vrf
, ifp
)
3535 rip_interface_sync(ifp
);
3538 /* Unlink RIP instance from VRF. */
3539 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3541 struct interface
*ifp
;
3544 rip
->distribute_ctx
->vrf
= NULL
;
3547 FOR_ALL_INTERFACES (vrf
, ifp
)
3548 rip_interface_sync(ifp
);
3551 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3555 rip_vrf_link(rip
, vrf
);
3556 rip
->enabled
= true;
3558 /* Resend all redistribute requests. */
3559 rip_redistribute_enable(rip
);
3561 /* Create read and timer thread. */
3562 rip_event(rip
, RIP_READ
, rip
->sock
);
3563 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3565 rip_zebra_vrf_register(vrf
);
3568 static void rip_instance_disable(struct rip
*rip
)
3570 struct vrf
*vrf
= rip
->vrf
;
3571 struct route_node
*rp
;
3573 /* Clear RIP routes */
3574 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3575 struct rip_info
*rinfo
;
3577 struct listnode
*listnode
;
3579 if ((list
= rp
->info
) == NULL
)
3582 rinfo
= listgetdata(listhead(list
));
3583 if (rip_route_rte(rinfo
))
3584 rip_zebra_ipv4_delete(rip
, rp
);
3586 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3587 RIP_TIMER_OFF(rinfo
->t_timeout
);
3588 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3589 rip_info_free(rinfo
);
3593 route_unlock_node(rp
);
3596 /* Flush all redistribute requests. */
3597 rip_redistribute_disable(rip
);
3599 /* Cancel RIP related timers. */
3600 RIP_TIMER_OFF(rip
->t_update
);
3601 RIP_TIMER_OFF(rip
->t_triggered_update
);
3602 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3604 /* Cancel read thread. */
3605 THREAD_READ_OFF(rip
->t_read
);
3607 /* Close RIP socket. */
3611 /* Clear existing peers. */
3612 list_delete_all_node(rip
->peer_list
);
3614 rip_zebra_vrf_deregister(vrf
);
3616 rip_vrf_unlink(rip
, vrf
);
3617 rip
->enabled
= false;
3620 static int rip_vrf_new(struct vrf
*vrf
)
3622 if (IS_RIP_DEBUG_EVENT
)
3623 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3629 static int rip_vrf_delete(struct vrf
*vrf
)
3631 if (IS_RIP_DEBUG_EVENT
)
3632 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3638 static int rip_vrf_enable(struct vrf
*vrf
)
3643 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3645 char *old_vrf_name
= NULL
;
3647 rip
= (struct rip
*)vrf
->info
;
3650 /* update vrf name */
3652 old_vrf_name
= rip
->vrf_name
;
3653 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf
->name
);
3655 * HACK: Change the RIP VRF in the running configuration directly,
3656 * bypassing the northbound layer. This is necessary to avoid deleting
3657 * the RIP and readding it in the new VRF, which would have
3658 * several implications.
3660 if (yang_module_find("frr-ripd") && old_vrf_name
) {
3661 struct lyd_node
*rip_dnode
;
3663 rip_dnode
= yang_dnode_get(
3664 running_config
->dnode
,
3665 "/frr-ripd:ripd/instance[vrf='%s']/vrf",
3668 yang_dnode_change_leaf(rip_dnode
, vrf
->name
);
3669 running_config
->version
++;
3673 XFREE(MTYPE_RIP_VRF_NAME
, old_vrf_name
);
3675 if (!rip
|| rip
->enabled
)
3678 if (IS_RIP_DEBUG_EVENT
)
3679 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3682 /* Activate the VRF RIP instance. */
3683 if (!rip
->enabled
) {
3684 socket
= rip_create_socket(vrf
);
3688 rip_instance_enable(rip
, vrf
, socket
);
3694 static int rip_vrf_disable(struct vrf
*vrf
)
3698 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3699 if (!rip
|| !rip
->enabled
)
3702 if (IS_RIP_DEBUG_EVENT
)
3703 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3706 /* Deactivate the VRF RIP instance. */
3708 rip_instance_disable(rip
);
3713 void rip_vrf_init(void)
3715 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3719 void rip_vrf_terminate(void)
3724 /* Allocate new rip structure and set default value. */
3727 /* Install top nodes. */
3728 install_node(&rip_node
, config_write_rip
);
3730 /* Install rip commands. */
3731 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3732 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3734 install_default(RIP_NODE
);
3736 /* Debug related init. */
3739 /* Access list install. */
3741 access_list_add_hook(rip_distribute_update_all_wrapper
);
3742 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3744 /* Prefix list initialize.*/
3746 prefix_list_add_hook(rip_distribute_update_all
);
3747 prefix_list_delete_hook(rip_distribute_update_all
);
3749 /* Distribute list install. */
3750 distribute_list_init(RIP_NODE
);
3753 rip_route_map_init();
3755 route_map_add_hook(rip_routemap_update
);
3756 route_map_delete_hook(rip_routemap_update
);
3758 if_rmap_init(RIP_NODE
);