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"
43 #include "lib_errors.h"
44 #include "northbound_cli.h"
46 #include "ripd/ripd.h"
47 #include "ripd/rip_debug.h"
48 #include "ripd/rip_errors.h"
49 #include "ripd/rip_interface.h"
51 /* UDP receive buffer size */
52 #define RIP_UDP_RCV_BUF 41600
54 DEFINE_MGROUP(RIPD
, "ripd")
55 DEFINE_MTYPE_STATIC(RIPD
, RIP
, "RIP structure")
56 DEFINE_MTYPE_STATIC(RIPD
, RIP_VRF_NAME
, "RIP VRF name")
57 DEFINE_MTYPE_STATIC(RIPD
, RIP_INFO
, "RIP route info")
58 DEFINE_MTYPE_STATIC(RIPD
, RIP_DISTANCE
, "RIP distance")
61 static void rip_output_process(struct connected
*, struct sockaddr_in
*, int,
63 static int rip_triggered_update(struct thread
*);
64 static int rip_update_jitter(unsigned long);
65 static void rip_distance_table_node_cleanup(struct route_table
*table
,
66 struct route_node
*node
);
67 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
);
68 static void rip_instance_disable(struct rip
*rip
);
70 static void rip_distribute_update(struct distribute_ctx
*ctx
,
71 struct distribute
*dist
);
73 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
74 struct if_rmap
*if_rmap
);
76 /* RIP output routes type. */
77 enum { rip_all_route
, rip_changed_route
};
79 /* RIP command strings. */
80 static const struct message rip_msg
[] = {{RIP_REQUEST
, "REQUEST"},
81 {RIP_RESPONSE
, "RESPONSE"},
82 {RIP_TRACEON
, "TRACEON"},
83 {RIP_TRACEOFF
, "TRACEOFF"},
85 {RIP_POLL_ENTRY
, "POLL ENTRY"},
88 /* Generate rb-tree of RIP instances. */
89 static inline int rip_instance_compare(const struct rip
*a
, const struct rip
*b
)
91 return strcmp(a
->vrf_name
, b
->vrf_name
);
93 RB_GENERATE(rip_instance_head
, rip
, entry
, rip_instance_compare
)
95 struct rip_instance_head rip_instances
= RB_INITIALIZER(&rip_instances
);
97 /* Utility function to set boradcast option to the socket. */
98 static int sockopt_broadcast(int sock
)
103 ret
= setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
106 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock
);
112 int rip_route_rte(struct rip_info
*rinfo
)
114 return (rinfo
->type
== ZEBRA_ROUTE_RIP
115 && rinfo
->sub_type
== RIP_ROUTE_RTE
);
118 static struct rip_info
*rip_info_new(void)
120 return XCALLOC(MTYPE_RIP_INFO
, sizeof(struct rip_info
));
123 void rip_info_free(struct rip_info
*rinfo
)
125 XFREE(MTYPE_RIP_INFO
, rinfo
);
128 struct rip
*rip_info_get_instance(const struct rip_info
*rinfo
)
130 return route_table_get_info(rinfo
->rp
->table
);
133 /* RIP route garbage collect timer. */
134 static int rip_garbage_collect(struct thread
*t
)
136 struct rip_info
*rinfo
;
137 struct route_node
*rp
;
139 rinfo
= THREAD_ARG(t
);
140 rinfo
->t_garbage_collect
= NULL
;
142 /* Off timeout timer. */
143 RIP_TIMER_OFF(rinfo
->t_timeout
);
145 /* Get route_node pointer. */
148 /* Unlock route_node. */
149 listnode_delete(rp
->info
, rinfo
);
150 if (list_isempty((struct list
*)rp
->info
)) {
151 list_delete((struct list
**)&rp
->info
);
152 route_unlock_node(rp
);
155 /* Free RIP routing information. */
156 rip_info_free(rinfo
);
161 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
);
163 /* Add new route to the ECMP list.
164 * RETURN: the new entry added in the list, or NULL if it is not the first
165 * entry and ECMP is not allowed.
167 struct rip_info
*rip_ecmp_add(struct rip
*rip
, struct rip_info
*rinfo_new
)
169 struct route_node
*rp
= rinfo_new
->rp
;
170 struct rip_info
*rinfo
= NULL
;
171 struct list
*list
= NULL
;
173 if (rp
->info
== NULL
)
174 rp
->info
= list_new();
175 list
= (struct list
*)rp
->info
;
177 /* If ECMP is not allowed and some entry already exists in the list,
179 if (listcount(list
) && !rip
->ecmp
)
182 rinfo
= rip_info_new();
183 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
184 listnode_add(list
, rinfo
);
186 if (rip_route_rte(rinfo
)) {
187 rip_timeout_update(rip
, rinfo
);
188 rip_zebra_ipv4_add(rip
, rp
);
191 /* Set the route change flag on the first entry. */
192 rinfo
= listgetdata(listhead(list
));
193 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
195 /* Signal the output process to trigger an update (see section 2.5). */
196 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
201 /* Replace the ECMP list with the new route.
202 * RETURN: the new entry added in the list
204 struct rip_info
*rip_ecmp_replace(struct rip
*rip
, struct rip_info
*rinfo_new
)
206 struct route_node
*rp
= rinfo_new
->rp
;
207 struct list
*list
= (struct list
*)rp
->info
;
208 struct rip_info
*rinfo
= NULL
, *tmp_rinfo
= NULL
;
209 struct listnode
*node
= NULL
, *nextnode
= NULL
;
211 if (list
== NULL
|| listcount(list
) == 0)
212 return rip_ecmp_add(rip
, rinfo_new
);
214 /* Get the first entry */
215 rinfo
= listgetdata(listhead(list
));
217 /* Learnt route replaced by a local one. Delete it from zebra. */
218 if (rip_route_rte(rinfo
) && !rip_route_rte(rinfo_new
))
219 if (CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
220 rip_zebra_ipv4_delete(rip
, rp
);
222 /* Re-use the first entry, and delete the others. */
223 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
224 if (tmp_rinfo
!= rinfo
) {
225 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
226 RIP_TIMER_OFF(tmp_rinfo
->t_garbage_collect
);
227 list_delete_node(list
, node
);
228 rip_info_free(tmp_rinfo
);
231 RIP_TIMER_OFF(rinfo
->t_timeout
);
232 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
233 memcpy(rinfo
, rinfo_new
, sizeof(struct rip_info
));
235 if (rip_route_rte(rinfo
)) {
236 rip_timeout_update(rip
, rinfo
);
237 /* The ADD message implies an update. */
238 rip_zebra_ipv4_add(rip
, rp
);
241 /* Set the route change flag. */
242 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
244 /* Signal the output process to trigger an update (see section 2.5). */
245 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
250 /* Delete one route from the ECMP list.
252 * null - the entry is freed, and other entries exist in the list
253 * the entry - the entry is the last one in the list; its metric is set
254 * to INFINITY, and the garbage collector is started for it
256 struct rip_info
*rip_ecmp_delete(struct rip
*rip
, struct rip_info
*rinfo
)
258 struct route_node
*rp
= rinfo
->rp
;
259 struct list
*list
= (struct list
*)rp
->info
;
261 RIP_TIMER_OFF(rinfo
->t_timeout
);
263 if (listcount(list
) > 1) {
264 /* Some other ECMP entries still exist. Just delete this entry.
266 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
267 listnode_delete(list
, rinfo
);
268 if (rip_route_rte(rinfo
)
269 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
270 /* The ADD message implies the update. */
271 rip_zebra_ipv4_add(rip
, rp
);
272 rip_info_free(rinfo
);
275 assert(rinfo
== listgetdata(listhead(list
)));
277 /* This is the only entry left in the list. We must keep it in
278 * the list for garbage collection time, with INFINITY metric.
281 rinfo
->metric
= RIP_METRIC_INFINITY
;
282 RIP_TIMER_ON(rinfo
->t_garbage_collect
, rip_garbage_collect
,
285 if (rip_route_rte(rinfo
)
286 && CHECK_FLAG(rinfo
->flags
, RIP_RTF_FIB
))
287 rip_zebra_ipv4_delete(rip
, rp
);
290 /* Set the route change flag on the first entry. */
291 rinfo
= listgetdata(listhead(list
));
292 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
294 /* Signal the output process to trigger an update (see section 2.5). */
295 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
300 /* Timeout RIP routes. */
301 static int rip_timeout(struct thread
*t
)
303 struct rip_info
*rinfo
= THREAD_ARG(t
);
304 struct rip
*rip
= rip_info_get_instance(rinfo
);
306 rip_ecmp_delete(rip
, rinfo
);
311 static void rip_timeout_update(struct rip
*rip
, struct rip_info
*rinfo
)
313 if (rinfo
->metric
!= RIP_METRIC_INFINITY
) {
314 RIP_TIMER_OFF(rinfo
->t_timeout
);
315 thread_add_timer(master
, rip_timeout
, rinfo
, rip
->timeout_time
,
320 static int rip_filter(int rip_distribute
, struct prefix_ipv4
*p
,
321 struct rip_interface
*ri
)
323 struct distribute
*dist
;
324 struct access_list
*alist
;
325 struct prefix_list
*plist
;
326 int distribute
= rip_distribute
== RIP_FILTER_OUT
? DISTRIBUTE_V4_OUT
328 const char *inout
= rip_distribute
== RIP_FILTER_OUT
? "out" : "in";
330 /* Input distribute-list filtering. */
331 if (ri
->list
[rip_distribute
]) {
332 if (access_list_apply(ri
->list
[rip_distribute
],
335 if (IS_RIP_DEBUG_PACKET
)
336 zlog_debug("%s/%d filtered by distribute %s",
337 inet_ntoa(p
->prefix
), p
->prefixlen
,
342 if (ri
->prefix
[rip_distribute
]) {
343 if (prefix_list_apply(ri
->prefix
[rip_distribute
],
346 if (IS_RIP_DEBUG_PACKET
)
347 zlog_debug("%s/%d filtered by prefix-list %s",
348 inet_ntoa(p
->prefix
), p
->prefixlen
,
354 /* All interface filter check. */
355 dist
= distribute_lookup(ri
->rip
->distribute_ctx
, NULL
);
357 if (dist
->list
[distribute
]) {
358 alist
= access_list_lookup(AFI_IP
,
359 dist
->list
[distribute
]);
362 if (access_list_apply(alist
, (struct prefix
*)p
)
364 if (IS_RIP_DEBUG_PACKET
)
366 "%s/%d filtered by distribute %s",
367 inet_ntoa(p
->prefix
),
368 p
->prefixlen
, inout
);
373 if (dist
->prefix
[distribute
]) {
374 plist
= prefix_list_lookup(AFI_IP
,
375 dist
->prefix
[distribute
]);
378 if (prefix_list_apply(plist
, (struct prefix
*)p
)
380 if (IS_RIP_DEBUG_PACKET
)
382 "%s/%d filtered by prefix-list %s",
383 inet_ntoa(p
->prefix
),
384 p
->prefixlen
, inout
);
393 /* Check nexthop address validity. */
394 static int rip_nexthop_check(struct rip
*rip
, struct in_addr
*addr
)
396 struct interface
*ifp
;
397 struct listnode
*cnode
;
398 struct connected
*ifc
;
401 /* If nexthop address matches local configured address then it is
404 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
405 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, ifc
)) {
408 if (p
->family
== AF_INET
409 && IPV4_ADDR_SAME(&p
->u
.prefix4
, addr
))
416 /* RIP add route to routing table. */
417 static void rip_rte_process(struct rte
*rte
, struct sockaddr_in
*from
,
418 struct interface
*ifp
)
422 struct prefix_ipv4 p
;
423 struct route_node
*rp
;
424 struct rip_info
*rinfo
= NULL
, newinfo
;
425 struct rip_interface
*ri
;
426 struct in_addr
*nexthop
;
428 unsigned char old_dist
, new_dist
;
429 struct list
*list
= NULL
;
430 struct listnode
*node
= NULL
;
432 /* Make prefix structure. */
433 memset(&p
, 0, sizeof(struct prefix_ipv4
));
435 p
.prefix
= rte
->prefix
;
436 p
.prefixlen
= ip_masklen(rte
->mask
);
438 /* Make sure mask is applied. */
444 /* Apply input filters. */
445 ret
= rip_filter(RIP_FILTER_IN
, &p
, ri
);
449 memset(&newinfo
, 0, sizeof(newinfo
));
450 newinfo
.type
= ZEBRA_ROUTE_RIP
;
451 newinfo
.sub_type
= RIP_ROUTE_RTE
;
452 newinfo
.nh
.gate
.ipv4
= rte
->nexthop
;
453 newinfo
.from
= from
->sin_addr
;
454 newinfo
.nh
.ifindex
= ifp
->ifindex
;
455 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
456 newinfo
.metric
= rte
->metric
;
457 newinfo
.metric_out
= rte
->metric
; /* XXX */
458 newinfo
.tag
= ntohs(rte
->tag
); /* XXX */
460 /* Modify entry according to the interface routemap. */
461 if (ri
->routemap
[RIP_FILTER_IN
]) {
462 /* The object should be of the type of rip_info */
463 ret
= route_map_apply(ri
->routemap
[RIP_FILTER_IN
],
464 (struct prefix
*)&p
, RMAP_RIP
, &newinfo
);
466 if (ret
== RMAP_DENYMATCH
) {
467 if (IS_RIP_DEBUG_PACKET
)
469 "RIP %s/%d is filtered by route-map in",
470 inet_ntoa(p
.prefix
), p
.prefixlen
);
474 /* Get back the object */
475 rte
->nexthop
= newinfo
.nexthop_out
;
476 rte
->tag
= htons(newinfo
.tag_out
); /* XXX */
477 rte
->metric
= newinfo
.metric_out
; /* XXX: the routemap uses the
481 /* Once the entry has been validated, update the metric by
482 adding the cost of the network on wich the message
483 arrived. If the result is greater than infinity, use infinity
484 (RFC2453 Sec. 3.9.2) */
485 /* Zebra ripd can handle offset-list in. */
486 ret
= rip_offset_list_apply_in(&p
, ifp
, &rte
->metric
);
488 /* If offset-list does not modify the metric use interface's
491 rte
->metric
+= ifp
->metric
? ifp
->metric
: 1;
493 if (rte
->metric
> RIP_METRIC_INFINITY
)
494 rte
->metric
= RIP_METRIC_INFINITY
;
496 /* Set nexthop pointer. */
497 if (rte
->nexthop
.s_addr
== 0)
498 nexthop
= &from
->sin_addr
;
500 nexthop
= &rte
->nexthop
;
502 /* Check if nexthop address is myself, then do nothing. */
503 if (rip_nexthop_check(rip
, nexthop
) < 0) {
504 if (IS_RIP_DEBUG_PACKET
)
505 zlog_debug("Nexthop address %s is myself",
506 inet_ntoa(*nexthop
));
510 /* Get index for the prefix. */
511 rp
= route_node_get(rip
->table
, (struct prefix
*)&p
);
514 newinfo
.nh
.gate
.ipv4
= *nexthop
;
515 newinfo
.nh
.type
= NEXTHOP_TYPE_IPV4
;
516 newinfo
.metric
= rte
->metric
;
517 newinfo
.tag
= ntohs(rte
->tag
);
518 newinfo
.distance
= rip_distance_apply(rip
, &newinfo
);
520 new_dist
= newinfo
.distance
? newinfo
.distance
521 : ZEBRA_RIP_DISTANCE_DEFAULT
;
523 /* Check to see whether there is already RIP route on the table. */
524 if ((list
= rp
->info
) != NULL
)
525 for (ALL_LIST_ELEMENTS_RO(list
, node
, rinfo
)) {
526 /* Need to compare with redistributed entry or local
528 if (!rip_route_rte(rinfo
))
531 if (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
532 && IPV4_ADDR_SAME(&rinfo
->nh
.gate
.ipv4
, nexthop
))
535 if (!listnextnode(node
)) {
536 /* Not found in the list */
538 if (rte
->metric
> rinfo
->metric
) {
539 /* New route has a greater metric.
541 route_unlock_node(rp
);
545 if (rte
->metric
< rinfo
->metric
)
546 /* New route has a smaller metric.
547 * Replace the ECMP list
548 * with the new one in below. */
551 /* Metrics are same. We compare the distances.
553 old_dist
= rinfo
->distance
555 : ZEBRA_RIP_DISTANCE_DEFAULT
;
557 if (new_dist
> old_dist
) {
558 /* New route has a greater distance.
560 route_unlock_node(rp
);
564 if (new_dist
< old_dist
)
565 /* New route has a smaller distance.
566 * Replace the ECMP list
567 * with the new one in below. */
570 /* Metrics and distances are both same. Keep
572 * the new route is added in the ECMP list in
578 /* Local static route. */
579 if (rinfo
->type
== ZEBRA_ROUTE_RIP
580 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
581 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))
582 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
583 route_unlock_node(rp
);
587 /* Redistributed route check. */
588 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
589 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
590 old_dist
= rinfo
->distance
;
591 /* Only routes directly connected to an interface
593 * may have a valid NULL distance */
594 if (rinfo
->nh
.gate
.ipv4
.s_addr
!= 0)
597 : ZEBRA_RIP_DISTANCE_DEFAULT
;
598 /* If imported route does not have STRICT precedence,
599 mark it as a ghost */
600 if (new_dist
<= old_dist
601 && rte
->metric
!= RIP_METRIC_INFINITY
)
602 rip_ecmp_replace(rip
, &newinfo
);
604 route_unlock_node(rp
);
611 route_unlock_node(rp
);
613 /* Now, check to see whether there is already an explicit route
614 for the destination prefix. If there is no such route, add
615 this route to the routing table, unless the metric is
616 infinity (there is no point in adding a route which
618 if (rte
->metric
!= RIP_METRIC_INFINITY
)
619 rip_ecmp_add(rip
, &newinfo
);
621 /* Route is there but we are not sure the route is RIP or not.
624 /* If there is an existing route, compare the next hop address
625 to the address of the router from which the datagram came.
626 If this datagram is from the same router as the existing
627 route, reinitialize the timeout. */
628 same
= (IPV4_ADDR_SAME(&rinfo
->from
, &from
->sin_addr
)
629 && (rinfo
->nh
.ifindex
== ifp
->ifindex
));
631 old_dist
= rinfo
->distance
? rinfo
->distance
632 : ZEBRA_RIP_DISTANCE_DEFAULT
;
634 /* Next, compare the metrics. If the datagram is from the same
635 router as the existing route, and the new metric is different
636 than the old one; or, if the new metric is lower than the old
637 one, or if the tag has been changed; or if there is a route
638 with a lower administrave distance; or an update of the
639 distance on the actual route; do the following actions: */
640 if ((same
&& rinfo
->metric
!= rte
->metric
)
641 || (rte
->metric
< rinfo
->metric
)
642 || ((same
) && (rinfo
->metric
== rte
->metric
)
643 && (newinfo
.tag
!= rinfo
->tag
))
644 || (old_dist
> new_dist
)
645 || ((old_dist
!= new_dist
) && same
)) {
646 if (listcount(list
) == 1) {
647 if (newinfo
.metric
!= RIP_METRIC_INFINITY
)
648 rip_ecmp_replace(rip
, &newinfo
);
650 rip_ecmp_delete(rip
, rinfo
);
652 if (newinfo
.metric
< rinfo
->metric
)
653 rip_ecmp_replace(rip
, &newinfo
);
654 else if (newinfo
.metric
> rinfo
->metric
)
655 rip_ecmp_delete(rip
, rinfo
);
656 else if (new_dist
< old_dist
)
657 rip_ecmp_replace(rip
, &newinfo
);
658 else if (new_dist
> old_dist
)
659 rip_ecmp_delete(rip
, rinfo
);
661 int update
= CHECK_FLAG(rinfo
->flags
,
666 assert(newinfo
.metric
667 != RIP_METRIC_INFINITY
);
669 RIP_TIMER_OFF(rinfo
->t_timeout
);
670 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
671 memcpy(rinfo
, &newinfo
,
672 sizeof(struct rip_info
));
673 rip_timeout_update(rip
, rinfo
);
676 rip_zebra_ipv4_add(rip
, rp
);
678 /* - Set the route change flag on the
680 rinfo
= listgetdata(listhead(list
));
681 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
682 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
685 } else /* same & no change */
686 rip_timeout_update(rip
, rinfo
);
688 /* Unlock tempolary lock of the route. */
689 route_unlock_node(rp
);
693 /* Dump RIP packet */
694 static void rip_packet_dump(struct rip_packet
*packet
, int size
,
699 const char *command_str
;
700 char pbuf
[BUFSIZ
], nbuf
[BUFSIZ
];
704 /* Set command string. */
705 if (packet
->command
> 0 && packet
->command
< RIP_COMMAND_MAX
)
706 command_str
= lookup_msg(rip_msg
, packet
->command
, NULL
);
708 command_str
= "unknown";
710 /* Dump packet header. */
711 zlog_debug("%s %s version %d packet size %d", sndrcv
, command_str
,
712 packet
->version
, size
);
714 /* Dump each routing table entry. */
717 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
718 if (packet
->version
== RIPv2
) {
719 netmask
= ip_masklen(rte
->mask
);
721 if (rte
->family
== htons(RIP_FAMILY_AUTH
)) {
723 == htons(RIP_AUTH_SIMPLE_PASSWORD
)) {
724 p
= (uint8_t *)&rte
->prefix
;
727 " family 0x%X type %d auth string: %s",
730 } else if (rte
->tag
== htons(RIP_AUTH_MD5
)) {
731 struct rip_md5_info
*md5
;
733 md5
= (struct rip_md5_info
*)&packet
737 " family 0x%X type %d (MD5 authentication)",
741 " RIP-2 packet len %d Key ID %d"
743 ntohs(md5
->packet_len
),
744 md5
->keyid
, md5
->auth_len
);
745 zlog_debug(" Sequence Number %ld",
746 (unsigned long)ntohl(
748 } else if (rte
->tag
== htons(RIP_AUTH_DATA
)) {
749 p
= (uint8_t *)&rte
->prefix
;
752 " family 0x%X type %d (MD5 data)",
756 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
757 "%02X%02X%02X%02X%02X%02X%02X%02X",
758 p
[0], p
[1], p
[2], p
[3], p
[4],
759 p
[5], p
[6], p
[7], p
[8], p
[9],
760 p
[10], p
[11], p
[12], p
[13],
764 " family 0x%X type %d (Unknown auth type)",
770 " %s/%d -> %s family %d tag %" ROUTE_TAG_PRI
772 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
,
775 inet_ntop(AF_INET
, &rte
->nexthop
, nbuf
,
778 (route_tag_t
)ntohs(rte
->tag
),
779 (unsigned long)ntohl(rte
->metric
));
782 " %s family %d tag %" ROUTE_TAG_PRI
784 inet_ntop(AF_INET
, &rte
->prefix
, pbuf
, BUFSIZ
),
786 (route_tag_t
)ntohs(rte
->tag
),
787 (unsigned long)ntohl(rte
->metric
));
792 /* Check if the destination address is valid (unicast; not net 0
793 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
794 check net 0 because we accept default route. */
795 static int rip_destination_check(struct in_addr addr
)
797 uint32_t destination
;
799 /* Convert to host byte order. */
800 destination
= ntohl(addr
.s_addr
);
802 if (IPV4_NET127(destination
))
805 /* Net 0 may match to the default route. */
806 if (IPV4_NET0(destination
) && destination
!= 0)
809 /* Unicast address must belong to class A, B, C. */
810 if (IN_CLASSA(destination
))
812 if (IN_CLASSB(destination
))
814 if (IN_CLASSC(destination
))
820 /* RIP version 2 authentication. */
821 static int rip_auth_simple_password(struct rte
*rte
, struct sockaddr_in
*from
,
822 struct interface
*ifp
)
824 struct rip_interface
*ri
;
825 char *auth_str
= (char *)rte
+ offsetof(struct rte
, prefix
);
828 /* reject passwords with zeros in the middle of the string */
829 for (i
= strnlen(auth_str
, 16); i
< 16; i
++) {
830 if (auth_str
[i
] != '\0')
834 if (IS_RIP_DEBUG_EVENT
)
835 zlog_debug("RIPv2 simple password authentication from %s",
836 inet_ntoa(from
->sin_addr
));
840 if (ri
->auth_type
!= RIP_AUTH_SIMPLE_PASSWORD
841 || rte
->tag
!= htons(RIP_AUTH_SIMPLE_PASSWORD
))
844 /* Simple password authentication. */
846 if (strncmp(auth_str
, ri
->auth_str
, 16) == 0)
850 struct keychain
*keychain
;
853 keychain
= keychain_lookup(ri
->key_chain
);
854 if (keychain
== NULL
|| keychain
->key
== NULL
)
857 key
= key_match_for_accept(keychain
, auth_str
);
864 /* RIP version 2 authentication with MD5. */
865 static int rip_auth_md5(struct rip_packet
*packet
, struct sockaddr_in
*from
,
866 int length
, struct interface
*ifp
)
868 struct rip_interface
*ri
;
869 struct rip_md5_info
*md5
;
870 struct rip_md5_data
*md5data
;
871 struct keychain
*keychain
;
874 uint8_t digest
[RIP_AUTH_MD5_SIZE
];
876 char auth_str
[RIP_AUTH_MD5_SIZE
] = {};
878 if (IS_RIP_DEBUG_EVENT
)
879 zlog_debug("RIPv2 MD5 authentication from %s",
880 inet_ntoa(from
->sin_addr
));
883 md5
= (struct rip_md5_info
*)&packet
->rte
;
885 /* Check auth type. */
886 if (ri
->auth_type
!= RIP_AUTH_MD5
|| md5
->type
!= htons(RIP_AUTH_MD5
))
889 /* If the authentication length is less than 16, then it must be wrong
891 * any interpretation of rfc2082. Some implementations also interpret
892 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
893 * RIP_AUTH_MD5_COMPAT_SIZE.
895 if (!((md5
->auth_len
== RIP_AUTH_MD5_SIZE
)
896 || (md5
->auth_len
== RIP_AUTH_MD5_COMPAT_SIZE
))) {
897 if (IS_RIP_DEBUG_EVENT
)
899 "RIPv2 MD5 authentication, strange authentication "
905 /* grab and verify check packet length */
906 packet_len
= ntohs(md5
->packet_len
);
908 if (packet_len
> (length
- RIP_HEADER_SIZE
- RIP_AUTH_MD5_SIZE
)) {
909 if (IS_RIP_DEBUG_EVENT
)
911 "RIPv2 MD5 authentication, packet length field %d "
912 "greater than received length %d!",
913 md5
->packet_len
, length
);
917 /* retrieve authentication data */
918 md5data
= (struct rip_md5_data
*)(((uint8_t *)packet
) + packet_len
);
921 keychain
= keychain_lookup(ri
->key_chain
);
922 if (keychain
== NULL
)
925 key
= key_lookup_for_accept(keychain
, md5
->keyid
);
926 if (key
== NULL
|| key
->string
== NULL
)
929 strlcpy(auth_str
, key
->string
, sizeof(auth_str
));
930 } else if (ri
->auth_str
)
931 strlcpy(auth_str
, ri
->auth_str
, sizeof(auth_str
));
933 if (auth_str
[0] == 0)
936 /* MD5 digest authentication. */
937 memset(&ctx
, 0, sizeof(ctx
));
939 MD5Update(&ctx
, packet
, packet_len
+ RIP_HEADER_SIZE
);
940 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
941 MD5Final(digest
, &ctx
);
943 if (memcmp(md5data
->digest
, digest
, RIP_AUTH_MD5_SIZE
) == 0)
949 /* Pick correct auth string for sends, prepare auth_str buffer for use.
950 * (left justified and padded).
952 * presumes one of ri or key is valid, and that the auth strings they point
953 * to are nul terminated. If neither are present, auth_str will be fully
957 static void rip_auth_prepare_str_send(struct rip_interface
*ri
, struct key
*key
,
958 char *auth_str
, int len
)
962 memset(auth_str
, 0, len
);
963 if (key
&& key
->string
)
964 strlcpy(auth_str
, key
->string
, len
);
965 else if (ri
->auth_str
)
966 strlcpy(auth_str
, ri
->auth_str
, len
);
971 /* Write RIPv2 simple password authentication information
973 * auth_str is presumed to be 2 bytes and correctly prepared
974 * (left justified and zero padded).
976 static void rip_auth_simple_write(struct stream
*s
, char *auth_str
, int len
)
978 assert(s
&& len
== RIP_AUTH_SIMPLE_SIZE
);
980 stream_putw(s
, RIP_FAMILY_AUTH
);
981 stream_putw(s
, RIP_AUTH_SIMPLE_PASSWORD
);
982 stream_put(s
, auth_str
, RIP_AUTH_SIMPLE_SIZE
);
987 /* write RIPv2 MD5 "authentication header"
988 * (uses the auth key data field)
990 * Digest offset field is set to 0.
992 * returns: offset of the digest offset field, which must be set when
993 * length to the auth-data MD5 digest is known.
995 static size_t rip_auth_md5_ah_write(struct stream
*s
, struct rip_interface
*ri
,
1000 assert(s
&& ri
&& ri
->auth_type
== RIP_AUTH_MD5
);
1002 /* MD5 authentication. */
1003 stream_putw(s
, RIP_FAMILY_AUTH
);
1004 stream_putw(s
, RIP_AUTH_MD5
);
1006 /* MD5 AH digest offset field.
1008 * Set to placeholder value here, to true value when RIP-2 Packet length
1009 * is known. Actual value is set in .....().
1011 doff
= stream_get_endp(s
);
1016 stream_putc(s
, key
->index
% 256);
1020 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1021 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1023 * to be configurable.
1025 stream_putc(s
, ri
->md5_auth_len
);
1027 /* Sequence Number (non-decreasing). */
1028 /* RFC2080: The value used in the sequence number is
1029 arbitrary, but two suggestions are the time of the
1030 message's creation or a simple message counter. */
1031 stream_putl(s
, time(NULL
));
1033 /* Reserved field must be zero. */
1040 /* If authentication is in used, write the appropriate header
1041 * returns stream offset to which length must later be written
1042 * or 0 if this is not required
1044 static size_t rip_auth_header_write(struct stream
*s
, struct rip_interface
*ri
,
1045 struct key
*key
, char *auth_str
, int len
)
1047 assert(ri
->auth_type
!= RIP_NO_AUTH
);
1049 switch (ri
->auth_type
) {
1050 case RIP_AUTH_SIMPLE_PASSWORD
:
1051 rip_auth_prepare_str_send(ri
, key
, auth_str
, len
);
1052 rip_auth_simple_write(s
, auth_str
, len
);
1055 return rip_auth_md5_ah_write(s
, ri
, key
);
1061 /* Write RIPv2 MD5 authentication data trailer */
1062 static void rip_auth_md5_set(struct stream
*s
, struct rip_interface
*ri
,
1063 size_t doff
, char *auth_str
, int authlen
)
1067 unsigned char digest
[RIP_AUTH_MD5_SIZE
];
1069 /* Make it sure this interface is configured as MD5
1071 assert((ri
->auth_type
== RIP_AUTH_MD5
)
1072 && (authlen
== RIP_AUTH_MD5_SIZE
));
1075 /* Get packet length. */
1076 len
= stream_get_endp(s
);
1078 /* Check packet length. */
1079 if (len
< (RIP_HEADER_SIZE
+ RIP_RTE_SIZE
)) {
1082 "rip_auth_md5_set(): packet length %ld is less than minimum length.",
1087 /* Set the digest offset length in the header */
1088 stream_putw_at(s
, doff
, len
);
1090 /* Set authentication data. */
1091 stream_putw(s
, RIP_FAMILY_AUTH
);
1092 stream_putw(s
, RIP_AUTH_DATA
);
1094 /* Generate a digest for the RIP packet. */
1095 memset(&ctx
, 0, sizeof(ctx
));
1097 MD5Update(&ctx
, STREAM_DATA(s
), stream_get_endp(s
));
1098 MD5Update(&ctx
, auth_str
, RIP_AUTH_MD5_SIZE
);
1099 MD5Final(digest
, &ctx
);
1101 /* Copy the digest to the packet. */
1102 stream_write(s
, digest
, RIP_AUTH_MD5_SIZE
);
1105 /* RIP routing information. */
1106 static void rip_response_process(struct rip_packet
*packet
, int size
,
1107 struct sockaddr_in
*from
,
1108 struct connected
*ifc
)
1110 struct rip_interface
*ri
= ifc
->ifp
->info
;
1111 struct rip
*rip
= ri
->rip
;
1114 struct prefix_ipv4 ifaddr
;
1115 struct prefix_ipv4 ifaddrclass
;
1118 memset(&ifaddr
, 0, sizeof(ifaddr
));
1119 /* We don't know yet. */
1122 /* The Response must be ignored if it is not from the RIP
1123 port. (RFC2453 - Sec. 3.9.2)*/
1124 if (from
->sin_port
!= htons(RIP_PORT_DEFAULT
)) {
1125 zlog_info("response doesn't come from RIP port: %d",
1127 rip_peer_bad_packet(rip
, from
);
1131 /* The datagram's IPv4 source address should be checked to see
1132 whether the datagram is from a valid neighbor; the source of the
1133 datagram must be on a directly connected network (RFC2453 - Sec.
1135 if (if_lookup_address((void *)&from
->sin_addr
, AF_INET
,
1139 "This datagram doesn't came from a valid neighbor: %s",
1140 inet_ntoa(from
->sin_addr
));
1141 rip_peer_bad_packet(rip
, from
);
1145 /* It is also worth checking to see whether the response is from one
1146 of the router's own addresses. */
1148 ; /* Alredy done in rip_read () */
1150 /* Update RIP peer. */
1151 rip_peer_update(rip
, from
, packet
->version
);
1153 /* Set RTE pointer. */
1156 for (lim
= (caddr_t
)packet
+ size
; (caddr_t
)rte
< lim
; rte
++) {
1157 /* RIPv2 authentication check. */
1158 /* If the Address Family Identifier of the first (and only the
1159 first) entry in the message is 0xFFFF, then the remainder of
1160 the entry contains the authentication. */
1161 /* If the packet gets here it means authentication enabled */
1162 /* Check is done in rip_read(). So, just skipping it */
1163 if (packet
->version
== RIPv2
&& rte
== packet
->rte
1164 && rte
->family
== htons(RIP_FAMILY_AUTH
))
1167 if (rte
->family
!= htons(AF_INET
)) {
1168 /* Address family check. RIP only supports AF_INET. */
1169 zlog_info("Unsupported family %d from %s.",
1171 inet_ntoa(from
->sin_addr
));
1175 /* - is the destination address valid (e.g., unicast; not net 0
1177 if (!rip_destination_check(rte
->prefix
)) {
1179 "Network is net 0 or net 127 or it is not unicast network");
1180 rip_peer_bad_route(rip
, from
);
1184 /* Convert metric value to host byte order. */
1185 rte
->metric
= ntohl(rte
->metric
);
1187 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1188 if (!(rte
->metric
>= 1 && rte
->metric
<= 16)) {
1189 zlog_info("Route's metric is not in the 1-16 range.");
1190 rip_peer_bad_route(rip
, from
);
1194 /* RIPv1 does not have nexthop value. */
1195 if (packet
->version
== RIPv1
&& rte
->nexthop
.s_addr
!= 0) {
1196 zlog_info("RIPv1 packet with nexthop value %s",
1197 inet_ntoa(rte
->nexthop
));
1198 rip_peer_bad_route(rip
, from
);
1202 /* That is, if the provided information is ignored, a possibly
1203 sub-optimal, but absolutely valid, route may be taken. If
1204 the received Next Hop is not directly reachable, it should be
1205 treated as 0.0.0.0. */
1206 if (packet
->version
== RIPv2
&& rte
->nexthop
.s_addr
!= 0) {
1209 /* Multicast address check. */
1210 addrval
= ntohl(rte
->nexthop
.s_addr
);
1211 if (IN_CLASSD(addrval
)) {
1213 "Nexthop %s is multicast address, skip this rte",
1214 inet_ntoa(rte
->nexthop
));
1218 if (!if_lookup_address((void *)&rte
->nexthop
, AF_INET
,
1219 rip
->vrf
->vrf_id
)) {
1220 struct route_node
*rn
;
1221 struct rip_info
*rinfo
;
1223 rn
= route_node_match_ipv4(rip
->table
,
1229 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1232 if (IS_RIP_DEBUG_EVENT
)
1234 "Next hop %s is on RIP network. Set nexthop to the packet's originator",
1237 rte
->nexthop
= rinfo
->from
;
1239 if (IS_RIP_DEBUG_EVENT
)
1241 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1244 rte
->nexthop
.s_addr
= 0;
1247 route_unlock_node(rn
);
1249 if (IS_RIP_DEBUG_EVENT
)
1251 "Next hop %s is not directly reachable. Treat it as 0.0.0.0",
1254 rte
->nexthop
.s_addr
= 0;
1259 /* For RIPv1, there won't be a valid netmask.
1261 This is a best guess at the masks. If everyone was using old
1262 Ciscos before the 'ip subnet zero' option, it would be almost
1265 Cisco summarize ripv1 advertisements to the classful boundary
1266 (/16 for class B's) except when the RIP packet does to inside
1267 the classful network in question. */
1269 if ((packet
->version
== RIPv1
&& rte
->prefix
.s_addr
!= 0)
1270 || (packet
->version
== RIPv2
1271 && (rte
->prefix
.s_addr
!= 0
1272 && rte
->mask
.s_addr
== 0))) {
1273 uint32_t destination
;
1275 if (subnetted
== -1) {
1276 memcpy(&ifaddr
, ifc
->address
,
1277 sizeof(struct prefix_ipv4
));
1278 memcpy(&ifaddrclass
, &ifaddr
,
1279 sizeof(struct prefix_ipv4
));
1280 apply_classful_mask_ipv4(&ifaddrclass
);
1282 if (ifaddr
.prefixlen
> ifaddrclass
.prefixlen
)
1286 destination
= ntohl(rte
->prefix
.s_addr
);
1288 if (IN_CLASSA(destination
))
1289 masklen2ip(8, &rte
->mask
);
1290 else if (IN_CLASSB(destination
))
1291 masklen2ip(16, &rte
->mask
);
1292 else if (IN_CLASSC(destination
))
1293 masklen2ip(24, &rte
->mask
);
1296 masklen2ip(ifaddrclass
.prefixlen
,
1297 (struct in_addr
*)&destination
);
1298 if ((subnetted
== 1)
1299 && ((rte
->prefix
.s_addr
& destination
)
1300 == ifaddrclass
.prefix
.s_addr
)) {
1301 masklen2ip(ifaddr
.prefixlen
, &rte
->mask
);
1302 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1303 != rte
->prefix
.s_addr
)
1304 masklen2ip(32, &rte
->mask
);
1305 if (IS_RIP_DEBUG_EVENT
)
1306 zlog_debug("Subnetted route %s",
1307 inet_ntoa(rte
->prefix
));
1309 if ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1310 != rte
->prefix
.s_addr
)
1314 if (IS_RIP_DEBUG_EVENT
) {
1315 zlog_debug("Resultant route %s",
1316 inet_ntoa(rte
->prefix
));
1317 zlog_debug("Resultant mask %s",
1318 inet_ntoa(rte
->mask
));
1322 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1323 ignore the entry. */
1324 if ((packet
->version
== RIPv2
) && (rte
->mask
.s_addr
!= 0)
1325 && ((rte
->prefix
.s_addr
& rte
->mask
.s_addr
)
1326 != rte
->prefix
.s_addr
)) {
1328 "RIPv2 address %s is not mask /%d applied one",
1329 inet_ntoa(rte
->prefix
), ip_masklen(rte
->mask
));
1330 rip_peer_bad_route(rip
, from
);
1334 /* Default route's netmask is ignored. */
1335 if (packet
->version
== RIPv2
&& (rte
->prefix
.s_addr
== 0)
1336 && (rte
->mask
.s_addr
!= 0)) {
1337 if (IS_RIP_DEBUG_EVENT
)
1339 "Default route with non-zero netmask. Set zero to netmask");
1340 rte
->mask
.s_addr
= 0;
1343 /* Routing table updates. */
1344 rip_rte_process(rte
, from
, ifc
->ifp
);
1348 /* Make socket for RIP protocol. */
1349 int rip_create_socket(struct vrf
*vrf
)
1353 struct sockaddr_in addr
;
1354 const char *vrf_dev
= NULL
;
1356 memset(&addr
, 0, sizeof(struct sockaddr_in
));
1357 addr
.sin_family
= AF_INET
;
1358 addr
.sin_addr
.s_addr
= INADDR_ANY
;
1359 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1360 addr
.sin_len
= sizeof(struct sockaddr_in
);
1361 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1362 /* sending port must always be the RIP port */
1363 addr
.sin_port
= htons(RIP_PORT_DEFAULT
);
1365 /* Make datagram socket. */
1366 if (vrf
->vrf_id
!= VRF_DEFAULT
)
1367 vrf_dev
= vrf
->name
;
1368 frr_elevate_privs(&ripd_privs
) {
1369 sock
= vrf_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, vrf
->vrf_id
,
1372 flog_err_sys(EC_LIB_SOCKET
,
1373 "Cannot create UDP socket: %s",
1374 safe_strerror(errno
));
1379 sockopt_broadcast(sock
);
1380 sockopt_reuseaddr(sock
);
1381 sockopt_reuseport(sock
);
1382 setsockopt_ipv4_multicast_loop(sock
, 0);
1383 #ifdef IPTOS_PREC_INTERNETCONTROL
1384 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
1386 setsockopt_so_recvbuf(sock
, RIP_UDP_RCV_BUF
);
1388 frr_elevate_privs(&ripd_privs
) {
1389 if ((ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1391 zlog_err("%s: Can't bind socket %d to %s port %d: %s",
1392 __func__
, sock
, inet_ntoa(addr
.sin_addr
),
1393 (int)ntohs(addr
.sin_port
),
1394 safe_strerror(errno
));
1404 /* RIP packet send to destination address, on interface denoted by
1405 * by connected argument. NULL to argument denotes destination should be
1406 * should be RIP multicast group
1408 static int rip_send_packet(uint8_t *buf
, int size
, struct sockaddr_in
*to
,
1409 struct connected
*ifc
)
1411 struct rip_interface
*ri
;
1414 struct sockaddr_in sin
;
1418 struct cmsghdr
*cmsgptr
;
1419 char adata
[256] = {};
1420 struct in_pktinfo
*pkt
;
1421 #endif /* GNU_LINUX */
1423 assert(ifc
!= NULL
);
1424 ri
= ifc
->ifp
->info
;
1427 if (IS_RIP_DEBUG_PACKET
) {
1428 #define ADDRESS_SIZE 20
1429 char dst
[ADDRESS_SIZE
];
1432 strlcpy(dst
, inet_ntoa(to
->sin_addr
), sizeof(dst
));
1434 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1435 strlcpy(dst
, inet_ntoa(sin
.sin_addr
), sizeof(dst
));
1438 zlog_debug("rip_send_packet %s > %s (%s)",
1439 inet_ntoa(ifc
->address
->u
.prefix4
), dst
,
1443 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)) {
1445 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1447 * with multiple addresses on the same subnet: the first address
1448 * on the subnet is configured "primary", and all subsequent
1450 * on that subnet are treated as "secondary" addresses.
1451 * In order to avoid routing-table bloat on other rip listeners,
1452 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY
1454 * XXX Since Linux is the only system for which the
1455 * ZEBRA_IFA_SECONDARY
1456 * flag is set, we would end up sending a packet for a
1458 * source address on non-linux systems.
1460 if (IS_RIP_DEBUG_PACKET
)
1461 zlog_debug("duplicate dropped");
1465 /* Make destination address. */
1466 memset(&sin
, 0, sizeof(struct sockaddr_in
));
1467 sin
.sin_family
= AF_INET
;
1468 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1469 sin
.sin_len
= sizeof(struct sockaddr_in
);
1470 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1472 /* When destination is specified, use it's port and address. */
1474 sin
.sin_port
= to
->sin_port
;
1475 sin
.sin_addr
= to
->sin_addr
;
1477 sin
.sin_port
= htons(RIP_PORT_DEFAULT
);
1478 sin
.sin_addr
.s_addr
= htonl(INADDR_RIP_GROUP
);
1480 rip_interface_multicast_set(rip
->sock
, ifc
);
1483 memset(&msg
, 0, sizeof(msg
));
1484 msg
.msg_name
= (void *)&sin
;
1485 msg
.msg_namelen
= sizeof(struct sockaddr_in
);
1492 msg
.msg_control
= (void *)adata
;
1493 msg
.msg_controllen
= CMSG_SPACE(sizeof(struct in_pktinfo
));
1495 cmsgptr
= (struct cmsghdr
*)adata
;
1496 cmsgptr
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1497 cmsgptr
->cmsg_level
= IPPROTO_IP
;
1498 cmsgptr
->cmsg_type
= IP_PKTINFO
;
1499 pkt
= (struct in_pktinfo
*)CMSG_DATA(cmsgptr
);
1500 pkt
->ipi_ifindex
= ifc
->ifp
->ifindex
;
1501 #endif /* GNU_LINUX */
1503 ret
= sendmsg(rip
->sock
, &msg
, 0);
1505 if (IS_RIP_DEBUG_EVENT
)
1506 zlog_debug("SEND to %s.%d", inet_ntoa(sin
.sin_addr
),
1507 ntohs(sin
.sin_port
));
1510 zlog_warn("can't send packet : %s", safe_strerror(errno
));
1515 /* Add redistributed route to RIP table. */
1516 void rip_redistribute_add(struct rip
*rip
, int type
, int sub_type
,
1517 struct prefix_ipv4
*p
, struct nexthop
*nh
,
1518 unsigned int metric
, unsigned char distance
,
1522 struct route_node
*rp
= NULL
;
1523 struct rip_info
*rinfo
= NULL
, newinfo
;
1524 struct list
*list
= NULL
;
1526 /* Redistribute route */
1527 ret
= rip_destination_check(p
->prefix
);
1531 rp
= route_node_get(rip
->table
, (struct prefix
*)p
);
1533 memset(&newinfo
, 0, sizeof(struct rip_info
));
1534 newinfo
.type
= type
;
1535 newinfo
.sub_type
= sub_type
;
1537 newinfo
.external_metric
= metric
;
1538 newinfo
.distance
= distance
;
1539 if (tag
<= UINT16_MAX
) /* RIP only supports 16 bit tags */
1544 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
1545 rinfo
= listgetdata(listhead(list
));
1547 if (rinfo
->type
== ZEBRA_ROUTE_CONNECT
1548 && rinfo
->sub_type
== RIP_ROUTE_INTERFACE
1549 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
1550 route_unlock_node(rp
);
1554 /* Manually configured RIP route check. */
1555 if (rinfo
->type
== ZEBRA_ROUTE_RIP
1556 && ((rinfo
->sub_type
== RIP_ROUTE_STATIC
)
1557 || (rinfo
->sub_type
== RIP_ROUTE_DEFAULT
))) {
1558 if (type
!= ZEBRA_ROUTE_RIP
1559 || ((sub_type
!= RIP_ROUTE_STATIC
)
1560 && (sub_type
!= RIP_ROUTE_DEFAULT
))) {
1561 route_unlock_node(rp
);
1566 (void)rip_ecmp_replace(rip
, &newinfo
);
1567 route_unlock_node(rp
);
1569 (void)rip_ecmp_add(rip
, &newinfo
);
1571 if (IS_RIP_DEBUG_EVENT
) {
1572 zlog_debug("Redistribute new prefix %s/%d",
1573 inet_ntoa(p
->prefix
), p
->prefixlen
);
1576 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1579 /* Delete redistributed route from RIP table. */
1580 void rip_redistribute_delete(struct rip
*rip
, int type
, int sub_type
,
1581 struct prefix_ipv4
*p
, ifindex_t ifindex
)
1584 struct route_node
*rp
;
1585 struct rip_info
*rinfo
;
1587 ret
= rip_destination_check(p
->prefix
);
1591 rp
= route_node_lookup(rip
->table
, (struct prefix
*)p
);
1593 struct list
*list
= rp
->info
;
1595 if (list
!= NULL
&& listcount(list
) != 0) {
1596 rinfo
= listgetdata(listhead(list
));
1597 if (rinfo
!= NULL
&& rinfo
->type
== type
1598 && rinfo
->sub_type
== sub_type
1599 && rinfo
->nh
.ifindex
== ifindex
) {
1600 /* Perform poisoned reverse. */
1601 rinfo
->metric
= RIP_METRIC_INFINITY
;
1602 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
1603 rip_garbage_collect
,
1605 RIP_TIMER_OFF(rinfo
->t_timeout
);
1606 rinfo
->flags
|= RIP_RTF_CHANGED
;
1608 if (IS_RIP_DEBUG_EVENT
)
1610 "Poison %s/%d on the interface %s with an "
1611 "infinity metric [delete]",
1612 inet_ntoa(p
->prefix
),
1618 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
1621 route_unlock_node(rp
);
1625 /* Response to request called from rip_read ().*/
1626 static void rip_request_process(struct rip_packet
*packet
, int size
,
1627 struct sockaddr_in
*from
, struct connected
*ifc
)
1632 struct prefix_ipv4 p
;
1633 struct route_node
*rp
;
1634 struct rip_info
*rinfo
;
1635 struct rip_interface
*ri
;
1637 /* Does not reponse to the requests on the loopback interfaces */
1638 if (if_is_loopback(ifc
->ifp
))
1641 /* Check RIP process is enabled on this interface. */
1642 ri
= ifc
->ifp
->info
;
1647 /* When passive interface is specified, suppress responses */
1651 /* RIP peer update. */
1652 rip_peer_update(rip
, from
, packet
->version
);
1654 lim
= ((caddr_t
)packet
) + size
;
1657 /* The Request is processed entry by entry. If there are no
1658 entries, no response is given. */
1659 if (lim
== (caddr_t
)rte
)
1662 /* There is one special case. If there is exactly one entry in the
1663 request, and it has an address family identifier of zero and a
1664 metric of infinity (i.e., 16), then this is a request to send the
1665 entire routing table. */
1666 if (lim
== ((caddr_t
)(rte
+ 1)) && ntohs(rte
->family
) == 0
1667 && ntohl(rte
->metric
) == RIP_METRIC_INFINITY
) {
1668 /* All route with split horizon */
1669 rip_output_process(ifc
, from
, rip_all_route
, packet
->version
);
1671 if (ntohs(rte
->family
) != AF_INET
)
1674 /* Examine the list of RTEs in the Request one by one. For each
1675 entry, look up the destination in the router's routing
1676 database and, if there is a route, put that route's metric in
1677 the metric field of the RTE. If there is no explicit route
1678 to the specified destination, put infinity in the metric
1679 field. Once all the entries have been filled in, change the
1680 command from Request to Response and send the datagram back
1681 to the requestor. */
1684 for (; ((caddr_t
)rte
) < lim
; rte
++) {
1685 p
.prefix
= rte
->prefix
;
1686 p
.prefixlen
= ip_masklen(rte
->mask
);
1687 apply_mask_ipv4(&p
);
1689 rp
= route_node_lookup(rip
->table
, (struct prefix
*)&p
);
1691 rinfo
= listgetdata(
1692 listhead((struct list
*)rp
->info
));
1693 rte
->metric
= htonl(rinfo
->metric
);
1694 route_unlock_node(rp
);
1696 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
1698 packet
->command
= RIP_RESPONSE
;
1700 (void)rip_send_packet((uint8_t *)packet
, size
, from
, ifc
);
1702 rip
->counters
.queries
++;
1705 /* First entry point of RIP packet. */
1706 static int rip_read(struct thread
*t
)
1708 struct rip
*rip
= THREAD_ARG(t
);
1712 union rip_buf rip_buf
;
1713 struct rip_packet
*packet
;
1714 struct sockaddr_in from
;
1718 struct interface
*ifp
= NULL
;
1719 struct connected
*ifc
;
1720 struct rip_interface
*ri
;
1723 /* Fetch socket then register myself. */
1724 sock
= THREAD_FD(t
);
1727 /* Add myself to tne next event */
1728 rip_event(rip
, RIP_READ
, sock
);
1730 /* RIPd manages only IPv4. */
1731 memset(&from
, 0, sizeof(struct sockaddr_in
));
1732 fromlen
= sizeof(struct sockaddr_in
);
1734 len
= recvfrom(sock
, (char *)&rip_buf
.buf
, sizeof(rip_buf
.buf
), 0,
1735 (struct sockaddr
*)&from
, &fromlen
);
1737 zlog_info("recvfrom failed (VRF %s): %s", rip
->vrf_name
,
1738 safe_strerror(errno
));
1742 /* Check is this packet comming from myself? */
1743 if (if_check_address(rip
, from
.sin_addr
)) {
1744 if (IS_RIP_DEBUG_PACKET
)
1745 zlog_debug("ignore packet comes from myself (VRF %s)",
1750 /* Which interface is this packet comes from. */
1751 ifc
= if_lookup_address((void *)&from
.sin_addr
, AF_INET
,
1756 /* RIP packet received */
1757 if (IS_RIP_DEBUG_EVENT
)
1758 zlog_debug("RECV packet from %s port %d on %s (VRF %s)",
1759 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1760 ifp
? ifp
->name
: "unknown", rip
->vrf_name
);
1762 /* If this packet come from unknown interface, ignore it. */
1765 "rip_read: cannot find interface for packet from %s port %d (VRF %s)",
1766 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1772 p
.u
.prefix4
= from
.sin_addr
;
1773 p
.prefixlen
= IPV4_MAX_BITLEN
;
1775 ifc
= connected_lookup_prefix(ifp
, &p
);
1779 "rip_read: cannot find connected address for packet from %s "
1780 "port %d on interface %s (VRF %s)",
1781 inet_ntoa(from
.sin_addr
), ntohs(from
.sin_port
),
1782 ifp
->name
, rip
->vrf_name
);
1786 /* Packet length check. */
1787 if (len
< RIP_PACKET_MINSIZ
) {
1788 zlog_warn("packet size %d is smaller than minimum size %d", len
,
1790 rip_peer_bad_packet(rip
, &from
);
1793 if (len
> RIP_PACKET_MAXSIZ
) {
1794 zlog_warn("packet size %d is larger than max size %d", len
,
1796 rip_peer_bad_packet(rip
, &from
);
1800 /* Packet alignment check. */
1801 if ((len
- RIP_PACKET_MINSIZ
) % 20) {
1802 zlog_warn("packet size %d is wrong for RIP packet alignment",
1804 rip_peer_bad_packet(rip
, &from
);
1808 /* Set RTE number. */
1809 rtenum
= ((len
- RIP_PACKET_MINSIZ
) / 20);
1811 /* For easy to handle. */
1812 packet
= &rip_buf
.rip_packet
;
1814 /* RIP version check. */
1815 if (packet
->version
== 0) {
1816 zlog_info("version 0 with command %d received.",
1818 rip_peer_bad_packet(rip
, &from
);
1822 /* Dump RIP packet. */
1823 if (IS_RIP_DEBUG_RECV
)
1824 rip_packet_dump(packet
, len
, "RECV");
1826 /* RIP version adjust. This code should rethink now. RFC1058 says
1827 that "Version 1 implementations are to ignore this extra data and
1828 process only the fields specified in this document.". So RIPv3
1829 packet should be treated as RIPv1 ignoring must be zero field. */
1830 if (packet
->version
> RIPv2
)
1831 packet
->version
= RIPv2
;
1833 /* Is RIP running or is this RIP neighbor ?*/
1835 if (!ri
->running
&& !rip_neighbor_lookup(rip
, &from
)) {
1836 if (IS_RIP_DEBUG_EVENT
)
1837 zlog_debug("RIP is not enabled on interface %s.",
1839 rip_peer_bad_packet(rip
, &from
);
1843 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1844 vrecv
= ((ri
->ri_receive
== RI_RIP_UNSPEC
) ? rip
->version_recv
1846 if (vrecv
== RI_RIP_VERSION_NONE
1847 || ((packet
->version
== RIPv1
) && !(vrecv
& RIPv1
))
1848 || ((packet
->version
== RIPv2
) && !(vrecv
& RIPv2
))) {
1849 if (IS_RIP_DEBUG_PACKET
)
1851 " packet's v%d doesn't fit to if version spec",
1853 rip_peer_bad_packet(rip
, &from
);
1857 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1858 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1859 accepted; authenticated RIP-2 messages shall be discarded. */
1860 if ((ri
->auth_type
== RIP_NO_AUTH
) && rtenum
1861 && (packet
->version
== RIPv2
)
1862 && (packet
->rte
->family
== htons(RIP_FAMILY_AUTH
))) {
1863 if (IS_RIP_DEBUG_EVENT
)
1865 "packet RIPv%d is dropped because authentication disabled",
1867 ripd_notif_send_auth_type_failure(ifp
->name
);
1868 rip_peer_bad_packet(rip
, &from
);
1873 If the router is configured to authenticate RIP-2 messages, then
1874 RIP-1 messages and RIP-2 messages which pass authentication
1875 testing shall be accepted; unauthenticated and failed
1876 authentication RIP-2 messages shall be discarded. For maximum
1877 security, RIP-1 messages should be ignored when authentication is
1878 in use (see section 4.1); otherwise, the routing information from
1879 authenticated messages will be propagated by RIP-1 routers in an
1880 unauthenticated manner.
1882 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1883 * always reply regardless of authentication settings, because:
1885 * - if there other authorised routers on-link, the REQUESTor can
1886 * passively obtain the routing updates anyway
1887 * - if there are no other authorised routers on-link, RIP can
1888 * easily be disabled for the link to prevent giving out information
1889 * on state of this routers RIP routing table..
1891 * I.e. if RIPv1 has any place anymore these days, it's as a very
1892 * simple way to distribute routing information (e.g. to embedded
1893 * hosts / appliances) and the ability to give out RIPv1
1894 * routing-information freely, while still requiring RIPv2
1895 * authentication for any RESPONSEs might be vaguely useful.
1897 if (ri
->auth_type
!= RIP_NO_AUTH
&& packet
->version
== RIPv1
) {
1898 /* Discard RIPv1 messages other than REQUESTs */
1899 if (packet
->command
!= RIP_REQUEST
) {
1900 if (IS_RIP_DEBUG_PACKET
)
1903 " dropped because authentication enabled");
1904 ripd_notif_send_auth_type_failure(ifp
->name
);
1905 rip_peer_bad_packet(rip
, &from
);
1908 } else if (ri
->auth_type
!= RIP_NO_AUTH
) {
1909 const char *auth_desc
;
1912 /* There definitely is no authentication in the packet.
1914 if (IS_RIP_DEBUG_PACKET
)
1916 "RIPv2 authentication failed: no auth RTE in packet");
1917 ripd_notif_send_auth_type_failure(ifp
->name
);
1918 rip_peer_bad_packet(rip
, &from
);
1922 /* First RTE must be an Authentication Family RTE */
1923 if (packet
->rte
->family
!= htons(RIP_FAMILY_AUTH
)) {
1924 if (IS_RIP_DEBUG_PACKET
)
1927 " dropped because authentication enabled");
1928 ripd_notif_send_auth_type_failure(ifp
->name
);
1929 rip_peer_bad_packet(rip
, &from
);
1933 /* Check RIPv2 authentication. */
1934 switch (ntohs(packet
->rte
->tag
)) {
1935 case RIP_AUTH_SIMPLE_PASSWORD
:
1936 auth_desc
= "simple";
1937 ret
= rip_auth_simple_password(packet
->rte
, &from
, ifp
);
1942 ret
= rip_auth_md5(packet
, &from
, len
, ifp
);
1943 /* Reset RIP packet length to trim MD5 data. */
1949 auth_desc
= "unknown type";
1950 if (IS_RIP_DEBUG_PACKET
)
1952 "RIPv2 Unknown authentication type %d",
1953 ntohs(packet
->rte
->tag
));
1957 if (IS_RIP_DEBUG_PACKET
)
1958 zlog_debug("RIPv2 %s authentication success",
1961 if (IS_RIP_DEBUG_PACKET
)
1962 zlog_debug("RIPv2 %s authentication failure",
1964 ripd_notif_send_auth_failure(ifp
->name
);
1965 rip_peer_bad_packet(rip
, &from
);
1970 /* Process each command. */
1971 switch (packet
->command
) {
1973 rip_response_process(packet
, len
, &from
, ifc
);
1977 rip_request_process(packet
, len
, &from
, ifc
);
1982 "Obsolete command %s received, please sent it to routed",
1983 lookup_msg(rip_msg
, packet
->command
, NULL
));
1984 rip_peer_bad_packet(rip
, &from
);
1986 case RIP_POLL_ENTRY
:
1987 zlog_info("Obsolete command %s received",
1988 lookup_msg(rip_msg
, packet
->command
, NULL
));
1989 rip_peer_bad_packet(rip
, &from
);
1992 zlog_info("Unknown RIP command %d received", packet
->command
);
1993 rip_peer_bad_packet(rip
, &from
);
2000 /* Write routing table entry to the stream and return next index of
2001 the routing table entry in the stream. */
2002 static int rip_write_rte(int num
, struct stream
*s
, struct prefix_ipv4
*p
,
2003 uint8_t version
, struct rip_info
*rinfo
)
2005 struct in_addr mask
;
2007 /* Write routing table entry. */
2008 if (version
== RIPv1
) {
2009 stream_putw(s
, AF_INET
);
2011 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2012 stream_put_ipv4(s
, 0);
2013 stream_put_ipv4(s
, 0);
2014 stream_putl(s
, rinfo
->metric_out
);
2016 masklen2ip(p
->prefixlen
, &mask
);
2018 stream_putw(s
, AF_INET
);
2019 stream_putw(s
, rinfo
->tag_out
);
2020 stream_put_ipv4(s
, p
->prefix
.s_addr
);
2021 stream_put_ipv4(s
, mask
.s_addr
);
2022 stream_put_ipv4(s
, rinfo
->nexthop_out
.s_addr
);
2023 stream_putl(s
, rinfo
->metric_out
);
2029 /* Send update to the ifp or spcified neighbor. */
2030 void rip_output_process(struct connected
*ifc
, struct sockaddr_in
*to
,
2031 int route_type
, uint8_t version
)
2036 struct route_node
*rp
;
2037 struct rip_info
*rinfo
;
2038 struct rip_interface
*ri
;
2039 struct prefix_ipv4
*p
;
2040 struct prefix_ipv4 classfull
;
2041 struct prefix_ipv4 ifaddrclass
;
2042 struct key
*key
= NULL
;
2043 /* this might need to made dynamic if RIP ever supported auth methods
2044 with larger key string sizes */
2045 char auth_str
[RIP_AUTH_SIMPLE_SIZE
];
2046 size_t doff
= 0; /* offset of digest offset field */
2050 struct list
*list
= NULL
;
2051 struct listnode
*listnode
= NULL
;
2053 /* Logging output event. */
2054 if (IS_RIP_DEBUG_EVENT
) {
2056 zlog_debug("update routes to neighbor %s",
2057 inet_ntoa(to
->sin_addr
));
2059 zlog_debug("update routes on interface %s ifindex %d",
2060 ifc
->ifp
->name
, ifc
->ifp
->ifindex
);
2063 /* Get RIP interface. */
2064 ri
= ifc
->ifp
->info
;
2067 /* Set output stream. */
2070 /* Reset stream and RTE counter. */
2072 rtemax
= RIP_MAX_RTE
;
2074 /* If output interface is in simple password authentication mode, we
2075 need space for authentication data. */
2076 if (ri
->auth_type
== RIP_AUTH_SIMPLE_PASSWORD
)
2079 /* If output interface is in MD5 authentication mode, we need space
2080 for authentication header and data. */
2081 if (ri
->auth_type
== RIP_AUTH_MD5
)
2084 /* If output interface is in simple password authentication mode
2085 and string or keychain is specified we need space for auth. data */
2086 if (ri
->auth_type
!= RIP_NO_AUTH
) {
2087 if (ri
->key_chain
) {
2088 struct keychain
*keychain
;
2090 keychain
= keychain_lookup(ri
->key_chain
);
2092 key
= key_lookup_for_send(keychain
);
2094 /* to be passed to auth functions later */
2095 rip_auth_prepare_str_send(ri
, key
, auth_str
, sizeof(auth_str
));
2096 if (strlen(auth_str
) == 0)
2100 if (version
== RIPv1
) {
2101 memcpy(&ifaddrclass
, ifc
->address
, sizeof(struct prefix_ipv4
));
2102 apply_classful_mask_ipv4(&ifaddrclass
);
2104 if (ifc
->address
->prefixlen
> ifaddrclass
.prefixlen
)
2108 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2109 if ((list
= rp
->info
) != NULL
&& listcount(list
) != 0) {
2110 rinfo
= listgetdata(listhead(list
));
2111 /* For RIPv1, if we are subnetted, output subnets in our
2113 /* that have the same mask as the output "interface".
2115 /* networks, only the classfull version is output. */
2117 if (version
== RIPv1
) {
2118 p
= (struct prefix_ipv4
*)&rp
->p
;
2120 if (IS_RIP_DEBUG_PACKET
)
2122 "RIPv1 mask check, %s/%d considered for output",
2123 inet_ntoa(rp
->p
.u
.prefix4
),
2128 (struct prefix
*)&ifaddrclass
,
2130 if ((ifc
->address
->prefixlen
2132 && (rp
->p
.prefixlen
!= 32))
2135 memcpy(&classfull
, &rp
->p
,
2136 sizeof(struct prefix_ipv4
));
2137 apply_classful_mask_ipv4(&classfull
);
2138 if (rp
->p
.u
.prefix4
.s_addr
!= 0
2139 && classfull
.prefixlen
2143 if (IS_RIP_DEBUG_PACKET
)
2145 "RIPv1 mask check, %s/%d made it through",
2146 inet_ntoa(rp
->p
.u
.prefix4
),
2149 p
= (struct prefix_ipv4
*)&rp
->p
;
2151 /* Apply output filters. */
2152 ret
= rip_filter(RIP_FILTER_OUT
, p
, ri
);
2156 /* Changed route only output. */
2157 if (route_type
== rip_changed_route
2158 && (!(rinfo
->flags
& RIP_RTF_CHANGED
)))
2161 /* Split horizon. */
2162 /* if (split_horizon == rip_split_horizon) */
2163 if (ri
->split_horizon
== RIP_SPLIT_HORIZON
) {
2165 * We perform split horizon for RIP and
2167 * For rip routes, we want to suppress the route
2169 * end up sending the route back on the
2171 * learned it from, with a higher metric. For
2173 * we suppress the route if the prefix is a
2175 * source address that we are going to use for
2177 * (in order to handle the case when multiple
2179 * configured on the same interface).
2182 struct rip_info
*tmp_rinfo
= NULL
;
2183 struct connected
*tmp_ifc
= NULL
;
2185 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2187 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2188 && tmp_rinfo
->nh
.ifindex
2189 == ifc
->ifp
->ifindex
) {
2195 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2196 for (ALL_LIST_ELEMENTS_RO(
2197 ifc
->ifp
->connected
,
2201 tmp_ifc
->address
)) {
2211 /* Preparation for route-map. */
2212 rinfo
->metric_set
= 0;
2213 rinfo
->nexthop_out
.s_addr
= 0;
2214 rinfo
->metric_out
= rinfo
->metric
;
2215 rinfo
->tag_out
= rinfo
->tag
;
2216 rinfo
->ifindex_out
= ifc
->ifp
->ifindex
;
2218 /* In order to avoid some local loops,
2219 * if the RIP route has a nexthop via this interface,
2221 * otherwise set it to 0. The nexthop should not be
2223 * beyond the local broadcast/multicast area in order
2224 * to avoid an IGP multi-level recursive look-up.
2227 if (rinfo
->nh
.ifindex
== ifc
->ifp
->ifindex
)
2228 rinfo
->nexthop_out
= rinfo
->nh
.gate
.ipv4
;
2230 /* Interface route-map */
2231 if (ri
->routemap
[RIP_FILTER_OUT
]) {
2232 ret
= route_map_apply(
2233 ri
->routemap
[RIP_FILTER_OUT
],
2234 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2236 if (ret
== RMAP_DENYMATCH
) {
2237 if (IS_RIP_DEBUG_PACKET
)
2239 "RIP %s/%d is filtered by route-map out",
2240 inet_ntoa(p
->prefix
),
2246 /* Apply redistribute route map - continue, if deny */
2247 if (rip
->redist
[rinfo
->type
].route_map
.name
2248 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2249 ret
= route_map_apply(
2250 rip
->redist
[rinfo
->type
].route_map
.map
,
2251 (struct prefix
*)p
, RMAP_RIP
, rinfo
);
2253 if (ret
== RMAP_DENYMATCH
) {
2254 if (IS_RIP_DEBUG_PACKET
)
2256 "%s/%d is filtered by route-map",
2257 inet_ntoa(p
->prefix
),
2263 /* When route-map does not set metric. */
2264 if (!rinfo
->metric_set
) {
2265 /* If redistribute metric is set. */
2266 if (rip
->redist
[rinfo
->type
].metric_config
2267 && rinfo
->metric
!= RIP_METRIC_INFINITY
) {
2269 rip
->redist
[rinfo
->type
].metric
;
2271 /* If the route is not connected or
2273 one, use default-metric value*/
2274 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
2276 != ZEBRA_ROUTE_CONNECT
2278 != RIP_METRIC_INFINITY
)
2280 rip
->default_metric
;
2284 /* Apply offset-list */
2285 if (rinfo
->metric
!= RIP_METRIC_INFINITY
)
2286 rip_offset_list_apply_out(p
, ifc
->ifp
,
2287 &rinfo
->metric_out
);
2289 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
2290 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
2292 /* Perform split-horizon with poisoned reverse
2293 * for RIP and connected routes.
2295 if (ri
->split_horizon
2296 == RIP_SPLIT_HORIZON_POISONED_REVERSE
) {
2298 * We perform split horizon for RIP and
2300 * For rip routes, we want to suppress the route
2302 * end up sending the route back on the
2304 * learned it from, with a higher metric. For
2306 * we suppress the route if the prefix is a
2308 * source address that we are going to use for
2310 * (in order to handle the case when multiple
2312 * configured on the same interface).
2314 struct rip_info
*tmp_rinfo
= NULL
;
2315 struct connected
*tmp_ifc
= NULL
;
2317 for (ALL_LIST_ELEMENTS_RO(list
, listnode
,
2319 if (tmp_rinfo
->type
== ZEBRA_ROUTE_RIP
2320 && tmp_rinfo
->nh
.ifindex
2321 == ifc
->ifp
->ifindex
)
2323 RIP_METRIC_INFINITY
;
2325 if (rinfo
->metric_out
!= RIP_METRIC_INFINITY
2326 && rinfo
->type
== ZEBRA_ROUTE_CONNECT
) {
2327 for (ALL_LIST_ELEMENTS_RO(
2328 ifc
->ifp
->connected
,
2332 tmp_ifc
->address
)) {
2334 RIP_METRIC_INFINITY
;
2340 /* Prepare preamble, auth headers, if needs be */
2342 stream_putc(s
, RIP_RESPONSE
);
2343 stream_putc(s
, version
);
2346 /* auth header for !v1 && !no_auth */
2347 if ((ri
->auth_type
!= RIP_NO_AUTH
)
2348 && (version
!= RIPv1
))
2349 doff
= rip_auth_header_write(
2350 s
, ri
, key
, auth_str
,
2351 RIP_AUTH_SIMPLE_SIZE
);
2354 /* Write RTE to the stream. */
2355 num
= rip_write_rte(num
, s
, p
, version
, rinfo
);
2356 if (num
== rtemax
) {
2357 if (version
== RIPv2
2358 && ri
->auth_type
== RIP_AUTH_MD5
)
2359 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2360 RIP_AUTH_SIMPLE_SIZE
);
2362 ret
= rip_send_packet(STREAM_DATA(s
),
2363 stream_get_endp(s
), to
,
2366 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2367 rip_packet_dump((struct rip_packet
*)
2376 /* Flush unwritten RTE. */
2378 if (version
== RIPv2
&& ri
->auth_type
== RIP_AUTH_MD5
)
2379 rip_auth_md5_set(s
, ri
, doff
, auth_str
,
2380 RIP_AUTH_SIMPLE_SIZE
);
2382 ret
= rip_send_packet(STREAM_DATA(s
), stream_get_endp(s
), to
,
2385 if (ret
>= 0 && IS_RIP_DEBUG_SEND
)
2386 rip_packet_dump((struct rip_packet
*)STREAM_DATA(s
),
2387 stream_get_endp(s
), "SEND");
2391 /* Statistics updates. */
2395 /* Send RIP packet to the interface. */
2396 static void rip_update_interface(struct connected
*ifc
, uint8_t version
,
2399 struct interface
*ifp
= ifc
->ifp
;
2400 struct rip_interface
*ri
= ifp
->info
;
2401 struct sockaddr_in to
;
2403 /* When RIP version is 2 and multicast enable interface. */
2404 if (version
== RIPv2
&& !ri
->v2_broadcast
&& if_is_multicast(ifp
)) {
2405 if (IS_RIP_DEBUG_EVENT
)
2406 zlog_debug("multicast announce on %s ", ifp
->name
);
2408 rip_output_process(ifc
, NULL
, route_type
, version
);
2412 /* If we can't send multicast packet, send it with unicast. */
2413 if (if_is_broadcast(ifp
) || if_is_pointopoint(ifp
)) {
2414 if (ifc
->address
->family
== AF_INET
) {
2415 /* Destination address and port setting. */
2416 memset(&to
, 0, sizeof(struct sockaddr_in
));
2417 if (ifc
->destination
)
2418 /* use specified broadcast or peer destination
2420 to
.sin_addr
= ifc
->destination
->u
.prefix4
;
2421 else if (ifc
->address
->prefixlen
< IPV4_MAX_PREFIXLEN
)
2422 /* calculate the appropriate broadcast address
2424 to
.sin_addr
.s_addr
= ipv4_broadcast_addr(
2425 ifc
->address
->u
.prefix4
.s_addr
,
2426 ifc
->address
->prefixlen
);
2428 /* do not know where to send the packet */
2430 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2432 if (IS_RIP_DEBUG_EVENT
)
2433 zlog_debug("%s announce to %s on %s",
2434 CONNECTED_PEER(ifc
) ? "unicast"
2436 inet_ntoa(to
.sin_addr
), ifp
->name
);
2438 rip_output_process(ifc
, &to
, route_type
, version
);
2443 /* Update send to all interface and neighbor. */
2444 static void rip_update_process(struct rip
*rip
, int route_type
)
2446 struct listnode
*ifnode
, *ifnnode
;
2447 struct connected
*connected
;
2448 struct interface
*ifp
;
2449 struct rip_interface
*ri
;
2450 struct route_node
*rp
;
2451 struct sockaddr_in to
;
2454 /* Send RIP update to each interface. */
2455 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
2456 if (if_is_loopback(ifp
))
2459 if (!if_is_operative(ifp
))
2462 /* Fetch RIP interface information. */
2465 /* When passive interface is specified, suppress announce to the
2472 * If there is no version configuration in the
2474 * use rip's version setting.
2476 int vsend
= ((ri
->ri_send
== RI_RIP_UNSPEC
)
2480 if (IS_RIP_DEBUG_EVENT
)
2481 zlog_debug("SEND UPDATE to %s ifindex %d",
2482 ifp
->name
, ifp
->ifindex
);
2484 /* send update on each connected network */
2485 for (ALL_LIST_ELEMENTS(ifp
->connected
, ifnode
, ifnnode
,
2487 if (connected
->address
->family
== AF_INET
) {
2489 rip_update_interface(
2493 && if_is_multicast(ifp
))
2494 rip_update_interface(
2502 /* RIP send updates to each neighbor. */
2503 for (rp
= route_top(rip
->neighbor
); rp
; rp
= route_next(rp
))
2504 if (rp
->info
!= NULL
) {
2507 connected
= if_lookup_address(&p
->u
.prefix4
, AF_INET
,
2511 "Neighbor %s doesn't have connected interface!",
2512 inet_ntoa(p
->u
.prefix4
));
2516 /* Set destination address and port */
2517 memset(&to
, 0, sizeof(struct sockaddr_in
));
2518 to
.sin_addr
= p
->u
.prefix4
;
2519 to
.sin_port
= htons(RIP_PORT_DEFAULT
);
2521 /* RIP version is rip's configuration. */
2522 rip_output_process(connected
, &to
, route_type
,
2527 /* RIP's periodical timer. */
2528 static int rip_update(struct thread
*t
)
2530 struct rip
*rip
= THREAD_ARG(t
);
2532 /* Clear timer pointer. */
2533 rip
->t_update
= NULL
;
2535 if (IS_RIP_DEBUG_EVENT
)
2536 zlog_debug("update timer fire!");
2538 /* Process update output. */
2539 rip_update_process(rip
, rip_all_route
);
2541 /* Triggered updates may be suppressed if a regular update is due by
2542 the time the triggered update would be sent. */
2543 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2546 /* Register myself. */
2547 rip_event(rip
, RIP_UPDATE_EVENT
, 0);
2552 /* Walk down the RIP routing table then clear changed flag. */
2553 static void rip_clear_changed_flag(struct rip
*rip
)
2555 struct route_node
*rp
;
2556 struct rip_info
*rinfo
= NULL
;
2557 struct list
*list
= NULL
;
2558 struct listnode
*listnode
= NULL
;
2560 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2561 if ((list
= rp
->info
) != NULL
)
2562 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
2563 UNSET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2564 /* This flag can be set only on the first entry.
2570 /* Triggered update interval timer. */
2571 static int rip_triggered_interval(struct thread
*t
)
2573 struct rip
*rip
= THREAD_ARG(t
);
2575 rip
->t_triggered_interval
= NULL
;
2579 rip_triggered_update(t
);
2584 /* Execute triggered update. */
2585 static int rip_triggered_update(struct thread
*t
)
2587 struct rip
*rip
= THREAD_ARG(t
);
2590 /* Clear thred pointer. */
2591 rip
->t_triggered_update
= NULL
;
2593 /* Cancel interval timer. */
2594 RIP_TIMER_OFF(rip
->t_triggered_interval
);
2597 /* Logging triggered update. */
2598 if (IS_RIP_DEBUG_EVENT
)
2599 zlog_debug("triggered update!");
2601 /* Split Horizon processing is done when generating triggered
2602 updates as well as normal updates (see section 2.6). */
2603 rip_update_process(rip
, rip_changed_route
);
2605 /* Once all of the triggered updates have been generated, the route
2606 change flags should be cleared. */
2607 rip_clear_changed_flag(rip
);
2609 /* After a triggered update is sent, a timer should be set for a
2610 random interval between 1 and 5 seconds. If other changes that
2611 would trigger updates occur before the timer expires, a single
2612 update is triggered when the timer expires. */
2613 interval
= (random() % 5) + 1;
2615 rip
->t_triggered_interval
= NULL
;
2616 thread_add_timer(master
, rip_triggered_interval
, rip
, interval
,
2617 &rip
->t_triggered_interval
);
2622 /* Withdraw redistributed route. */
2623 void rip_redistribute_withdraw(struct rip
*rip
, int type
)
2625 struct route_node
*rp
;
2626 struct rip_info
*rinfo
= NULL
;
2627 struct list
*list
= NULL
;
2629 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2630 if ((list
= rp
->info
) != NULL
) {
2631 rinfo
= listgetdata(listhead(list
));
2632 if (rinfo
->type
== type
2633 && rinfo
->sub_type
!= RIP_ROUTE_INTERFACE
) {
2634 /* Perform poisoned reverse. */
2635 rinfo
->metric
= RIP_METRIC_INFINITY
;
2636 RIP_TIMER_ON(rinfo
->t_garbage_collect
,
2637 rip_garbage_collect
,
2639 RIP_TIMER_OFF(rinfo
->t_timeout
);
2640 rinfo
->flags
|= RIP_RTF_CHANGED
;
2642 if (IS_RIP_DEBUG_EVENT
) {
2643 struct prefix_ipv4
*p
=
2644 (struct prefix_ipv4
*)&rp
->p
;
2647 "Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
2648 inet_ntoa(p
->prefix
),
2655 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2660 struct rip
*rip_lookup_by_vrf_id(vrf_id_t vrf_id
)
2664 vrf
= vrf_lookup_by_id(vrf_id
);
2671 struct rip
*rip_lookup_by_vrf_name(const char *vrf_name
)
2675 rip
.vrf_name
= (char *)vrf_name
;
2677 return RB_FIND(rip_instance_head
, &rip_instances
, &rip
);
2680 /* Create new RIP instance and set it to global variable. */
2681 struct rip
*rip_create(const char *vrf_name
, struct vrf
*vrf
, int socket
)
2685 rip
= XCALLOC(MTYPE_RIP
, sizeof(struct rip
));
2686 rip
->vrf_name
= XSTRDUP(MTYPE_RIP_VRF_NAME
, vrf_name
);
2688 /* Set initial value. */
2689 rip
->ecmp
= yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE
);
2690 rip
->default_metric
=
2691 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE
);
2693 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE
);
2694 rip
->passive_default
=
2695 yang_get_default_bool("%s/passive-default", RIP_INSTANCE
);
2696 rip
->garbage_time
= yang_get_default_uint32("%s/timers/flush-interval",
2698 rip
->timeout_time
= yang_get_default_uint32(
2699 "%s/timers/holddown-interval", RIP_INSTANCE
);
2700 rip
->update_time
= yang_get_default_uint32("%s/timers/update-interval",
2703 yang_get_default_enum("%s/version/send", RIP_INSTANCE
);
2705 yang_get_default_enum("%s/version/receive", RIP_INSTANCE
);
2707 /* Initialize RIP data structures. */
2708 rip
->table
= route_table_init();
2709 route_table_set_info(rip
->table
, rip
);
2710 rip
->neighbor
= route_table_init();
2711 rip
->peer_list
= list_new();
2712 rip
->peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;
2713 rip
->peer_list
->del
= rip_peer_list_del
;
2714 rip
->distance_table
= route_table_init();
2715 rip
->distance_table
->cleanup
= rip_distance_table_node_cleanup
;
2716 rip
->enable_interface
= vector_init(1);
2717 rip
->enable_network
= route_table_init();
2718 rip
->passive_nondefault
= vector_init(1);
2719 rip
->offset_list_master
= list_new();
2720 rip
->offset_list_master
->cmp
= (int (*)(void *, void *))offset_list_cmp
;
2721 rip
->offset_list_master
->del
= (void (*)(void *))offset_list_free
;
2723 /* Distribute list install. */
2724 rip
->distribute_ctx
= distribute_list_ctx_create(vrf
);
2725 distribute_list_add_hook(rip
->distribute_ctx
, rip_distribute_update
);
2726 distribute_list_delete_hook(rip
->distribute_ctx
, rip_distribute_update
);
2728 /* if rmap install. */
2729 rip
->if_rmap_ctx
= if_rmap_ctx_create(vrf_name
);
2730 if_rmap_hook_add(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2731 if_rmap_hook_delete(rip
->if_rmap_ctx
, rip_if_rmap_update
);
2733 /* Make output stream. */
2734 rip
->obuf
= stream_new(1500);
2736 /* Enable the routing instance if possible. */
2737 if (vrf
&& vrf_is_enabled(vrf
))
2738 rip_instance_enable(rip
, vrf
, socket
);
2744 RB_INSERT(rip_instance_head
, &rip_instances
, rip
);
2749 /* Sned RIP request to the destination. */
2750 int rip_request_send(struct sockaddr_in
*to
, struct interface
*ifp
,
2751 uint8_t version
, struct connected
*connected
)
2754 struct rip_packet rip_packet
;
2755 struct listnode
*node
, *nnode
;
2757 memset(&rip_packet
, 0, sizeof(rip_packet
));
2759 rip_packet
.command
= RIP_REQUEST
;
2760 rip_packet
.version
= version
;
2761 rte
= rip_packet
.rte
;
2762 rte
->metric
= htonl(RIP_METRIC_INFINITY
);
2766 * connected is only sent for ripv1 case, or when
2767 * interface does not support multicast. Caller loops
2768 * over each connected address for this case.
2770 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2772 != sizeof(rip_packet
))
2775 return sizeof(rip_packet
);
2778 /* send request on each connected network */
2779 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, connected
)) {
2780 struct prefix_ipv4
*p
;
2782 p
= (struct prefix_ipv4
*)connected
->address
;
2784 if (p
->family
!= AF_INET
)
2787 if (rip_send_packet((uint8_t *)&rip_packet
, sizeof(rip_packet
),
2789 != sizeof(rip_packet
))
2792 return sizeof(rip_packet
);
2795 static int rip_update_jitter(unsigned long time
)
2797 #define JITTER_BOUND 4
2798 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2799 Given that, we cannot let time be less than JITTER_BOUND seconds.
2800 The RIPv2 RFC says jitter should be small compared to
2801 update_time. We consider 1/JITTER_BOUND to be small.
2804 int jitter_input
= time
;
2807 if (jitter_input
< JITTER_BOUND
)
2808 jitter_input
= JITTER_BOUND
;
2810 jitter
= (((random() % ((jitter_input
* 2) + 1)) - jitter_input
));
2812 return jitter
/ JITTER_BOUND
;
2815 void rip_event(struct rip
*rip
, enum rip_event event
, int sock
)
2822 thread_add_read(master
, rip_read
, rip
, sock
, &rip
->t_read
);
2824 case RIP_UPDATE_EVENT
:
2825 RIP_TIMER_OFF(rip
->t_update
);
2826 jitter
= rip_update_jitter(rip
->update_time
);
2827 thread_add_timer(master
, rip_update
, rip
,
2828 sock
? 2 : rip
->update_time
+ jitter
,
2831 case RIP_TRIGGERED_UPDATE
:
2832 if (rip
->t_triggered_interval
)
2835 thread_add_event(master
, rip_triggered_update
, rip
, 0,
2836 &rip
->t_triggered_update
);
2845 rip_update_default_metric (void)
2847 struct route_node
*np
;
2848 struct rip_info
*rinfo
= NULL
;
2849 struct list
*list
= NULL
;
2850 struct listnode
*listnode
= NULL
;
2852 for (np
= route_top (rip
->table
); np
; np
= route_next (np
))
2853 if ((list
= np
->info
) != NULL
)
2854 for (ALL_LIST_ELEMENTS_RO (list
, listnode
, rinfo
))
2855 if (rinfo
->type
!= ZEBRA_ROUTE_RIP
&& rinfo
->type
!= ZEBRA_ROUTE_CONNECT
)
2856 rinfo
->metric
= rip
->default_metric
;
2860 struct rip_distance
*rip_distance_new(void)
2862 return XCALLOC(MTYPE_RIP_DISTANCE
, sizeof(struct rip_distance
));
2865 void rip_distance_free(struct rip_distance
*rdistance
)
2867 if (rdistance
->access_list
)
2868 free(rdistance
->access_list
);
2869 XFREE(MTYPE_RIP_DISTANCE
, rdistance
);
2872 static void rip_distance_table_node_cleanup(struct route_table
*table
,
2873 struct route_node
*node
)
2875 struct rip_distance
*rdistance
;
2877 rdistance
= node
->info
;
2879 rip_distance_free(rdistance
);
2882 /* Apply RIP information to distance method. */
2883 uint8_t rip_distance_apply(struct rip
*rip
, struct rip_info
*rinfo
)
2885 struct route_node
*rn
;
2886 struct prefix_ipv4 p
;
2887 struct rip_distance
*rdistance
;
2888 struct access_list
*alist
;
2890 memset(&p
, 0, sizeof(struct prefix_ipv4
));
2892 p
.prefix
= rinfo
->from
;
2893 p
.prefixlen
= IPV4_MAX_BITLEN
;
2895 /* Check source address. */
2896 rn
= route_node_match(rip
->distance_table
, (struct prefix
*)&p
);
2898 rdistance
= rn
->info
;
2899 route_unlock_node(rn
);
2901 if (rdistance
->access_list
) {
2902 alist
= access_list_lookup(AFI_IP
,
2903 rdistance
->access_list
);
2906 if (access_list_apply(alist
, &rinfo
->rp
->p
)
2910 return rdistance
->distance
;
2912 return rdistance
->distance
;
2916 return rip
->distance
;
2921 static void rip_distance_show(struct vty
*vty
, struct rip
*rip
)
2923 struct route_node
*rn
;
2924 struct rip_distance
*rdistance
;
2928 vty_out(vty
, " Distance: (default is %u)\n",
2929 rip
->distance
? rip
->distance
: ZEBRA_RIP_DISTANCE_DEFAULT
);
2931 for (rn
= route_top(rip
->distance_table
); rn
; rn
= route_next(rn
))
2932 if ((rdistance
= rn
->info
) != NULL
) {
2935 " Address Distance List\n");
2938 sprintf(buf
, "%s/%d", inet_ntoa(rn
->p
.u
.prefix4
),
2940 vty_out(vty
, " %-20s %4d %s\n", buf
,
2941 rdistance
->distance
,
2942 rdistance
->access_list
? rdistance
->access_list
2947 /* Update ECMP routes to zebra when ECMP is disabled. */
2948 void rip_ecmp_disable(struct rip
*rip
)
2950 struct route_node
*rp
;
2951 struct rip_info
*rinfo
, *tmp_rinfo
;
2953 struct listnode
*node
, *nextnode
;
2955 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
))
2956 if ((list
= rp
->info
) != NULL
&& listcount(list
) > 1) {
2957 rinfo
= listgetdata(listhead(list
));
2958 if (!rip_route_rte(rinfo
))
2961 /* Drop all other entries, except the first one. */
2962 for (ALL_LIST_ELEMENTS(list
, node
, nextnode
, tmp_rinfo
))
2963 if (tmp_rinfo
!= rinfo
) {
2964 RIP_TIMER_OFF(tmp_rinfo
->t_timeout
);
2966 tmp_rinfo
->t_garbage_collect
);
2967 list_delete_node(list
, node
);
2968 rip_info_free(tmp_rinfo
);
2972 rip_zebra_ipv4_add(rip
, rp
);
2974 /* Set the route change flag. */
2975 SET_FLAG(rinfo
->flags
, RIP_RTF_CHANGED
);
2977 /* Signal the output process to trigger an update. */
2978 rip_event(rip
, RIP_TRIGGERED_UPDATE
, 0);
2982 /* Print out routes update time. */
2983 static void rip_vty_out_uptime(struct vty
*vty
, struct rip_info
*rinfo
)
2988 char timebuf
[TIME_BUF
];
2989 struct thread
*thread
;
2991 if ((thread
= rinfo
->t_timeout
) != NULL
) {
2992 clock
= thread_timer_remain_second(thread
);
2993 tm
= gmtime(&clock
);
2994 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
2995 vty_out(vty
, "%5s", timebuf
);
2996 } else if ((thread
= rinfo
->t_garbage_collect
) != NULL
) {
2997 clock
= thread_timer_remain_second(thread
);
2998 tm
= gmtime(&clock
);
2999 strftime(timebuf
, TIME_BUF
, "%M:%S", tm
);
3000 vty_out(vty
, "%5s", timebuf
);
3004 static const char *rip_route_type_print(int sub_type
)
3009 case RIP_ROUTE_STATIC
:
3011 case RIP_ROUTE_DEFAULT
:
3013 case RIP_ROUTE_REDISTRIBUTE
:
3015 case RIP_ROUTE_INTERFACE
:
3024 "show ip rip [vrf NAME]",
3031 struct route_node
*np
;
3032 struct rip_info
*rinfo
= NULL
;
3033 struct list
*list
= NULL
;
3034 struct listnode
*listnode
= NULL
;
3035 const char *vrf_name
;
3038 if (argv_find(argv
, argc
, "vrf", &idx
))
3039 vrf_name
= argv
[idx
+ 1]->arg
;
3041 vrf_name
= VRF_DEFAULT_NAME
;
3043 rip
= rip_lookup_by_vrf_name(vrf_name
);
3045 vty_out(vty
, "%% RIP instance not found\n");
3048 if (!rip
->enabled
) {
3049 vty_out(vty
, "%% RIP instance is disabled\n");
3054 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
3056 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
3057 " (i) - interface\n\n"
3058 " Network Next Hop Metric From Tag Time\n");
3060 for (np
= route_top(rip
->table
); np
; np
= route_next(np
))
3061 if ((list
= np
->info
) != NULL
)
3062 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3066 vty
, "%c(%s) %s/%d",
3067 /* np->lock, For debugging. */
3068 zebra_route_char(rinfo
->type
),
3069 rip_route_type_print(rinfo
->sub_type
),
3070 inet_ntoa(np
->p
.u
.prefix4
),
3076 vty_out(vty
, "%*s", len
, " ");
3078 switch (rinfo
->nh
.type
) {
3079 case NEXTHOP_TYPE_IPV4
:
3080 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3081 vty_out(vty
, "%-20s %2d ",
3082 inet_ntoa(rinfo
->nh
.gate
.ipv4
),
3085 case NEXTHOP_TYPE_IFINDEX
:
3090 case NEXTHOP_TYPE_BLACKHOLE
:
3095 case NEXTHOP_TYPE_IPV6
:
3096 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3098 "V6 Address Hidden %2d ",
3103 /* Route which exist in kernel routing table. */
3104 if ((rinfo
->type
== ZEBRA_ROUTE_RIP
)
3105 && (rinfo
->sub_type
== RIP_ROUTE_RTE
)) {
3106 vty_out(vty
, "%-15s ",
3107 inet_ntoa(rinfo
->from
));
3108 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3109 (route_tag_t
)rinfo
->tag
);
3110 rip_vty_out_uptime(vty
, rinfo
);
3111 } else if (rinfo
->metric
3112 == RIP_METRIC_INFINITY
) {
3113 vty_out(vty
, "self ");
3114 vty_out(vty
, "%3" ROUTE_TAG_PRI
" ",
3115 (route_tag_t
)rinfo
->tag
);
3116 rip_vty_out_uptime(vty
, rinfo
);
3118 if (rinfo
->external_metric
) {
3120 vty
, "self (%s:%d)",
3123 rinfo
->external_metric
);
3126 vty_out(vty
, "%*s", len
,
3131 vty_out(vty
, "%3" ROUTE_TAG_PRI
,
3132 (route_tag_t
)rinfo
->tag
);
3140 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3141 DEFUN (show_ip_rip_status
,
3142 show_ip_rip_status_cmd
,
3143 "show ip rip [vrf NAME] status",
3148 "IP routing protocol process parameters and statistics\n")
3151 struct interface
*ifp
;
3152 struct rip_interface
*ri
;
3153 extern const struct message ri_version_msg
[];
3154 const char *send_version
;
3155 const char *receive_version
;
3156 const char *vrf_name
;
3159 if (argv_find(argv
, argc
, "vrf", &idx
))
3160 vrf_name
= argv
[idx
+ 1]->arg
;
3162 vrf_name
= VRF_DEFAULT_NAME
;
3164 rip
= rip_lookup_by_vrf_name(vrf_name
);
3166 vty_out(vty
, "%% RIP instance not found\n");
3169 if (!rip
->enabled
) {
3170 vty_out(vty
, "%% RIP instance is disabled\n");
3174 vty_out(vty
, "Routing Protocol is \"rip\"\n");
3175 vty_out(vty
, " Sending updates every %u seconds with +/-50%%,",
3177 vty_out(vty
, " next due in %lu seconds\n",
3178 thread_timer_remain_second(rip
->t_update
));
3179 vty_out(vty
, " Timeout after %u seconds,", rip
->timeout_time
);
3180 vty_out(vty
, " garbage collect after %u seconds\n", rip
->garbage_time
);
3182 /* Filtering status show. */
3183 config_show_distribute(vty
, rip
->distribute_ctx
);
3185 /* Default metric information. */
3186 vty_out(vty
, " Default redistribution metric is %u\n",
3187 rip
->default_metric
);
3189 /* Redistribute information. */
3190 vty_out(vty
, " Redistributing:");
3191 rip_show_redistribute_config(vty
, rip
);
3194 vty_out(vty
, " Default version control: send version %s,",
3195 lookup_msg(ri_version_msg
, rip
->version_send
, NULL
));
3196 if (rip
->version_recv
== RI_RIP_VERSION_1_AND_2
)
3197 vty_out(vty
, " receive any version \n");
3199 vty_out(vty
, " receive version %s \n",
3200 lookup_msg(ri_version_msg
, rip
->version_recv
, NULL
));
3202 vty_out(vty
, " Interface Send Recv Key-chain\n");
3204 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3210 if (ri
->enable_network
|| ri
->enable_interface
) {
3211 if (ri
->ri_send
== RI_RIP_UNSPEC
)
3213 lookup_msg(ri_version_msg
,
3214 rip
->version_send
, NULL
);
3216 send_version
= lookup_msg(ri_version_msg
,
3219 if (ri
->ri_receive
== RI_RIP_UNSPEC
)
3221 lookup_msg(ri_version_msg
,
3222 rip
->version_recv
, NULL
);
3224 receive_version
= lookup_msg(
3225 ri_version_msg
, ri
->ri_receive
, NULL
);
3227 vty_out(vty
, " %-17s%-3s %-3s %s\n", ifp
->name
,
3228 send_version
, receive_version
,
3229 ri
->key_chain
? ri
->key_chain
: "");
3233 vty_out(vty
, " Routing for Networks:\n");
3234 rip_show_network_config(vty
, rip
);
3237 int found_passive
= 0;
3238 FOR_ALL_INTERFACES (rip
->vrf
, ifp
) {
3241 if ((ri
->enable_network
|| ri
->enable_interface
)
3243 if (!found_passive
) {
3245 " Passive Interface(s):\n");
3248 vty_out(vty
, " %s\n", ifp
->name
);
3253 vty_out(vty
, " Routing Information Sources:\n");
3255 " Gateway BadPackets BadRoutes Distance Last Update\n");
3256 rip_peer_display(vty
, rip
);
3258 rip_distance_show(vty
, rip
);
3263 /* RIP configuration write function. */
3264 static int config_write_rip(struct vty
*vty
)
3269 RB_FOREACH(rip
, rip_instance_head
, &rip_instances
) {
3270 char xpath
[XPATH_MAXLEN
];
3271 struct lyd_node
*dnode
;
3273 snprintf(xpath
, sizeof(xpath
),
3274 "/frr-ripd:ripd/instance[vrf='%s']", rip
->vrf_name
);
3276 dnode
= yang_dnode_get(running_config
->dnode
, xpath
);
3279 nb_cli_show_dnode_cmds(vty
, dnode
, false);
3281 /* Distribute configuration. */
3282 config_write_distribute(vty
, rip
->distribute_ctx
);
3284 /* Interface routemap configuration */
3285 config_write_if_rmap(vty
, rip
->if_rmap_ctx
);
3293 /* RIP node structure. */
3294 static struct cmd_node rip_node
= {RIP_NODE
, "%s(config-router)# ", 1};
3296 /* Distribute-list update functions. */
3297 static void rip_distribute_update(struct distribute_ctx
*ctx
,
3298 struct distribute
*dist
)
3300 struct interface
*ifp
;
3301 struct rip_interface
*ri
;
3302 struct access_list
*alist
;
3303 struct prefix_list
*plist
;
3305 if (!ctx
->vrf
|| !dist
->ifname
)
3308 ifp
= if_lookup_by_name(dist
->ifname
, ctx
->vrf
->vrf_id
);
3314 if (dist
->list
[DISTRIBUTE_V4_IN
]) {
3315 alist
= access_list_lookup(AFI_IP
,
3316 dist
->list
[DISTRIBUTE_V4_IN
]);
3318 ri
->list
[RIP_FILTER_IN
] = alist
;
3320 ri
->list
[RIP_FILTER_IN
] = NULL
;
3322 ri
->list
[RIP_FILTER_IN
] = NULL
;
3324 if (dist
->list
[DISTRIBUTE_V4_OUT
]) {
3325 alist
= access_list_lookup(AFI_IP
,
3326 dist
->list
[DISTRIBUTE_V4_OUT
]);
3328 ri
->list
[RIP_FILTER_OUT
] = alist
;
3330 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3332 ri
->list
[RIP_FILTER_OUT
] = NULL
;
3334 if (dist
->prefix
[DISTRIBUTE_V4_IN
]) {
3335 plist
= prefix_list_lookup(AFI_IP
,
3336 dist
->prefix
[DISTRIBUTE_V4_IN
]);
3338 ri
->prefix
[RIP_FILTER_IN
] = plist
;
3340 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3342 ri
->prefix
[RIP_FILTER_IN
] = NULL
;
3344 if (dist
->prefix
[DISTRIBUTE_V4_OUT
]) {
3345 plist
= prefix_list_lookup(AFI_IP
,
3346 dist
->prefix
[DISTRIBUTE_V4_OUT
]);
3348 ri
->prefix
[RIP_FILTER_OUT
] = plist
;
3350 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3352 ri
->prefix
[RIP_FILTER_OUT
] = NULL
;
3355 void rip_distribute_update_interface(struct interface
*ifp
)
3357 struct rip_interface
*ri
= ifp
->info
;
3358 struct rip
*rip
= ri
->rip
;
3359 struct distribute
*dist
;
3363 dist
= distribute_lookup(rip
->distribute_ctx
, ifp
->name
);
3365 rip_distribute_update(rip
->distribute_ctx
, dist
);
3368 /* Update all interface's distribute list. */
3370 static void rip_distribute_update_all(struct prefix_list
*notused
)
3372 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3373 struct interface
*ifp
;
3375 FOR_ALL_INTERFACES (vrf
, ifp
)
3376 rip_distribute_update_interface(ifp
);
3379 static void rip_distribute_update_all_wrapper(struct access_list
*notused
)
3381 rip_distribute_update_all(NULL
);
3384 /* Delete all added rip route. */
3385 void rip_clean(struct rip
*rip
)
3388 rip_instance_disable(rip
);
3390 stream_free(rip
->obuf
);
3392 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
3393 if (rip
->redist
[i
].route_map
.name
)
3394 free(rip
->redist
[i
].route_map
.name
);
3396 route_table_finish(rip
->table
);
3397 route_table_finish(rip
->neighbor
);
3398 list_delete(&rip
->peer_list
);
3399 distribute_list_delete(&rip
->distribute_ctx
);
3400 if_rmap_ctx_delete(rip
->if_rmap_ctx
);
3402 rip_clean_network(rip
);
3403 rip_passive_nondefault_clean(rip
);
3404 vector_free(rip
->enable_interface
);
3405 route_table_finish(rip
->enable_network
);
3406 vector_free(rip
->passive_nondefault
);
3407 list_delete(&rip
->offset_list_master
);
3408 rip_interfaces_clean(rip
);
3409 route_table_finish(rip
->distance_table
);
3411 RB_REMOVE(rip_instance_head
, &rip_instances
, rip
);
3412 XFREE(MTYPE_RIP_VRF_NAME
, rip
->vrf_name
);
3413 XFREE(MTYPE_RIP
, rip
);
3416 static void rip_if_rmap_update(struct if_rmap_ctx
*ctx
,
3417 struct if_rmap
*if_rmap
)
3419 struct interface
*ifp
= NULL
;
3420 struct rip_interface
*ri
;
3421 struct route_map
*rmap
;
3422 struct vrf
*vrf
= NULL
;
3425 vrf
= vrf_lookup_by_name(ctx
->name
);
3427 ifp
= if_lookup_by_name(if_rmap
->ifname
, vrf
->vrf_id
);
3432 if (if_rmap
->routemap
[IF_RMAP_IN
]) {
3433 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_IN
]);
3435 ri
->routemap
[IF_RMAP_IN
] = rmap
;
3437 ri
->routemap
[IF_RMAP_IN
] = NULL
;
3439 ri
->routemap
[RIP_FILTER_IN
] = NULL
;
3441 if (if_rmap
->routemap
[IF_RMAP_OUT
]) {
3442 rmap
= route_map_lookup_by_name(if_rmap
->routemap
[IF_RMAP_OUT
]);
3444 ri
->routemap
[IF_RMAP_OUT
] = rmap
;
3446 ri
->routemap
[IF_RMAP_OUT
] = NULL
;
3448 ri
->routemap
[RIP_FILTER_OUT
] = NULL
;
3451 void rip_if_rmap_update_interface(struct interface
*ifp
)
3453 struct rip_interface
*ri
= ifp
->info
;
3454 struct rip
*rip
= ri
->rip
;
3455 struct if_rmap
*if_rmap
;
3456 struct if_rmap_ctx
*ctx
;
3460 ctx
= rip
->if_rmap_ctx
;
3463 if_rmap
= if_rmap_lookup(ctx
, ifp
->name
);
3465 rip_if_rmap_update(ctx
, if_rmap
);
3468 static void rip_routemap_update_redistribute(struct rip
*rip
)
3470 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
3471 if (rip
->redist
[i
].route_map
.name
) {
3472 rip
->redist
[i
].route_map
.map
= route_map_lookup_by_name(
3473 rip
->redist
[i
].route_map
.name
);
3474 route_map_counter_increment(
3475 rip
->redist
[i
].route_map
.map
);
3481 static void rip_routemap_update(const char *notused
)
3483 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
3485 struct interface
*ifp
;
3487 FOR_ALL_INTERFACES (vrf
, ifp
)
3488 rip_if_rmap_update_interface(ifp
);
3492 rip_routemap_update_redistribute(rip
);
3495 /* Link RIP instance to VRF. */
3496 static void rip_vrf_link(struct rip
*rip
, struct vrf
*vrf
)
3498 struct interface
*ifp
;
3501 rip
->distribute_ctx
->vrf
= vrf
;
3504 FOR_ALL_INTERFACES (vrf
, ifp
)
3505 rip_interface_sync(ifp
);
3508 /* Unlink RIP instance from VRF. */
3509 static void rip_vrf_unlink(struct rip
*rip
, struct vrf
*vrf
)
3511 struct interface
*ifp
;
3514 rip
->distribute_ctx
->vrf
= NULL
;
3517 FOR_ALL_INTERFACES (vrf
, ifp
)
3518 rip_interface_sync(ifp
);
3521 static void rip_instance_enable(struct rip
*rip
, struct vrf
*vrf
, int sock
)
3525 rip_vrf_link(rip
, vrf
);
3526 rip
->enabled
= true;
3528 /* Resend all redistribute requests. */
3529 rip_redistribute_enable(rip
);
3531 /* Create read and timer thread. */
3532 rip_event(rip
, RIP_READ
, rip
->sock
);
3533 rip_event(rip
, RIP_UPDATE_EVENT
, 1);
3535 rip_zebra_vrf_register(vrf
);
3538 static void rip_instance_disable(struct rip
*rip
)
3540 struct vrf
*vrf
= rip
->vrf
;
3541 struct route_node
*rp
;
3543 /* Clear RIP routes */
3544 for (rp
= route_top(rip
->table
); rp
; rp
= route_next(rp
)) {
3545 struct rip_info
*rinfo
;
3547 struct listnode
*listnode
;
3549 if ((list
= rp
->info
) == NULL
)
3552 rinfo
= listgetdata(listhead(list
));
3553 if (rip_route_rte(rinfo
))
3554 rip_zebra_ipv4_delete(rip
, rp
);
3556 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
3557 RIP_TIMER_OFF(rinfo
->t_timeout
);
3558 RIP_TIMER_OFF(rinfo
->t_garbage_collect
);
3559 rip_info_free(rinfo
);
3563 route_unlock_node(rp
);
3566 /* Flush all redistribute requests. */
3567 rip_redistribute_disable(rip
);
3569 /* Cancel RIP related timers. */
3570 RIP_TIMER_OFF(rip
->t_update
);
3571 RIP_TIMER_OFF(rip
->t_triggered_update
);
3572 RIP_TIMER_OFF(rip
->t_triggered_interval
);
3574 /* Cancel read thread. */
3575 THREAD_READ_OFF(rip
->t_read
);
3577 /* Close RIP socket. */
3581 /* Clear existing peers. */
3582 list_delete_all_node(rip
->peer_list
);
3584 rip_zebra_vrf_deregister(vrf
);
3586 rip_vrf_unlink(rip
, vrf
);
3587 rip
->enabled
= false;
3590 static int rip_vrf_new(struct vrf
*vrf
)
3592 if (IS_RIP_DEBUG_EVENT
)
3593 zlog_debug("%s: VRF created: %s(%u)", __func__
, vrf
->name
,
3599 static int rip_vrf_delete(struct vrf
*vrf
)
3601 if (IS_RIP_DEBUG_EVENT
)
3602 zlog_debug("%s: VRF deleted: %s(%u)", __func__
, vrf
->name
,
3608 static int rip_vrf_enable(struct vrf
*vrf
)
3613 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3614 if (!rip
|| rip
->enabled
)
3617 if (IS_RIP_DEBUG_EVENT
)
3618 zlog_debug("%s: VRF %s(%u) enabled", __func__
, vrf
->name
,
3621 /* Activate the VRF RIP instance. */
3622 if (!rip
->enabled
) {
3623 socket
= rip_create_socket(vrf
);
3627 rip_instance_enable(rip
, vrf
, socket
);
3633 static int rip_vrf_disable(struct vrf
*vrf
)
3637 rip
= rip_lookup_by_vrf_name(vrf
->name
);
3638 if (!rip
|| !rip
->enabled
)
3641 if (IS_RIP_DEBUG_EVENT
)
3642 zlog_debug("%s: VRF %s(%u) disabled", __func__
, vrf
->name
,
3645 /* Deactivate the VRF RIP instance. */
3647 rip_instance_disable(rip
);
3652 void rip_vrf_init(void)
3654 vrf_init(rip_vrf_new
, rip_vrf_enable
, rip_vrf_disable
, rip_vrf_delete
,
3658 void rip_vrf_terminate(void)
3663 /* Allocate new rip structure and set default value. */
3666 /* Install top nodes. */
3667 install_node(&rip_node
, config_write_rip
);
3669 /* Install rip commands. */
3670 install_element(VIEW_NODE
, &show_ip_rip_cmd
);
3671 install_element(VIEW_NODE
, &show_ip_rip_status_cmd
);
3673 install_default(RIP_NODE
);
3675 /* Debug related init. */
3678 /* Access list install. */
3680 access_list_add_hook(rip_distribute_update_all_wrapper
);
3681 access_list_delete_hook(rip_distribute_update_all_wrapper
);
3683 /* Prefix list initialize.*/
3685 prefix_list_add_hook(rip_distribute_update_all
);
3686 prefix_list_delete_hook(rip_distribute_update_all
);
3688 /* Distribute list install. */
3689 distribute_list_init(RIP_NODE
);
3692 rip_route_map_init();
3694 route_map_add_hook(rip_routemap_update
);
3695 route_map_delete_hook(rip_routemap_update
);
3697 if_rmap_init(RIP_NODE
);