]> git.proxmox.com Git - mirror_frr.git/blob - ripd/ripd.c
Merge pull request #13270 from pguibert6WIND/better_srv6_output_seg6local
[mirror_frr.git] / ripd / ripd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RIP version 1 and 2.
3 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro <kunihiro@zebra.org>
5 */
6
7 #include <zebra.h>
8
9 #include "vrf.h"
10 #include "if.h"
11 #include "command.h"
12 #include "prefix.h"
13 #include "table.h"
14 #include "frrevent.h"
15 #include "memory.h"
16 #include "log.h"
17 #include "stream.h"
18 #include "filter.h"
19 #include "sockunion.h"
20 #include "sockopt.h"
21 #include "routemap.h"
22 #include "if_rmap.h"
23 #include "plist.h"
24 #include "distribute.h"
25 #ifdef CRYPTO_INTERNAL
26 #include "md5.h"
27 #endif
28 #include "keychain.h"
29 #include "privs.h"
30 #include "lib_errors.h"
31 #include "northbound_cli.h"
32 #include "network.h"
33 #include "lib/printfrr.h"
34
35 #include "ripd/ripd.h"
36 #include "ripd/rip_nb.h"
37 #include "ripd/rip_debug.h"
38 #include "ripd/rip_errors.h"
39 #include "ripd/rip_interface.h"
40
41 /* UDP receive buffer size */
42 #define RIP_UDP_RCV_BUF 41600
43
44 DEFINE_MGROUP(RIPD, "ripd");
45 DEFINE_MTYPE_STATIC(RIPD, RIP, "RIP structure");
46 DEFINE_MTYPE_STATIC(RIPD, RIP_VRF_NAME, "RIP VRF name");
47 DEFINE_MTYPE_STATIC(RIPD, RIP_INFO, "RIP route info");
48 DEFINE_MTYPE_STATIC(RIPD, RIP_DISTANCE, "RIP distance");
49
50 /* Prototypes. */
51 static void rip_output_process(struct connected *, struct sockaddr_in *, int,
52 uint8_t);
53 static void rip_triggered_update(struct event *);
54 static int rip_update_jitter(unsigned long);
55 static void rip_distance_table_node_cleanup(struct route_table *table,
56 struct route_node *node);
57 static void rip_instance_enable(struct rip *rip, struct vrf *vrf, int sock);
58 static void rip_instance_disable(struct rip *rip);
59
60 static void rip_distribute_update(struct distribute_ctx *ctx,
61 struct distribute *dist);
62
63 static void rip_if_rmap_update(struct if_rmap_ctx *ctx,
64 struct if_rmap *if_rmap);
65
66 /* RIP output routes type. */
67 enum { rip_all_route, rip_changed_route };
68
69 /* RIP command strings. */
70 static const struct message rip_msg[] = {{RIP_REQUEST, "REQUEST"},
71 {RIP_RESPONSE, "RESPONSE"},
72 {RIP_TRACEON, "TRACEON"},
73 {RIP_TRACEOFF, "TRACEOFF"},
74 {RIP_POLL, "POLL"},
75 {RIP_POLL_ENTRY, "POLL ENTRY"},
76 {0}};
77
78 /* Generate rb-tree of RIP instances. */
79 static inline int rip_instance_compare(const struct rip *a, const struct rip *b)
80 {
81 return strcmp(a->vrf_name, b->vrf_name);
82 }
83 RB_GENERATE(rip_instance_head, rip, entry, rip_instance_compare)
84
85 struct rip_instance_head rip_instances = RB_INITIALIZER(&rip_instances);
86
87 /* Utility function to set broadcast option to the socket. */
88 static int sockopt_broadcast(int sock)
89 {
90 int ret;
91 int on = 1;
92
93 ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
94 sizeof(on));
95 if (ret < 0) {
96 zlog_warn("can't set sockopt SO_BROADCAST to socket %d", sock);
97 return -1;
98 }
99 return 0;
100 }
101
102 int rip_route_rte(struct rip_info *rinfo)
103 {
104 return (rinfo->type == ZEBRA_ROUTE_RIP
105 && rinfo->sub_type == RIP_ROUTE_RTE);
106 }
107
108 static struct rip_info *rip_info_new(void)
109 {
110 return XCALLOC(MTYPE_RIP_INFO, sizeof(struct rip_info));
111 }
112
113 void rip_info_free(struct rip_info *rinfo)
114 {
115 XFREE(MTYPE_RIP_INFO, rinfo);
116 }
117
118 struct rip *rip_info_get_instance(const struct rip_info *rinfo)
119 {
120 return route_table_get_info(rinfo->rp->table);
121 }
122
123 /* RIP route garbage collect timer. */
124 static void rip_garbage_collect(struct event *t)
125 {
126 struct rip_info *rinfo;
127 struct route_node *rp;
128
129 rinfo = EVENT_ARG(t);
130
131 /* Off timeout timer. */
132 EVENT_OFF(rinfo->t_timeout);
133
134 /* Get route_node pointer. */
135 rp = rinfo->rp;
136
137 /* Unlock route_node. */
138 listnode_delete(rp->info, rinfo);
139 if (list_isempty((struct list *)rp->info)) {
140 list_delete((struct list **)&rp->info);
141 route_unlock_node(rp);
142 }
143
144 /* Free RIP routing information. */
145 rip_info_free(rinfo);
146 }
147
148 static void rip_timeout_update(struct rip *rip, struct rip_info *rinfo);
149
150 /* Add new route to the ECMP list.
151 * RETURN: the new entry added in the list, or NULL if it is not the first
152 * entry and ECMP is not allowed.
153 */
154 struct rip_info *rip_ecmp_add(struct rip *rip, struct rip_info *rinfo_new)
155 {
156 struct route_node *rp = rinfo_new->rp;
157 struct rip_info *rinfo = NULL;
158 struct list *list = NULL;
159
160 if (rp->info == NULL)
161 rp->info = list_new();
162 list = (struct list *)rp->info;
163
164 /* If ECMP is not allowed and some entry already exists in the list,
165 * do nothing. */
166 if (listcount(list) && !rip->ecmp)
167 return NULL;
168
169 rinfo = rip_info_new();
170 memcpy(rinfo, rinfo_new, sizeof(struct rip_info));
171 listnode_add(list, rinfo);
172
173 if (rip_route_rte(rinfo)) {
174 rip_timeout_update(rip, rinfo);
175 rip_zebra_ipv4_add(rip, rp);
176 }
177
178 /* Set the route change flag on the first entry. */
179 rinfo = listgetdata(listhead(list));
180 SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
181
182 /* Signal the output process to trigger an update (see section 2.5). */
183 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
184
185 return rinfo;
186 }
187
188 /* Replace the ECMP list with the new route.
189 * RETURN: the new entry added in the list
190 */
191 struct rip_info *rip_ecmp_replace(struct rip *rip, struct rip_info *rinfo_new)
192 {
193 struct route_node *rp = rinfo_new->rp;
194 struct list *list = (struct list *)rp->info;
195 struct rip_info *rinfo = NULL, *tmp_rinfo = NULL;
196 struct listnode *node = NULL, *nextnode = NULL;
197
198 if (list == NULL || listcount(list) == 0)
199 return rip_ecmp_add(rip, rinfo_new);
200
201 /* Get the first entry */
202 rinfo = listgetdata(listhead(list));
203
204 /* Learnt route replaced by a local one. Delete it from zebra. */
205 if (rip_route_rte(rinfo) && !rip_route_rte(rinfo_new))
206 if (CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
207 rip_zebra_ipv4_delete(rip, rp);
208
209 /* Re-use the first entry, and delete the others. */
210 for (ALL_LIST_ELEMENTS(list, node, nextnode, tmp_rinfo)) {
211 if (tmp_rinfo == rinfo)
212 continue;
213
214 EVENT_OFF(tmp_rinfo->t_timeout);
215 EVENT_OFF(tmp_rinfo->t_garbage_collect);
216 list_delete_node(list, node);
217 rip_info_free(tmp_rinfo);
218 }
219
220 EVENT_OFF(rinfo->t_timeout);
221 EVENT_OFF(rinfo->t_garbage_collect);
222 memcpy(rinfo, rinfo_new, sizeof(struct rip_info));
223
224 if (rip_route_rte(rinfo)) {
225 rip_timeout_update(rip, rinfo);
226 /* The ADD message implies an update. */
227 rip_zebra_ipv4_add(rip, rp);
228 }
229
230 /* Set the route change flag. */
231 SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
232
233 /* Signal the output process to trigger an update (see section 2.5). */
234 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
235
236 return rinfo;
237 }
238
239 /* Delete one route from the ECMP list.
240 * RETURN:
241 * null - the entry is freed, and other entries exist in the list
242 * the entry - the entry is the last one in the list; its metric is set
243 * to INFINITY, and the garbage collector is started for it
244 */
245 struct rip_info *rip_ecmp_delete(struct rip *rip, struct rip_info *rinfo)
246 {
247 struct route_node *rp = rinfo->rp;
248 struct list *list = (struct list *)rp->info;
249
250 EVENT_OFF(rinfo->t_timeout);
251
252 if (listcount(list) > 1) {
253 /* Some other ECMP entries still exist. Just delete this entry.
254 */
255 EVENT_OFF(rinfo->t_garbage_collect);
256 listnode_delete(list, rinfo);
257 if (rip_route_rte(rinfo)
258 && CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
259 /* The ADD message implies the update. */
260 rip_zebra_ipv4_add(rip, rp);
261 rip_info_free(rinfo);
262 rinfo = NULL;
263 } else {
264 assert(rinfo == listgetdata(listhead(list)));
265
266 /* This is the only entry left in the list. We must keep it in
267 * the list for garbage collection time, with INFINITY metric.
268 */
269
270 rinfo->metric = RIP_METRIC_INFINITY;
271 RIP_TIMER_ON(rinfo->t_garbage_collect, rip_garbage_collect,
272 rip->garbage_time);
273
274 if (rip_route_rte(rinfo)
275 && CHECK_FLAG(rinfo->flags, RIP_RTF_FIB))
276 rip_zebra_ipv4_delete(rip, rp);
277 }
278
279 /* Set the route change flag on the first entry. */
280 rinfo = listgetdata(listhead(list));
281 SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
282
283 /* Signal the output process to trigger an update (see section 2.5). */
284 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
285
286 return rinfo;
287 }
288
289 /* Timeout RIP routes. */
290 static void rip_timeout(struct event *t)
291 {
292 struct rip_info *rinfo = EVENT_ARG(t);
293 struct rip *rip = rip_info_get_instance(rinfo);
294
295 rip_ecmp_delete(rip, rinfo);
296 }
297
298 static void rip_timeout_update(struct rip *rip, struct rip_info *rinfo)
299 {
300 if (rinfo->metric != RIP_METRIC_INFINITY) {
301 EVENT_OFF(rinfo->t_timeout);
302 event_add_timer(master, rip_timeout, rinfo, rip->timeout_time,
303 &rinfo->t_timeout);
304 }
305 }
306
307 static int rip_filter(int rip_distribute, struct prefix_ipv4 *p,
308 struct rip_interface *ri)
309 {
310 struct distribute *dist;
311 struct access_list *alist;
312 struct prefix_list *plist;
313 int distribute = rip_distribute == RIP_FILTER_OUT ? DISTRIBUTE_V4_OUT
314 : DISTRIBUTE_V4_IN;
315 const char *inout = rip_distribute == RIP_FILTER_OUT ? "out" : "in";
316
317 /* Input distribute-list filtering. */
318 if (ri->list[rip_distribute] &&
319 access_list_apply(ri->list[rip_distribute], (struct prefix *)p) ==
320 FILTER_DENY) {
321 if (IS_RIP_DEBUG_PACKET)
322 zlog_debug("%pFX filtered by distribute %s", p, inout);
323 return -1;
324 }
325
326 if (ri->prefix[rip_distribute] &&
327 prefix_list_apply(ri->prefix[rip_distribute], (struct prefix *)p) ==
328 PREFIX_DENY) {
329 if (IS_RIP_DEBUG_PACKET)
330 zlog_debug("%pFX filtered by prefix-list %s", p, inout);
331 return -1;
332 }
333
334 /* All interface filter check. */
335 dist = distribute_lookup(ri->rip->distribute_ctx, NULL);
336 if (!dist)
337 return 0;
338
339 if (dist->list[distribute]) {
340 alist = access_list_lookup(AFI_IP, dist->list[distribute]);
341
342 if (alist) {
343 if (access_list_apply(alist, (struct prefix *)p) ==
344 FILTER_DENY) {
345 if (IS_RIP_DEBUG_PACKET)
346 zlog_debug(
347 "%pFX filtered by distribute %s",
348 p, inout);
349 return -1;
350 }
351 }
352 }
353 if (dist->prefix[distribute]) {
354 plist = prefix_list_lookup(AFI_IP, dist->prefix[distribute]);
355
356 if (plist) {
357 if (prefix_list_apply(plist, (struct prefix *)p) ==
358 PREFIX_DENY) {
359 if (IS_RIP_DEBUG_PACKET)
360 zlog_debug(
361 "%pFX filtered by prefix-list %s",
362 p, inout);
363 return -1;
364 }
365 }
366 }
367
368 return 0;
369 }
370
371 /* Check nexthop address validity. */
372 static int rip_nexthop_check(struct rip *rip, struct in_addr *addr)
373 {
374 struct interface *ifp;
375 struct listnode *cnode;
376 struct connected *ifc;
377 struct prefix *p;
378
379 /* If nexthop address matches local configured address then it is
380 invalid nexthop. */
381
382 FOR_ALL_INTERFACES (rip->vrf, ifp) {
383 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
384 p = ifc->address;
385
386 if (p->family == AF_INET
387 && IPV4_ADDR_SAME(&p->u.prefix4, addr))
388 return -1;
389 }
390 }
391 return 0;
392 }
393
394 /* RIP add route to routing table. */
395 static void rip_rte_process(struct rte *rte, struct sockaddr_in *from,
396 struct interface *ifp)
397 {
398 struct rip *rip;
399 int ret;
400 struct prefix_ipv4 p;
401 struct route_node *rp;
402 struct rip_info *rinfo = NULL, newinfo;
403 struct rip_interface *ri;
404 struct in_addr *nexthop;
405 int same = 0;
406 unsigned char old_dist, new_dist;
407 struct list *list = NULL;
408 struct listnode *node = NULL;
409
410 /* Make prefix structure. */
411 memset(&p, 0, sizeof(struct prefix_ipv4));
412 p.family = AF_INET;
413 p.prefix = rte->prefix;
414 p.prefixlen = ip_masklen(rte->mask);
415
416 /* Make sure mask is applied. */
417 apply_mask_ipv4(&p);
418
419 ri = ifp->info;
420 rip = ri->rip;
421
422 /* Apply input filters. */
423 ret = rip_filter(RIP_FILTER_IN, &p, ri);
424 if (ret < 0)
425 return;
426
427 memset(&newinfo, 0, sizeof(newinfo));
428 newinfo.type = ZEBRA_ROUTE_RIP;
429 newinfo.sub_type = RIP_ROUTE_RTE;
430 newinfo.nh.gate.ipv4 = rte->nexthop;
431 newinfo.from = from->sin_addr;
432 newinfo.nh.ifindex = ifp->ifindex;
433 newinfo.nh.type = NEXTHOP_TYPE_IPV4_IFINDEX;
434 newinfo.metric = rte->metric;
435 newinfo.metric_out = rte->metric; /* XXX */
436 newinfo.tag = ntohs(rte->tag); /* XXX */
437
438 /* Modify entry according to the interface routemap. */
439 if (ri->routemap[RIP_FILTER_IN]) {
440 /* The object should be of the type of rip_info */
441 ret = route_map_apply(ri->routemap[RIP_FILTER_IN],
442 (struct prefix *)&p, &newinfo);
443
444 if (ret == RMAP_DENYMATCH) {
445 if (IS_RIP_DEBUG_PACKET)
446 zlog_debug(
447 "RIP %pFX is filtered by route-map in",
448 &p);
449 return;
450 }
451
452 /* Get back the object */
453 rte->nexthop = newinfo.nexthop_out;
454 rte->tag = htons(newinfo.tag_out); /* XXX */
455 rte->metric = newinfo.metric_out; /* XXX: the routemap uses the
456 metric_out field */
457 }
458
459 /* Once the entry has been validated, update the metric by
460 adding the cost of the network on which the message
461 arrived. If the result is greater than infinity, use infinity
462 (RFC2453 Sec. 3.9.2) */
463 /* Zebra ripd can handle offset-list in. */
464 ret = rip_offset_list_apply_in(&p, ifp, &rte->metric);
465
466 /* If offset-list does not modify the metric use interface's
467 metric. */
468 if (!ret)
469 rte->metric += ifp->metric ? ifp->metric : 1;
470
471 if (rte->metric > RIP_METRIC_INFINITY)
472 rte->metric = RIP_METRIC_INFINITY;
473
474 /* Set nexthop pointer. */
475 if (rte->nexthop.s_addr == INADDR_ANY)
476 nexthop = &from->sin_addr;
477 else
478 nexthop = &rte->nexthop;
479
480 /* Check if nexthop address is myself, then do nothing. */
481 if (rip_nexthop_check(rip, nexthop) < 0) {
482 if (IS_RIP_DEBUG_PACKET)
483 zlog_debug("Nexthop address %pI4 is myself",
484 nexthop);
485 return;
486 }
487
488 /* Get index for the prefix. */
489 rp = route_node_get(rip->table, (struct prefix *)&p);
490
491 newinfo.rp = rp;
492 newinfo.nh.gate.ipv4 = *nexthop;
493 newinfo.nh.type = NEXTHOP_TYPE_IPV4;
494 newinfo.metric = rte->metric;
495 newinfo.tag = ntohs(rte->tag);
496 newinfo.distance = rip_distance_apply(rip, &newinfo);
497
498 new_dist = newinfo.distance ? newinfo.distance
499 : ZEBRA_RIP_DISTANCE_DEFAULT;
500
501 /* Check to see whether there is already RIP route on the table. */
502 if ((list = rp->info) != NULL)
503 for (ALL_LIST_ELEMENTS_RO(list, node, rinfo)) {
504 /* Need to compare with redistributed entry or local
505 * entry */
506 if (!rip_route_rte(rinfo))
507 break;
508
509 if (IPV4_ADDR_SAME(&rinfo->from, &from->sin_addr)
510 && IPV4_ADDR_SAME(&rinfo->nh.gate.ipv4, nexthop))
511 break;
512
513 if (listnextnode(node))
514 continue;
515
516 /* Not found in the list */
517
518 if (rte->metric > rinfo->metric) {
519 /* New route has a greater metric.
520 * Discard it. */
521 route_unlock_node(rp);
522 return;
523 }
524
525 if (rte->metric < rinfo->metric)
526 /* New route has a smaller metric.
527 * Replace the ECMP list
528 * with the new one in below. */
529 break;
530
531 /* Metrics are same. We compare the distances.
532 */
533 old_dist = rinfo->distance ? rinfo->distance
534 : ZEBRA_RIP_DISTANCE_DEFAULT;
535
536 if (new_dist > old_dist) {
537 /* New route has a greater distance.
538 * Discard it. */
539 route_unlock_node(rp);
540 return;
541 }
542
543 if (new_dist < old_dist)
544 /* New route has a smaller distance.
545 * Replace the ECMP list
546 * with the new one in below. */
547 break;
548
549 /* Metrics and distances are both same. Keep
550 * "rinfo" null and
551 * the new route is added in the ECMP list in
552 * below. */
553 }
554
555 if (rinfo) {
556 /* Local static route. */
557 if (rinfo->type == ZEBRA_ROUTE_RIP
558 && ((rinfo->sub_type == RIP_ROUTE_STATIC)
559 || (rinfo->sub_type == RIP_ROUTE_DEFAULT))
560 && rinfo->metric != RIP_METRIC_INFINITY) {
561 route_unlock_node(rp);
562 return;
563 }
564
565 /* Redistributed route check. */
566 if (rinfo->type != ZEBRA_ROUTE_RIP
567 && rinfo->metric != RIP_METRIC_INFINITY) {
568 old_dist = rinfo->distance;
569 /* Only routes directly connected to an interface
570 * (nexthop == 0)
571 * may have a valid NULL distance */
572 if (rinfo->nh.gate.ipv4.s_addr != INADDR_ANY)
573 old_dist = old_dist
574 ? old_dist
575 : ZEBRA_RIP_DISTANCE_DEFAULT;
576 /* If imported route does not have STRICT precedence,
577 mark it as a ghost */
578 if (new_dist <= old_dist
579 && rte->metric != RIP_METRIC_INFINITY)
580 rip_ecmp_replace(rip, &newinfo);
581
582 route_unlock_node(rp);
583 return;
584 }
585 }
586
587 if (!rinfo) {
588 if (rp->info)
589 route_unlock_node(rp);
590
591 /* Now, check to see whether there is already an explicit route
592 for the destination prefix. If there is no such route, add
593 this route to the routing table, unless the metric is
594 infinity (there is no point in adding a route which
595 unusable). */
596 if (rte->metric != RIP_METRIC_INFINITY)
597 rip_ecmp_add(rip, &newinfo);
598 } else {
599 /* Route is there but we are not sure the route is RIP or not.
600 */
601
602 /* If there is an existing route, compare the next hop address
603 to the address of the router from which the datagram came.
604 If this datagram is from the same router as the existing
605 route, reinitialize the timeout. */
606 same = (IPV4_ADDR_SAME(&rinfo->from, &from->sin_addr)
607 && (rinfo->nh.ifindex == ifp->ifindex));
608
609 old_dist = rinfo->distance ? rinfo->distance
610 : ZEBRA_RIP_DISTANCE_DEFAULT;
611
612 /* Next, compare the metrics. If the datagram is from the same
613 router as the existing route, and the new metric is different
614 than the old one; or, if the new metric is lower than the old
615 one, or if the tag has been changed; or if there is a route
616 with a lower administrave distance; or an update of the
617 distance on the actual route; do the following actions: */
618 if ((same && rinfo->metric != rte->metric)
619 || (rte->metric < rinfo->metric)
620 || ((same) && (rinfo->metric == rte->metric)
621 && (newinfo.tag != rinfo->tag))
622 || (old_dist > new_dist)
623 || ((old_dist != new_dist) && same)) {
624 if (listcount(list) == 1) {
625 if (newinfo.metric != RIP_METRIC_INFINITY)
626 rip_ecmp_replace(rip, &newinfo);
627 else
628 rip_ecmp_delete(rip, rinfo);
629 } else {
630 if (newinfo.metric < rinfo->metric)
631 rip_ecmp_replace(rip, &newinfo);
632 else if (newinfo.metric > rinfo->metric)
633 rip_ecmp_delete(rip, rinfo);
634 else if (new_dist < old_dist)
635 rip_ecmp_replace(rip, &newinfo);
636 else if (new_dist > old_dist)
637 rip_ecmp_delete(rip, rinfo);
638 else {
639 int update = CHECK_FLAG(rinfo->flags,
640 RIP_RTF_FIB)
641 ? 1
642 : 0;
643
644 assert(newinfo.metric
645 != RIP_METRIC_INFINITY);
646
647 EVENT_OFF(rinfo->t_timeout);
648 EVENT_OFF(rinfo->t_garbage_collect);
649 memcpy(rinfo, &newinfo,
650 sizeof(struct rip_info));
651 rip_timeout_update(rip, rinfo);
652
653 if (update)
654 rip_zebra_ipv4_add(rip, rp);
655
656 /* - Set the route change flag on the
657 * first entry. */
658 rinfo = listgetdata(listhead(list));
659 SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
660 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
661 }
662 }
663 } else /* same & no change */
664 rip_timeout_update(rip, rinfo);
665
666 /* Unlock tempolary lock of the route. */
667 route_unlock_node(rp);
668 }
669 }
670
671 /* Dump RIP packet */
672 static void rip_packet_dump(struct rip_packet *packet, int size,
673 const char *sndrcv)
674 {
675 caddr_t lim;
676 struct rte *rte;
677 const char *command_str;
678 uint8_t netmask = 0;
679 uint8_t *p;
680
681 /* Set command string. */
682 if (packet->command > 0 && packet->command < RIP_COMMAND_MAX)
683 command_str = lookup_msg(rip_msg, packet->command, NULL);
684 else
685 command_str = "unknown";
686
687 /* Dump packet header. */
688 zlog_debug("%s %s version %d packet size %d", sndrcv, command_str,
689 packet->version, size);
690
691 /* Dump each routing table entry. */
692 rte = packet->rte;
693
694 for (lim = (caddr_t)packet + size; (caddr_t)rte < lim; rte++) {
695 if (packet->version == RIPv2) {
696 netmask = ip_masklen(rte->mask);
697
698 if (rte->family == htons(RIP_FAMILY_AUTH)) {
699 if (rte->tag
700 == htons(RIP_AUTH_SIMPLE_PASSWORD)) {
701 p = (uint8_t *)&rte->prefix;
702
703 zlog_debug(
704 " family 0x%X type %d auth string: %s",
705 ntohs(rte->family),
706 ntohs(rte->tag), p);
707 } else if (rte->tag == htons(RIP_AUTH_MD5)) {
708 struct rip_md5_info *md5;
709
710 md5 = (struct rip_md5_info *)&packet
711 ->rte;
712
713 zlog_debug(
714 " family 0x%X type %d (MD5 authentication)",
715 ntohs(md5->family),
716 ntohs(md5->type));
717 zlog_debug(
718 " RIP-2 packet len %d Key ID %d Auth Data len %d",
719 ntohs(md5->packet_len),
720 md5->keyid, md5->auth_len);
721 zlog_debug(" Sequence Number %ld",
722 (unsigned long)ntohl(
723 md5->sequence));
724 } else if (rte->tag == htons(RIP_AUTH_DATA)) {
725 p = (uint8_t *)&rte->prefix;
726
727 zlog_debug(
728 " family 0x%X type %d (MD5 data)",
729 ntohs(rte->family),
730 ntohs(rte->tag));
731 zlog_debug(
732 " MD5: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
733 p[0], p[1], p[2], p[3], p[4],
734 p[5], p[6], p[7], p[8], p[9],
735 p[10], p[11], p[12], p[13],
736 p[14], p[15]);
737 } else {
738 zlog_debug(
739 " family 0x%X type %d (Unknown auth type)",
740 ntohs(rte->family),
741 ntohs(rte->tag));
742 }
743 } else
744 zlog_debug(
745 " %pI4/%d -> %pI4 family %d tag %" ROUTE_TAG_PRI
746 " metric %ld",
747 &rte->prefix, netmask, &rte->nexthop,
748 ntohs(rte->family),
749 (route_tag_t)ntohs(rte->tag),
750 (unsigned long)ntohl(rte->metric));
751 } else {
752 zlog_debug(" %pI4 family %d tag %" ROUTE_TAG_PRI
753 " metric %ld",
754 &rte->prefix, ntohs(rte->family),
755 (route_tag_t)ntohs(rte->tag),
756 (unsigned long)ntohl(rte->metric));
757 }
758 }
759 }
760
761 /* Check if the destination address is valid (unicast; not net 0
762 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
763 check net 0 because we accept default route. */
764 static int rip_destination_check(struct in_addr addr)
765 {
766 uint32_t destination;
767
768 /* Convert to host byte order. */
769 destination = ntohl(addr.s_addr);
770
771 if (IPV4_NET127(destination))
772 return 0;
773
774 /* Net 0 may match to the default route. */
775 if (IPV4_NET0(destination) && destination != 0)
776 return 0;
777
778 /* Unicast address must belong to class A, B, C. */
779 if (IN_CLASSA(destination))
780 return 1;
781 if (IN_CLASSB(destination))
782 return 1;
783 if (IN_CLASSC(destination))
784 return 1;
785
786 return 0;
787 }
788
789 /* RIP version 2 authentication. */
790 static int rip_auth_simple_password(struct rte *rte, struct sockaddr_in *from,
791 struct interface *ifp)
792 {
793 struct rip_interface *ri;
794 char *auth_str = (char *)rte + offsetof(struct rte, prefix);
795 int i;
796
797 /* reject passwords with zeros in the middle of the string */
798 for (i = strnlen(auth_str, 16); i < 16; i++) {
799 if (auth_str[i] != '\0')
800 return 0;
801 }
802
803 if (IS_RIP_DEBUG_EVENT)
804 zlog_debug("RIPv2 simple password authentication from %pI4",
805 &from->sin_addr);
806
807 ri = ifp->info;
808
809 if (ri->auth_type != RIP_AUTH_SIMPLE_PASSWORD
810 || rte->tag != htons(RIP_AUTH_SIMPLE_PASSWORD))
811 return 0;
812
813 /* Simple password authentication. */
814 if (ri->auth_str) {
815 if (strncmp(auth_str, ri->auth_str, 16) == 0)
816 return 1;
817 }
818 if (ri->key_chain) {
819 struct keychain *keychain;
820 struct key *key;
821
822 keychain = keychain_lookup(ri->key_chain);
823 if (keychain == NULL || keychain->key == NULL)
824 return 0;
825
826 key = key_match_for_accept(keychain, auth_str);
827 if (key)
828 return 1;
829 }
830 return 0;
831 }
832
833 /* RIP version 2 authentication with MD5. */
834 static int rip_auth_md5(struct rip_packet *packet, struct sockaddr_in *from,
835 int length, struct interface *ifp)
836 {
837 struct rip_interface *ri;
838 struct rip_md5_info *md5;
839 struct rip_md5_data *md5data;
840 struct keychain *keychain;
841 struct key *key;
842 #ifdef CRYPTO_OPENSSL
843 EVP_MD_CTX *ctx;
844 #elif CRYPTO_INTERNAL
845 MD5_CTX ctx;
846 #endif
847 uint8_t digest[RIP_AUTH_MD5_SIZE];
848 uint16_t packet_len;
849 char auth_str[RIP_AUTH_MD5_SIZE] = {};
850
851 if (IS_RIP_DEBUG_EVENT)
852 zlog_debug("RIPv2 MD5 authentication from %pI4",
853 &from->sin_addr);
854
855 ri = ifp->info;
856 md5 = (struct rip_md5_info *)&packet->rte;
857
858 /* Check auth type. */
859 if (ri->auth_type != RIP_AUTH_MD5 || md5->type != htons(RIP_AUTH_MD5))
860 return 0;
861
862 /* If the authentication length is less than 16, then it must be wrong
863 * for
864 * any interpretation of rfc2082. Some implementations also interpret
865 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka
866 * RIP_AUTH_MD5_COMPAT_SIZE.
867 */
868 if (!((md5->auth_len == RIP_AUTH_MD5_SIZE)
869 || (md5->auth_len == RIP_AUTH_MD5_COMPAT_SIZE))) {
870 if (IS_RIP_DEBUG_EVENT)
871 zlog_debug(
872 "RIPv2 MD5 authentication, strange authentication length field %d",
873 md5->auth_len);
874 return 0;
875 }
876
877 /* grab and verify check packet length */
878 packet_len = ntohs(md5->packet_len);
879
880 if (packet_len > (length - RIP_HEADER_SIZE - RIP_AUTH_MD5_SIZE)) {
881 if (IS_RIP_DEBUG_EVENT)
882 zlog_debug(
883 "RIPv2 MD5 authentication, packet length field %d greater than received length %d!",
884 md5->packet_len, length);
885 return 0;
886 }
887
888 /* retrieve authentication data */
889 md5data = (struct rip_md5_data *)(((uint8_t *)packet) + packet_len);
890
891 if (ri->key_chain) {
892 keychain = keychain_lookup(ri->key_chain);
893 if (keychain == NULL)
894 return 0;
895
896 key = key_lookup_for_accept(keychain, md5->keyid);
897 if (key == NULL || key->string == NULL)
898 return 0;
899
900 memcpy(auth_str, key->string,
901 MIN(sizeof(auth_str), strlen(key->string)));
902 } else if (ri->auth_str)
903 memcpy(auth_str, ri->auth_str,
904 MIN(sizeof(auth_str), strlen(ri->auth_str)));
905
906 if (auth_str[0] == 0)
907 return 0;
908
909 /* MD5 digest authentication. */
910 #ifdef CRYPTO_OPENSSL
911 unsigned int md5_size = RIP_AUTH_MD5_SIZE;
912 ctx = EVP_MD_CTX_new();
913 EVP_DigestInit(ctx, EVP_md5());
914 EVP_DigestUpdate(ctx, packet, packet_len + RIP_HEADER_SIZE);
915 EVP_DigestUpdate(ctx, auth_str, RIP_AUTH_MD5_SIZE);
916 EVP_DigestFinal(ctx, digest, &md5_size);
917 EVP_MD_CTX_free(ctx);
918 #elif CRYPTO_INTERNAL
919 memset(&ctx, 0, sizeof(ctx));
920 MD5Init(&ctx);
921 MD5Update(&ctx, packet, packet_len + RIP_HEADER_SIZE);
922 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
923 MD5Final(digest, &ctx);
924 #endif
925
926 if (memcmp(md5data->digest, digest, RIP_AUTH_MD5_SIZE) == 0)
927 return packet_len;
928 else
929 return 0;
930 }
931
932 /* Pick correct auth string for sends, prepare auth_str buffer for use.
933 * (left justified and padded).
934 *
935 * presumes one of ri or key is valid, and that the auth strings they point
936 * to are nul terminated. If neither are present, auth_str will be fully
937 * zero padded.
938 *
939 */
940 static void rip_auth_prepare_str_send(struct rip_interface *ri, struct key *key,
941 char *auth_str, int len)
942 {
943 assert(ri || key);
944
945 memset(auth_str, 0, len);
946 if (key && key->string)
947 memcpy(auth_str, key->string,
948 MIN((size_t)len, strlen(key->string)));
949 else if (ri->auth_str)
950 memcpy(auth_str, ri->auth_str,
951 MIN((size_t)len, strlen(ri->auth_str)));
952
953 return;
954 }
955
956 /* Write RIPv2 simple password authentication information
957 *
958 * auth_str is presumed to be 2 bytes and correctly prepared
959 * (left justified and zero padded).
960 */
961 static void rip_auth_simple_write(struct stream *s, char *auth_str, int len)
962 {
963 assert(s && len == RIP_AUTH_SIMPLE_SIZE);
964
965 stream_putw(s, RIP_FAMILY_AUTH);
966 stream_putw(s, RIP_AUTH_SIMPLE_PASSWORD);
967 stream_put(s, auth_str, RIP_AUTH_SIMPLE_SIZE);
968
969 return;
970 }
971
972 /* write RIPv2 MD5 "authentication header"
973 * (uses the auth key data field)
974 *
975 * Digest offset field is set to 0.
976 *
977 * returns: offset of the digest offset field, which must be set when
978 * length to the auth-data MD5 digest is known.
979 */
980 static size_t rip_auth_md5_ah_write(struct stream *s, struct rip_interface *ri,
981 struct key *key)
982 {
983 size_t doff = 0;
984 static uint32_t seq = 0;
985
986 assert(s && ri && ri->auth_type == RIP_AUTH_MD5);
987
988 /* MD5 authentication. */
989 stream_putw(s, RIP_FAMILY_AUTH);
990 stream_putw(s, RIP_AUTH_MD5);
991
992 /* MD5 AH digest offset field.
993 *
994 * Set to placeholder value here, to true value when RIP-2 Packet length
995 * is known. Actual value is set in .....().
996 */
997 doff = stream_get_endp(s);
998 stream_putw(s, 0);
999
1000 /* Key ID. */
1001 if (key)
1002 stream_putc(s, key->index % 256);
1003 else
1004 stream_putc(s, 1);
1005
1006 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1007 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for
1008 * this
1009 * to be configurable.
1010 */
1011 stream_putc(s, ri->md5_auth_len);
1012
1013 /* Sequence Number (non-decreasing). */
1014 /* RFC2080: The value used in the sequence number is
1015 arbitrary, but two suggestions are the time of the
1016 message's creation or a simple message counter. */
1017 stream_putl(s, ++seq);
1018
1019 /* Reserved field must be zero. */
1020 stream_putl(s, 0);
1021 stream_putl(s, 0);
1022
1023 return doff;
1024 }
1025
1026 /* If authentication is in used, write the appropriate header
1027 * returns stream offset to which length must later be written
1028 * or 0 if this is not required
1029 */
1030 static size_t rip_auth_header_write(struct stream *s, struct rip_interface *ri,
1031 struct key *key, char *auth_str, int len)
1032 {
1033 assert(ri->auth_type != RIP_NO_AUTH);
1034
1035 switch (ri->auth_type) {
1036 case RIP_AUTH_SIMPLE_PASSWORD:
1037 rip_auth_prepare_str_send(ri, key, auth_str, len);
1038 rip_auth_simple_write(s, auth_str, len);
1039 return 0;
1040 case RIP_AUTH_MD5:
1041 return rip_auth_md5_ah_write(s, ri, key);
1042 }
1043 assert(1);
1044 return 0;
1045 }
1046
1047 /* Write RIPv2 MD5 authentication data trailer */
1048 static void rip_auth_md5_set(struct stream *s, struct rip_interface *ri,
1049 size_t doff, char *auth_str, int authlen)
1050 {
1051 unsigned long len;
1052 #ifdef CRYPTO_OPENSSL
1053 EVP_MD_CTX *ctx;
1054 #elif CRYPTO_INTERNAL
1055 MD5_CTX ctx;
1056 #endif
1057 unsigned char digest[RIP_AUTH_MD5_SIZE];
1058
1059 /* Make it sure this interface is configured as MD5
1060 authentication. */
1061 assert((ri->auth_type == RIP_AUTH_MD5)
1062 && (authlen == RIP_AUTH_MD5_SIZE));
1063 assert(doff > 0);
1064
1065 /* Get packet length. */
1066 len = stream_get_endp(s);
1067
1068 /* Check packet length. */
1069 if (len < (RIP_HEADER_SIZE + RIP_RTE_SIZE)) {
1070 flog_err(EC_RIP_PACKET,
1071 "%s: packet length %ld is less than minimum length.",
1072 __func__, len);
1073 return;
1074 }
1075
1076 /* Set the digest offset length in the header */
1077 stream_putw_at(s, doff, len);
1078
1079 /* Set authentication data. */
1080 stream_putw(s, RIP_FAMILY_AUTH);
1081 stream_putw(s, RIP_AUTH_DATA);
1082
1083 /* Generate a digest for the RIP packet. */
1084 #ifdef CRYPTO_OPENSSL
1085 unsigned int md5_size = RIP_AUTH_MD5_SIZE;
1086 ctx = EVP_MD_CTX_new();
1087 EVP_DigestInit(ctx, EVP_md5());
1088 EVP_DigestUpdate(ctx, STREAM_DATA(s), stream_get_endp(s));
1089 EVP_DigestUpdate(ctx, auth_str, RIP_AUTH_MD5_SIZE);
1090 EVP_DigestFinal(ctx, digest, &md5_size);
1091 EVP_MD_CTX_free(ctx);
1092 #elif CRYPTO_INTERNAL
1093 memset(&ctx, 0, sizeof(ctx));
1094 MD5Init(&ctx);
1095 MD5Update(&ctx, STREAM_DATA(s), stream_get_endp(s));
1096 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
1097 MD5Final(digest, &ctx);
1098 #endif
1099
1100 /* Copy the digest to the packet. */
1101 stream_write(s, digest, RIP_AUTH_MD5_SIZE);
1102 }
1103
1104 /* RIP routing information. */
1105 static void rip_response_process(struct rip_packet *packet, int size,
1106 struct sockaddr_in *from,
1107 struct connected *ifc)
1108 {
1109 struct rip_interface *ri = ifc->ifp->info;
1110 struct rip *rip = ri->rip;
1111 caddr_t lim;
1112 struct rte *rte;
1113 struct prefix_ipv4 ifaddr;
1114 struct prefix_ipv4 ifaddrclass;
1115 int subnetted;
1116
1117 memset(&ifaddr, 0, sizeof(ifaddr));
1118 /* We don't know yet. */
1119 subnetted = -1;
1120
1121 /* The Response must be ignored if it is not from the RIP
1122 port. (RFC2453 - Sec. 3.9.2)*/
1123 if (from->sin_port != htons(RIP_PORT_DEFAULT)) {
1124 zlog_info("response doesn't come from RIP port: %d",
1125 from->sin_port);
1126 rip_peer_bad_packet(rip, ri, from);
1127 return;
1128 }
1129
1130 /* The datagram's IPv4 source address should be checked to see
1131 whether the datagram is from a valid neighbor; the source of the
1132 datagram must be on a directly connected network (RFC2453 - Sec.
1133 3.9.2) */
1134 if (if_lookup_address((void *)&from->sin_addr, AF_INET,
1135 rip->vrf->vrf_id)
1136 == NULL) {
1137 zlog_info(
1138 "This datagram doesn't come from a valid neighbor: %pI4",
1139 &from->sin_addr);
1140 rip_peer_bad_packet(rip, ri, from);
1141 return;
1142 }
1143
1144 /* It is also worth checking to see whether the response is from one
1145 of the router's own addresses. */
1146
1147 ; /* Alredy done in rip_read () */
1148
1149 /* Update RIP peer. */
1150 rip_peer_update(rip, ri, from, packet->version);
1151
1152 /* Set RTE pointer. */
1153 rte = packet->rte;
1154
1155 for (lim = (caddr_t)packet + size; (caddr_t)rte < lim; rte++) {
1156 /* RIPv2 authentication check. */
1157 /* If the Address Family Identifier of the first (and only the
1158 first) entry in the message is 0xFFFF, then the remainder of
1159 the entry contains the authentication. */
1160 /* If the packet gets here it means authentication enabled */
1161 /* Check is done in rip_read(). So, just skipping it */
1162 if (packet->version == RIPv2 && rte == packet->rte
1163 && rte->family == htons(RIP_FAMILY_AUTH))
1164 continue;
1165
1166 if (rte->family != htons(AF_INET)) {
1167 /* Address family check. RIP only supports AF_INET. */
1168 zlog_info("Unsupported family %d from %pI4",
1169 ntohs(rte->family),
1170 &from->sin_addr);
1171 continue;
1172 }
1173
1174 /* - is the destination address valid (e.g., unicast; not net 0
1175 or 127) */
1176 if (!rip_destination_check(rte->prefix)) {
1177 zlog_info(
1178 "Network is net 0 or net 127 or it is not unicast network");
1179 rip_peer_bad_route(rip, ri, from);
1180 continue;
1181 }
1182
1183 /* Convert metric value to host byte order. */
1184 rte->metric = ntohl(rte->metric);
1185
1186 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1187 if (!(rte->metric >= 1 && rte->metric <= 16)) {
1188 zlog_info("Route's metric is not in the 1-16 range.");
1189 rip_peer_bad_route(rip, ri, from);
1190 continue;
1191 }
1192
1193 /* RIPv1 does not have nexthop value. */
1194 if (packet->version == RIPv1
1195 && rte->nexthop.s_addr != INADDR_ANY) {
1196 zlog_info("RIPv1 packet with nexthop value %pI4",
1197 &rte->nexthop);
1198 rip_peer_bad_route(rip, ri, from);
1199 continue;
1200 }
1201
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
1207 && rte->nexthop.s_addr != INADDR_ANY) {
1208 uint32_t addrval;
1209
1210 /* Multicast address check. */
1211 addrval = ntohl(rte->nexthop.s_addr);
1212 if (IN_CLASSD(addrval)) {
1213 zlog_info(
1214 "Nexthop %pI4 is multicast address, skip this rte",
1215 &rte->nexthop);
1216 continue;
1217 }
1218
1219 if (!if_lookup_address((void *)&rte->nexthop, AF_INET,
1220 rip->vrf->vrf_id)) {
1221 struct route_node *rn;
1222 struct rip_info *rinfo;
1223
1224 rn = route_node_match_ipv4(rip->table,
1225 &rte->nexthop);
1226
1227 if (rn) {
1228 rinfo = rn->info;
1229
1230 if (rinfo->type == ZEBRA_ROUTE_RIP
1231 && rinfo->sub_type
1232 == RIP_ROUTE_RTE) {
1233 if (IS_RIP_DEBUG_EVENT)
1234 zlog_debug(
1235 "Next hop %pI4 is on RIP network. Set nexthop to the packet's originator",
1236 &rte->nexthop);
1237 rte->nexthop = rinfo->from;
1238 } else {
1239 if (IS_RIP_DEBUG_EVENT)
1240 zlog_debug(
1241 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1242 &rte->nexthop);
1243 rte->nexthop.s_addr =
1244 INADDR_ANY;
1245 }
1246
1247 route_unlock_node(rn);
1248 } else {
1249 if (IS_RIP_DEBUG_EVENT)
1250 zlog_debug(
1251 "Next hop %pI4 is not directly reachable. Treat it as 0.0.0.0",
1252 &rte->nexthop);
1253 rte->nexthop.s_addr = INADDR_ANY;
1254 }
1255 }
1256 }
1257
1258 /* For RIPv1, there won't be a valid netmask.
1259 * This is a best guess at the masks. If everyone was using old
1260 * Ciscos before the 'ip subnet zero' option, it would be almost
1261 * right too :-)
1262 *
1263 * Cisco summarize ripv1 advertisements to the classful boundary
1264 * (/16 for class B's) except when the RIP packet does to inside
1265 * the classful network in question.
1266 */
1267 if ((packet->version == RIPv1
1268 && rte->prefix.s_addr != INADDR_ANY)
1269 || (packet->version == RIPv2
1270 && (rte->prefix.s_addr != INADDR_ANY
1271 && rte->mask.s_addr == INADDR_ANY))) {
1272 uint32_t destination;
1273
1274 if (subnetted == -1) {
1275 memcpy(&ifaddr, ifc->address, sizeof(ifaddr));
1276 memcpy(&ifaddrclass, &ifaddr,
1277 sizeof(ifaddrclass));
1278 apply_classful_mask_ipv4(&ifaddrclass);
1279 subnetted = 0;
1280 if (ifaddr.prefixlen > ifaddrclass.prefixlen)
1281 subnetted = 1;
1282 }
1283
1284 destination = ntohl(rte->prefix.s_addr);
1285
1286 if (IN_CLASSA(destination))
1287 masklen2ip(8, &rte->mask);
1288 else if (IN_CLASSB(destination))
1289 masklen2ip(16, &rte->mask);
1290 else if (IN_CLASSC(destination))
1291 masklen2ip(24, &rte->mask);
1292
1293 if (subnetted == 1)
1294 masklen2ip(ifaddrclass.prefixlen,
1295 (struct in_addr *)&destination);
1296 if ((subnetted == 1)
1297 && ((rte->prefix.s_addr & destination)
1298 == ifaddrclass.prefix.s_addr)) {
1299 masklen2ip(ifaddr.prefixlen, &rte->mask);
1300 if ((rte->prefix.s_addr & rte->mask.s_addr)
1301 != rte->prefix.s_addr)
1302 masklen2ip(32, &rte->mask);
1303 if (IS_RIP_DEBUG_EVENT)
1304 zlog_debug("Subnetted route %pI4",
1305 &rte->prefix);
1306 } else {
1307 if ((rte->prefix.s_addr & rte->mask.s_addr)
1308 != rte->prefix.s_addr)
1309 continue;
1310 }
1311
1312 if (IS_RIP_DEBUG_EVENT) {
1313 zlog_debug("Resultant route %pI4",
1314 &rte->prefix);
1315 zlog_debug("Resultant mask %pI4",
1316 &rte->mask);
1317 }
1318 }
1319
1320 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1321 ignore the entry. */
1322 if ((packet->version == RIPv2)
1323 && (rte->mask.s_addr != INADDR_ANY)
1324 && ((rte->prefix.s_addr & rte->mask.s_addr)
1325 != rte->prefix.s_addr)) {
1326 zlog_warn(
1327 "RIPv2 address %pI4 is not mask /%d applied one",
1328 &rte->prefix, ip_masklen(rte->mask));
1329 rip_peer_bad_route(rip, ri, from);
1330 continue;
1331 }
1332
1333 /* Default route's netmask is ignored. */
1334 if (packet->version == RIPv2
1335 && (rte->prefix.s_addr == INADDR_ANY)
1336 && (rte->mask.s_addr != INADDR_ANY)) {
1337 if (IS_RIP_DEBUG_EVENT)
1338 zlog_debug(
1339 "Default route with non-zero netmask. Set zero to netmask");
1340 rte->mask.s_addr = INADDR_ANY;
1341 }
1342
1343 /* Routing table updates. */
1344 rip_rte_process(rte, from, ifc->ifp);
1345 }
1346 }
1347
1348 /* Make socket for RIP protocol. */
1349 int rip_create_socket(struct vrf *vrf)
1350 {
1351 int ret;
1352 int sock;
1353 struct sockaddr_in addr;
1354 const char *vrf_dev = NULL;
1355
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);
1364
1365 /* Make datagram socket. */
1366 if (vrf->vrf_id != VRF_DEFAULT)
1367 vrf_dev = vrf->name;
1368 frr_with_privs(&ripd_privs) {
1369 sock = vrf_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, vrf->vrf_id,
1370 vrf_dev);
1371 if (sock < 0) {
1372 flog_err_sys(EC_LIB_SOCKET,
1373 "Cannot create UDP socket: %s",
1374 safe_strerror(errno));
1375 return -1;
1376 }
1377 }
1378
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);
1385 #endif
1386 setsockopt_so_recvbuf(sock, RIP_UDP_RCV_BUF);
1387
1388 frr_with_privs(&ripd_privs) {
1389 if ((ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)))
1390 < 0) {
1391 zlog_err("%s: Can't bind socket %d to %pI4 port %d: %s",
1392 __func__, sock, &addr.sin_addr,
1393 (int)ntohs(addr.sin_port),
1394 safe_strerror(errno));
1395
1396 close(sock);
1397 return ret;
1398 }
1399 }
1400
1401 return sock;
1402 }
1403
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
1407 */
1408 static int rip_send_packet(uint8_t *buf, int size, struct sockaddr_in *to,
1409 struct connected *ifc)
1410 {
1411 struct rip_interface *ri;
1412 struct rip *rip;
1413 int ret;
1414 struct sockaddr_in sin;
1415 struct msghdr msg;
1416 struct iovec iov;
1417 #ifdef GNU_LINUX
1418 struct cmsghdr *cmsgptr;
1419 char adata[256] = {};
1420 struct in_pktinfo *pkt;
1421 #endif /* GNU_LINUX */
1422
1423 assert(ifc != NULL);
1424 ri = ifc->ifp->info;
1425 rip = ri->rip;
1426
1427 if (IS_RIP_DEBUG_PACKET) {
1428 #define ADDRESS_SIZE 20
1429 char dst[ADDRESS_SIZE];
1430
1431 if (to) {
1432 inet_ntop(AF_INET, &to->sin_addr, dst, sizeof(dst));
1433 } else {
1434 sin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
1435 inet_ntop(AF_INET, &sin.sin_addr, dst, sizeof(dst));
1436 }
1437 #undef ADDRESS_SIZE
1438 zlog_debug("%s %pI4 > %s (%s)", __func__,
1439 &ifc->address->u.prefix4, dst, ifc->ifp->name);
1440 }
1441
1442 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) {
1443 /*
1444 * ZEBRA_IFA_SECONDARY is set on linux when an interface is
1445 * configured with multiple addresses on the same
1446 * subnet: the first address on the subnet is configured
1447 * "primary", and all subsequent addresses on that subnet
1448 * are treated as "secondary" addresses. In order to avoid
1449 * routing-table bloat on other rip listeners, we do not send
1450 * out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1451 * XXX Since Linux is the only system for which the
1452 * ZEBRA_IFA_SECONDARY flag is set, we would end up
1453 * sending a packet for a "secondary" source address on
1454 * non-linux systems.
1455 */
1456 if (IS_RIP_DEBUG_PACKET)
1457 zlog_debug("duplicate dropped");
1458 return 0;
1459 }
1460
1461 /* Make destination address. */
1462 memset(&sin, 0, sizeof(sin));
1463 sin.sin_family = AF_INET;
1464 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1465 sin.sin_len = sizeof(struct sockaddr_in);
1466 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1467
1468 /* When destination is specified, use it's port and address. */
1469 if (to) {
1470 sin.sin_port = to->sin_port;
1471 sin.sin_addr = to->sin_addr;
1472 } else {
1473 sin.sin_port = htons(RIP_PORT_DEFAULT);
1474 sin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP);
1475
1476 rip_interface_multicast_set(rip->sock, ifc);
1477 }
1478
1479 memset(&msg, 0, sizeof(msg));
1480 msg.msg_name = (void *)&sin;
1481 msg.msg_namelen = sizeof(struct sockaddr_in);
1482 msg.msg_iov = &iov;
1483 msg.msg_iovlen = 1;
1484 iov.iov_base = buf;
1485 iov.iov_len = size;
1486
1487 #ifdef GNU_LINUX
1488 msg.msg_control = (void *)adata;
1489 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
1490
1491 cmsgptr = (struct cmsghdr *)adata;
1492 cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
1493 cmsgptr->cmsg_level = IPPROTO_IP;
1494 cmsgptr->cmsg_type = IP_PKTINFO;
1495 pkt = (struct in_pktinfo *)CMSG_DATA(cmsgptr);
1496 pkt->ipi_ifindex = ifc->ifp->ifindex;
1497 pkt->ipi_spec_dst.s_addr = ifc->address->u.prefix4.s_addr;
1498 #endif /* GNU_LINUX */
1499
1500 ret = sendmsg(rip->sock, &msg, 0);
1501
1502 if (IS_RIP_DEBUG_EVENT)
1503 zlog_debug("SEND to %pI4 port %d", &sin.sin_addr,
1504 ntohs(sin.sin_port));
1505
1506 if (ret < 0)
1507 zlog_warn("can't send packet : %s", safe_strerror(errno));
1508
1509 return ret;
1510 }
1511
1512 /* Add redistributed route to RIP table. */
1513 void rip_redistribute_add(struct rip *rip, int type, int sub_type,
1514 struct prefix_ipv4 *p, struct nexthop *nh,
1515 unsigned int metric, unsigned char distance,
1516 route_tag_t tag)
1517 {
1518 int ret;
1519 struct route_node *rp = NULL;
1520 struct rip_info *rinfo = NULL, newinfo;
1521 struct list *list = NULL;
1522
1523 /* Redistribute route */
1524 ret = rip_destination_check(p->prefix);
1525 if (!ret)
1526 return;
1527
1528 rp = route_node_get(rip->table, (struct prefix *)p);
1529
1530 memset(&newinfo, 0, sizeof(newinfo));
1531 newinfo.type = type;
1532 newinfo.sub_type = sub_type;
1533 newinfo.metric = 1;
1534 newinfo.external_metric = metric;
1535 newinfo.distance = distance;
1536 if (tag <= UINT16_MAX) /* RIP only supports 16 bit tags */
1537 newinfo.tag = tag;
1538 newinfo.rp = rp;
1539 newinfo.nh = *nh;
1540
1541 if ((list = rp->info) != NULL && listcount(list) != 0) {
1542 rinfo = listgetdata(listhead(list));
1543
1544 if (rinfo->type == ZEBRA_ROUTE_CONNECT
1545 && rinfo->sub_type == RIP_ROUTE_INTERFACE
1546 && rinfo->metric != RIP_METRIC_INFINITY) {
1547 route_unlock_node(rp);
1548 return;
1549 }
1550
1551 /* Manually configured RIP route check. */
1552 if (rinfo->type == ZEBRA_ROUTE_RIP
1553 && ((rinfo->sub_type == RIP_ROUTE_STATIC)
1554 || (rinfo->sub_type == RIP_ROUTE_DEFAULT))) {
1555 if (type != ZEBRA_ROUTE_RIP
1556 || ((sub_type != RIP_ROUTE_STATIC)
1557 && (sub_type != RIP_ROUTE_DEFAULT))) {
1558 route_unlock_node(rp);
1559 return;
1560 }
1561 }
1562
1563 (void)rip_ecmp_replace(rip, &newinfo);
1564 route_unlock_node(rp);
1565 } else
1566 (void)rip_ecmp_add(rip, &newinfo);
1567
1568 if (IS_RIP_DEBUG_EVENT) {
1569 zlog_debug("Redistribute new prefix %pFX", p);
1570 }
1571
1572 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
1573 }
1574
1575 /* Delete redistributed route from RIP table. */
1576 void rip_redistribute_delete(struct rip *rip, int type, int sub_type,
1577 struct prefix_ipv4 *p, ifindex_t ifindex)
1578 {
1579 int ret;
1580 struct route_node *rp;
1581 struct rip_info *rinfo;
1582
1583 ret = rip_destination_check(p->prefix);
1584 if (!ret)
1585 return;
1586
1587 rp = route_node_lookup(rip->table, (struct prefix *)p);
1588 if (rp) {
1589 struct list *list = rp->info;
1590
1591 if (list != NULL && listcount(list) != 0) {
1592 rinfo = listgetdata(listhead(list));
1593 if (rinfo != NULL && rinfo->type == type
1594 && rinfo->sub_type == sub_type
1595 && rinfo->nh.ifindex == ifindex) {
1596 /* Perform poisoned reverse. */
1597 rinfo->metric = RIP_METRIC_INFINITY;
1598 RIP_TIMER_ON(rinfo->t_garbage_collect,
1599 rip_garbage_collect,
1600 rip->garbage_time);
1601 EVENT_OFF(rinfo->t_timeout);
1602 rinfo->flags |= RIP_RTF_CHANGED;
1603
1604 if (IS_RIP_DEBUG_EVENT)
1605 zlog_debug(
1606 "Poison %pFX on the interface %s with an infinity metric [delete]",
1607 p,
1608 ifindex2ifname(
1609 ifindex,
1610 rip->vrf->vrf_id));
1611
1612 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
1613 }
1614 }
1615 route_unlock_node(rp);
1616 }
1617 }
1618
1619 /* Response to request called from rip_read ().*/
1620 static void rip_request_process(struct rip_packet *packet, int size,
1621 struct sockaddr_in *from, struct connected *ifc)
1622 {
1623 struct rip *rip;
1624 caddr_t lim;
1625 struct rte *rte;
1626 struct prefix_ipv4 p;
1627 struct route_node *rp;
1628 struct rip_info *rinfo;
1629 struct rip_interface *ri;
1630
1631 /* Does not reponse to the requests on the loopback interfaces */
1632 if (if_is_loopback(ifc->ifp))
1633 return;
1634
1635 /* Check RIP process is enabled on this interface. */
1636 ri = ifc->ifp->info;
1637 if (!ri->running)
1638 return;
1639 rip = ri->rip;
1640
1641 /* When passive interface is specified, suppress responses */
1642 if (ri->passive)
1643 return;
1644
1645 /* RIP peer update. */
1646 rip_peer_update(rip, ri, from, packet->version);
1647
1648 lim = ((caddr_t)packet) + size;
1649 rte = packet->rte;
1650
1651 /* The Request is processed entry by entry. If there are no
1652 entries, no response is given. */
1653 if (lim == (caddr_t)rte)
1654 return;
1655
1656 /* There is one special case. If there is exactly one entry in the
1657 request, and it has an address family identifier of zero and a
1658 metric of infinity (i.e., 16), then this is a request to send the
1659 entire routing table. */
1660 if (lim == ((caddr_t)(rte + 1)) && ntohs(rte->family) == 0
1661 && ntohl(rte->metric) == RIP_METRIC_INFINITY) {
1662 /* All route with split horizon */
1663 rip_output_process(ifc, from, rip_all_route, packet->version);
1664 } else {
1665 if (ntohs(rte->family) != AF_INET)
1666 return;
1667
1668 /* Examine the list of RTEs in the Request one by one. For each
1669 entry, look up the destination in the router's routing
1670 database and, if there is a route, put that route's metric in
1671 the metric field of the RTE. If there is no explicit route
1672 to the specified destination, put infinity in the metric
1673 field. Once all the entries have been filled in, change the
1674 command from Request to Response and send the datagram back
1675 to the requestor. */
1676 p.family = AF_INET;
1677
1678 for (; ((caddr_t)rte) < lim; rte++) {
1679 p.prefix = rte->prefix;
1680 p.prefixlen = ip_masklen(rte->mask);
1681 apply_mask_ipv4(&p);
1682
1683 rp = route_node_lookup(rip->table, (struct prefix *)&p);
1684 if (rp) {
1685 rinfo = listgetdata(
1686 listhead((struct list *)rp->info));
1687 rte->metric = htonl(rinfo->metric);
1688 route_unlock_node(rp);
1689 } else
1690 rte->metric = htonl(RIP_METRIC_INFINITY);
1691 }
1692 packet->command = RIP_RESPONSE;
1693
1694 (void)rip_send_packet((uint8_t *)packet, size, from, ifc);
1695 }
1696 rip->counters.queries++;
1697 }
1698
1699 /* First entry point of RIP packet. */
1700 static void rip_read(struct event *t)
1701 {
1702 struct rip *rip = EVENT_ARG(t);
1703 int sock;
1704 int ret;
1705 int rtenum;
1706 union rip_buf rip_buf;
1707 struct rip_packet *packet;
1708 struct sockaddr_in from;
1709 int len;
1710 int vrecv;
1711 socklen_t fromlen;
1712 struct interface *ifp = NULL;
1713 struct connected *ifc;
1714 struct rip_interface *ri = NULL;
1715 struct prefix p;
1716
1717 /* Fetch socket then register myself. */
1718 sock = EVENT_FD(t);
1719
1720 /* Add myself to tne next event */
1721 rip_event(rip, RIP_READ, sock);
1722
1723 /* RIPd manages only IPv4. */
1724 memset(&from, 0, sizeof(from));
1725 fromlen = sizeof(struct sockaddr_in);
1726
1727 len = recvfrom(sock, (char *)&rip_buf.buf, sizeof(rip_buf.buf), 0,
1728 (struct sockaddr *)&from, &fromlen);
1729 if (len < 0) {
1730 zlog_info("recvfrom failed (VRF %s): %s", rip->vrf_name,
1731 safe_strerror(errno));
1732 return;
1733 }
1734
1735 /* Check is this packet comming from myself? */
1736 if (if_check_address(rip, from.sin_addr)) {
1737 if (IS_RIP_DEBUG_PACKET)
1738 zlog_debug("ignore packet comes from myself (VRF %s)",
1739 rip->vrf_name);
1740 return;
1741 }
1742
1743 /* Which interface is this packet comes from. */
1744 ifc = if_lookup_address((void *)&from.sin_addr, AF_INET,
1745 rip->vrf->vrf_id);
1746 if (ifc) {
1747 ifp = ifc->ifp;
1748 ri = ifp->info;
1749 }
1750
1751 /* RIP packet received */
1752 if (IS_RIP_DEBUG_EVENT)
1753 zlog_debug("RECV packet from %pI4 port %d on %s (VRF %s)",
1754 &from.sin_addr, ntohs(from.sin_port),
1755 ifp ? ifp->name : "unknown", rip->vrf_name);
1756
1757 /* If this packet come from unknown interface, ignore it. */
1758 if (ifp == NULL || ri == NULL) {
1759 zlog_info(
1760 "%s: cannot find interface for packet from %pI4 port %d (VRF %s)",
1761 __func__, &from.sin_addr, ntohs(from.sin_port),
1762 rip->vrf_name);
1763 return;
1764 }
1765
1766 p.family = AF_INET;
1767 p.u.prefix4 = from.sin_addr;
1768 p.prefixlen = IPV4_MAX_BITLEN;
1769
1770 ifc = connected_lookup_prefix(ifp, &p);
1771
1772 if (ifc == NULL) {
1773 zlog_info(
1774 "%s: cannot find connected address for packet from %pI4 port %d on interface %s (VRF %s)",
1775 __func__, &from.sin_addr, ntohs(from.sin_port),
1776 ifp->name, rip->vrf_name);
1777 return;
1778 }
1779
1780 /* Packet length check. */
1781 if (len < RIP_PACKET_MINSIZ) {
1782 zlog_warn("packet size %d is smaller than minimum size %d", len,
1783 RIP_PACKET_MINSIZ);
1784 rip_peer_bad_packet(rip, ri, &from);
1785 return;
1786 }
1787 if (len > RIP_PACKET_MAXSIZ) {
1788 zlog_warn("packet size %d is larger than max size %d", len,
1789 RIP_PACKET_MAXSIZ);
1790 rip_peer_bad_packet(rip, ri, &from);
1791 return;
1792 }
1793
1794 /* Packet alignment check. */
1795 if ((len - RIP_PACKET_MINSIZ) % 20) {
1796 zlog_warn("packet size %d is wrong for RIP packet alignment",
1797 len);
1798 rip_peer_bad_packet(rip, ri, &from);
1799 return;
1800 }
1801
1802 /* Set RTE number. */
1803 rtenum = ((len - RIP_PACKET_MINSIZ) / 20);
1804
1805 /* For easy to handle. */
1806 packet = &rip_buf.rip_packet;
1807
1808 /* RIP version check. */
1809 if (packet->version == 0) {
1810 zlog_info("version 0 with command %d received.",
1811 packet->command);
1812 rip_peer_bad_packet(rip, ri, &from);
1813 return;
1814 }
1815
1816 /* Dump RIP packet. */
1817 if (IS_RIP_DEBUG_RECV)
1818 rip_packet_dump(packet, len, "RECV");
1819
1820 /* RIP version adjust. This code should rethink now. RFC1058 says
1821 that "Version 1 implementations are to ignore this extra data and
1822 process only the fields specified in this document.". So RIPv3
1823 packet should be treated as RIPv1 ignoring must be zero field. */
1824 if (packet->version > RIPv2)
1825 packet->version = RIPv2;
1826
1827 /* Is RIP running or is this RIP neighbor ?*/
1828 if (!ri->running && !rip_neighbor_lookup(rip, &from)) {
1829 if (IS_RIP_DEBUG_EVENT)
1830 zlog_debug("RIP is not enabled on interface %s.",
1831 ifp->name);
1832 rip_peer_bad_packet(rip, ri, &from);
1833 return;
1834 }
1835
1836 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1837 vrecv = ((ri->ri_receive == RI_RIP_UNSPEC) ? rip->version_recv
1838 : ri->ri_receive);
1839 if (vrecv == RI_RIP_VERSION_NONE
1840 || ((packet->version == RIPv1) && !(vrecv & RIPv1))
1841 || ((packet->version == RIPv2) && !(vrecv & RIPv2))) {
1842 if (IS_RIP_DEBUG_PACKET)
1843 zlog_debug(
1844 " packet's v%d doesn't fit to if version spec",
1845 packet->version);
1846 rip_peer_bad_packet(rip, ri, &from);
1847 return;
1848 }
1849
1850 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1851 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1852 accepted; authenticated RIP-2 messages shall be discarded. */
1853 if ((ri->auth_type == RIP_NO_AUTH) && rtenum
1854 && (packet->version == RIPv2)
1855 && (packet->rte->family == htons(RIP_FAMILY_AUTH))) {
1856 if (IS_RIP_DEBUG_EVENT)
1857 zlog_debug(
1858 "packet RIPv%d is dropped because authentication disabled",
1859 packet->version);
1860 ripd_notif_send_auth_type_failure(ifp->name);
1861 rip_peer_bad_packet(rip, ri, &from);
1862 return;
1863 }
1864
1865 /* RFC:
1866 If the router is configured to authenticate RIP-2 messages, then
1867 RIP-1 messages and RIP-2 messages which pass authentication
1868 testing shall be accepted; unauthenticated and failed
1869 authentication RIP-2 messages shall be discarded. For maximum
1870 security, RIP-1 messages should be ignored when authentication is
1871 in use (see section 4.1); otherwise, the routing information from
1872 authenticated messages will be propagated by RIP-1 routers in an
1873 unauthenticated manner.
1874 */
1875 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1876 * always reply regardless of authentication settings, because:
1877 *
1878 * - if there other authorised routers on-link, the REQUESTor can
1879 * passively obtain the routing updates anyway
1880 * - if there are no other authorised routers on-link, RIP can
1881 * easily be disabled for the link to prevent giving out information
1882 * on state of this routers RIP routing table..
1883 *
1884 * I.e. if RIPv1 has any place anymore these days, it's as a very
1885 * simple way to distribute routing information (e.g. to embedded
1886 * hosts / appliances) and the ability to give out RIPv1
1887 * routing-information freely, while still requiring RIPv2
1888 * authentication for any RESPONSEs might be vaguely useful.
1889 */
1890 if (ri->auth_type != RIP_NO_AUTH && packet->version == RIPv1) {
1891 /* Discard RIPv1 messages other than REQUESTs */
1892 if (packet->command != RIP_REQUEST) {
1893 if (IS_RIP_DEBUG_PACKET)
1894 zlog_debug(
1895 "RIPv1 dropped because authentication enabled");
1896 ripd_notif_send_auth_type_failure(ifp->name);
1897 rip_peer_bad_packet(rip, ri, &from);
1898 return;
1899 }
1900 } else if (ri->auth_type != RIP_NO_AUTH) {
1901 const char *auth_desc;
1902
1903 if (rtenum == 0) {
1904 /* There definitely is no authentication in the packet.
1905 */
1906 if (IS_RIP_DEBUG_PACKET)
1907 zlog_debug(
1908 "RIPv2 authentication failed: no auth RTE in packet");
1909 ripd_notif_send_auth_type_failure(ifp->name);
1910 rip_peer_bad_packet(rip, ri, &from);
1911 return;
1912 }
1913
1914 /* First RTE must be an Authentication Family RTE */
1915 if (packet->rte->family != htons(RIP_FAMILY_AUTH)) {
1916 if (IS_RIP_DEBUG_PACKET)
1917 zlog_debug(
1918 "RIPv2 dropped because authentication enabled");
1919 ripd_notif_send_auth_type_failure(ifp->name);
1920 rip_peer_bad_packet(rip, ri, &from);
1921 return;
1922 }
1923
1924 /* Check RIPv2 authentication. */
1925 switch (ntohs(packet->rte->tag)) {
1926 case RIP_AUTH_SIMPLE_PASSWORD:
1927 auth_desc = "simple";
1928 ret = rip_auth_simple_password(packet->rte, &from, ifp);
1929 break;
1930
1931 case RIP_AUTH_MD5:
1932 auth_desc = "MD5";
1933 ret = rip_auth_md5(packet, &from, len, ifp);
1934 /* Reset RIP packet length to trim MD5 data. */
1935 len = ret;
1936 break;
1937
1938 default:
1939 ret = 0;
1940 auth_desc = "unknown type";
1941 if (IS_RIP_DEBUG_PACKET)
1942 zlog_debug(
1943 "RIPv2 Unknown authentication type %d",
1944 ntohs(packet->rte->tag));
1945 }
1946
1947 if (ret) {
1948 if (IS_RIP_DEBUG_PACKET)
1949 zlog_debug("RIPv2 %s authentication success",
1950 auth_desc);
1951 } else {
1952 if (IS_RIP_DEBUG_PACKET)
1953 zlog_debug("RIPv2 %s authentication failure",
1954 auth_desc);
1955 ripd_notif_send_auth_failure(ifp->name);
1956 rip_peer_bad_packet(rip, ri, &from);
1957 return;
1958 }
1959 }
1960
1961 /* Process each command. */
1962 switch (packet->command) {
1963 case RIP_RESPONSE:
1964 rip_response_process(packet, len, &from, ifc);
1965 break;
1966 case RIP_REQUEST:
1967 case RIP_POLL:
1968 rip_request_process(packet, len, &from, ifc);
1969 break;
1970 case RIP_TRACEON:
1971 case RIP_TRACEOFF:
1972 zlog_info(
1973 "Obsolete command %s received, please sent it to routed",
1974 lookup_msg(rip_msg, packet->command, NULL));
1975 rip_peer_bad_packet(rip, ri, &from);
1976 break;
1977 case RIP_POLL_ENTRY:
1978 zlog_info("Obsolete command %s received",
1979 lookup_msg(rip_msg, packet->command, NULL));
1980 rip_peer_bad_packet(rip, ri, &from);
1981 break;
1982 default:
1983 zlog_info("Unknown RIP command %d received", packet->command);
1984 rip_peer_bad_packet(rip, ri, &from);
1985 break;
1986 }
1987 }
1988
1989 /* Write routing table entry to the stream and return next index of
1990 the routing table entry in the stream. */
1991 static int rip_write_rte(int num, struct stream *s, struct prefix_ipv4 *p,
1992 uint8_t version, struct rip_info *rinfo)
1993 {
1994 struct in_addr mask;
1995
1996 /* Write routing table entry. */
1997 if (version == RIPv1) {
1998 stream_putw(s, AF_INET);
1999 stream_putw(s, 0);
2000 stream_put_ipv4(s, p->prefix.s_addr);
2001 stream_put_ipv4(s, 0);
2002 stream_put_ipv4(s, 0);
2003 stream_putl(s, rinfo->metric_out);
2004 } else {
2005 masklen2ip(p->prefixlen, &mask);
2006
2007 stream_putw(s, AF_INET);
2008 stream_putw(s, rinfo->tag_out);
2009 stream_put_ipv4(s, p->prefix.s_addr);
2010 stream_put_ipv4(s, mask.s_addr);
2011 stream_put_ipv4(s, rinfo->nexthop_out.s_addr);
2012 stream_putl(s, rinfo->metric_out);
2013 }
2014
2015 return ++num;
2016 }
2017
2018 /* Send update to the ifp or spcified neighbor. */
2019 void rip_output_process(struct connected *ifc, struct sockaddr_in *to,
2020 int route_type, uint8_t version)
2021 {
2022 struct rip *rip;
2023 int ret;
2024 struct stream *s;
2025 struct route_node *rp;
2026 struct rip_info *rinfo;
2027 struct rip_interface *ri;
2028 struct prefix_ipv4 *p;
2029 struct prefix_ipv4 classfull;
2030 struct prefix_ipv4 ifaddrclass;
2031 struct key *key = NULL;
2032 /* this might need to made dynamic if RIP ever supported auth methods
2033 with larger key string sizes */
2034 char auth_str[RIP_AUTH_SIMPLE_SIZE];
2035 size_t doff = 0; /* offset of digest offset field */
2036 int num = 0;
2037 int rtemax;
2038 int subnetted = 0;
2039 struct list *list = NULL;
2040 struct listnode *listnode = NULL;
2041
2042 /* Logging output event. */
2043 if (IS_RIP_DEBUG_EVENT) {
2044 if (to)
2045 zlog_debug("update routes to neighbor %pI4",
2046 &to->sin_addr);
2047 else
2048 zlog_debug("update routes on interface %s ifindex %d",
2049 ifc->ifp->name, ifc->ifp->ifindex);
2050 }
2051
2052 /* Get RIP interface. */
2053 ri = ifc->ifp->info;
2054 rip = ri->rip;
2055
2056 /* Set output stream. */
2057 s = rip->obuf;
2058
2059 /* Reset stream and RTE counter. */
2060 stream_reset(s);
2061 rtemax = RIP_MAX_RTE;
2062
2063 /* If output interface is in simple password authentication mode, we
2064 need space for authentication data. */
2065 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
2066 rtemax -= 1;
2067
2068 /* If output interface is in MD5 authentication mode, we need space
2069 for authentication header and data. */
2070 if (ri->auth_type == RIP_AUTH_MD5)
2071 rtemax -= 2;
2072
2073 /* If output interface is in simple password authentication mode
2074 and string or keychain is specified we need space for auth. data */
2075 if (ri->auth_type != RIP_NO_AUTH) {
2076 if (ri->key_chain) {
2077 struct keychain *keychain;
2078
2079 keychain = keychain_lookup(ri->key_chain);
2080 if (keychain)
2081 key = key_lookup_for_send(keychain);
2082 }
2083 /* to be passed to auth functions later */
2084 rip_auth_prepare_str_send(ri, key, auth_str, sizeof(auth_str));
2085 if (strlen(auth_str) == 0)
2086 return;
2087 }
2088
2089 if (version == RIPv1) {
2090 memcpy(&ifaddrclass, ifc->address, sizeof(ifaddrclass));
2091 apply_classful_mask_ipv4(&ifaddrclass);
2092 subnetted = 0;
2093 if (ifc->address->prefixlen > ifaddrclass.prefixlen)
2094 subnetted = 1;
2095 }
2096
2097 for (rp = route_top(rip->table); rp; rp = route_next(rp)) {
2098 list = rp->info;
2099
2100 if (list == NULL)
2101 continue;
2102
2103 if (listcount(list) == 0)
2104 continue;
2105
2106 rinfo = listgetdata(listhead(list));
2107 /*
2108 * For RIPv1, if we are subnetted, output subnets in our
2109 * network that have the same mask as the output "interface".
2110 * For other networks, only the classfull version is output.
2111 */
2112 if (version == RIPv1) {
2113 p = (struct prefix_ipv4 *)&rp->p;
2114
2115 if (IS_RIP_DEBUG_PACKET)
2116 zlog_debug(
2117 "RIPv1 mask check, %pFX considered for output",
2118 &rp->p);
2119
2120 if (subnetted &&
2121 prefix_match((struct prefix *)&ifaddrclass,
2122 &rp->p)) {
2123 if ((ifc->address->prefixlen !=
2124 rp->p.prefixlen) &&
2125 (rp->p.prefixlen != IPV4_MAX_BITLEN))
2126 continue;
2127 } else {
2128 memcpy(&classfull, &rp->p,
2129 sizeof(struct prefix_ipv4));
2130 apply_classful_mask_ipv4(&classfull);
2131 if (rp->p.u.prefix4.s_addr != INADDR_ANY &&
2132 classfull.prefixlen != rp->p.prefixlen)
2133 continue;
2134 }
2135 if (IS_RIP_DEBUG_PACKET)
2136 zlog_debug(
2137 "RIPv1 mask check, %pFX made it through",
2138 &rp->p);
2139 } else
2140 p = (struct prefix_ipv4 *)&rp->p;
2141
2142 /* Apply output filters. */
2143 ret = rip_filter(RIP_FILTER_OUT, p, ri);
2144 if (ret < 0)
2145 continue;
2146
2147 /* Changed route only output. */
2148 if (route_type == rip_changed_route &&
2149 (!(rinfo->flags & RIP_RTF_CHANGED)))
2150 continue;
2151
2152 /* Split horizon. */
2153 if (ri->split_horizon == RIP_SPLIT_HORIZON) {
2154 /*
2155 * We perform split horizon for RIP and connected
2156 * route. For rip routes, we want to suppress the
2157 * route if we would end up sending the route back on
2158 * the interface that we learned it from, with a
2159 * higher metric. For connected routes, we suppress
2160 * the route if the prefix is a subset of the source
2161 * address that we are going to use for the packet
2162 * (in order to handle the case when multiple subnets
2163 * are configured on the same interface).
2164 */
2165 int suppress = 0;
2166 struct rip_info *tmp_rinfo = NULL;
2167 struct connected *tmp_ifc = NULL;
2168
2169 for (ALL_LIST_ELEMENTS_RO(list, listnode, tmp_rinfo))
2170 if (tmp_rinfo->type == ZEBRA_ROUTE_RIP &&
2171 tmp_rinfo->nh.ifindex ==
2172 ifc->ifp->ifindex) {
2173 suppress = 1;
2174 break;
2175 }
2176
2177 if (!suppress && rinfo->type == ZEBRA_ROUTE_CONNECT) {
2178 for (ALL_LIST_ELEMENTS_RO(ifc->ifp->connected,
2179 listnode, tmp_ifc))
2180 if (prefix_match((struct prefix *)p,
2181 tmp_ifc->address)) {
2182 suppress = 1;
2183 break;
2184 }
2185 }
2186
2187 if (suppress)
2188 continue;
2189 }
2190
2191 /* Preparation for route-map. */
2192 rinfo->metric_set = 0;
2193 rinfo->nexthop_out.s_addr = 0;
2194 rinfo->metric_out = rinfo->metric;
2195 rinfo->tag_out = rinfo->tag;
2196 rinfo->ifindex_out = ifc->ifp->ifindex;
2197
2198 /* In order to avoid some local loops, if the RIP route has
2199 * a nexthop via this interface, keep the nexthop, otherwise
2200 * set it to 0. The nexthop should not be propagated beyond
2201 * the local broadcast/multicast area in order to avoid an
2202 * IGP multi-level recursive look-up. see (4.4)
2203 */
2204 if (rinfo->nh.ifindex == ifc->ifp->ifindex)
2205 rinfo->nexthop_out = rinfo->nh.gate.ipv4;
2206
2207 /* Interface route-map */
2208 if (ri->routemap[RIP_FILTER_OUT]) {
2209 ret = route_map_apply(ri->routemap[RIP_FILTER_OUT],
2210 (struct prefix *)p, rinfo);
2211
2212 if (ret == RMAP_DENYMATCH) {
2213 if (IS_RIP_DEBUG_PACKET)
2214 zlog_debug(
2215 "RIP %pFX is filtered by route-map out",
2216 p);
2217 continue;
2218 }
2219 }
2220
2221 /* Apply redistribute route map - continue, if deny */
2222 if (rip->redist[rinfo->type].route_map.name &&
2223 rinfo->sub_type != RIP_ROUTE_INTERFACE) {
2224 ret = route_map_apply(
2225 rip->redist[rinfo->type].route_map.map,
2226 (struct prefix *)p, rinfo);
2227
2228 if (ret == RMAP_DENYMATCH) {
2229 if (IS_RIP_DEBUG_PACKET)
2230 zlog_debug(
2231 "%pFX is filtered by route-map",
2232 p);
2233 continue;
2234 }
2235 }
2236
2237 /* When route-map does not set metric. */
2238 if (!rinfo->metric_set) {
2239 /* If redistribute metric is set. */
2240 if (rip->redist[rinfo->type].metric_config &&
2241 rinfo->metric != RIP_METRIC_INFINITY) {
2242 rinfo->metric_out =
2243 rip->redist[rinfo->type].metric;
2244 } else {
2245 /* If the route is not connected or localy
2246 * generated one, use default-metric value
2247 */
2248 if (rinfo->type != ZEBRA_ROUTE_RIP &&
2249 rinfo->type != ZEBRA_ROUTE_CONNECT &&
2250 rinfo->metric != RIP_METRIC_INFINITY)
2251 rinfo->metric_out = rip->default_metric;
2252 }
2253 }
2254
2255 /* Apply offset-list */
2256 if (rinfo->metric != RIP_METRIC_INFINITY)
2257 rip_offset_list_apply_out(p, ifc->ifp,
2258 &rinfo->metric_out);
2259
2260 if (rinfo->metric_out > RIP_METRIC_INFINITY)
2261 rinfo->metric_out = RIP_METRIC_INFINITY;
2262
2263 /* Perform split-horizon with poisoned reverse
2264 * for RIP and connected routes.
2265 **/
2266 if (ri->split_horizon == RIP_SPLIT_HORIZON_POISONED_REVERSE) {
2267 /*
2268 * We perform split horizon for RIP and connected
2269 * route. For rip routes, we want to suppress the
2270 * route if we would end up sending the route back
2271 * on the interface that we learned it from, with a
2272 * higher metric. For connected routes, we suppress
2273 * the route if the prefix is a subset of the source
2274 * address that we are going to use for the packet
2275 * (in order to handle the case when multiple
2276 * subnets are configured on the same interface).
2277 */
2278 struct rip_info *tmp_rinfo = NULL;
2279 struct connected *tmp_ifc = NULL;
2280
2281 for (ALL_LIST_ELEMENTS_RO(list, listnode, tmp_rinfo))
2282 if (tmp_rinfo->type == ZEBRA_ROUTE_RIP &&
2283 tmp_rinfo->nh.ifindex == ifc->ifp->ifindex)
2284 rinfo->metric_out = RIP_METRIC_INFINITY;
2285
2286 if (rinfo->metric_out != RIP_METRIC_INFINITY &&
2287 rinfo->type == ZEBRA_ROUTE_CONNECT) {
2288 for (ALL_LIST_ELEMENTS_RO(ifc->ifp->connected,
2289 listnode, tmp_ifc))
2290 if (prefix_match((struct prefix *)p,
2291 tmp_ifc->address)) {
2292 rinfo->metric_out =
2293 RIP_METRIC_INFINITY;
2294 break;
2295 }
2296 }
2297 }
2298
2299 /* Prepare preamble, auth headers, if needs be */
2300 if (num == 0) {
2301 stream_putc(s, RIP_RESPONSE);
2302 stream_putc(s, version);
2303 stream_putw(s, 0);
2304
2305 /* auth header for !v1 && !no_auth */
2306 if ((ri->auth_type != RIP_NO_AUTH) &&
2307 (version != RIPv1))
2308 doff = rip_auth_header_write(
2309 s, ri, key, auth_str,
2310 RIP_AUTH_SIMPLE_SIZE);
2311 }
2312
2313 /* Write RTE to the stream. */
2314 num = rip_write_rte(num, s, p, version, rinfo);
2315 if (num == rtemax) {
2316 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
2317 rip_auth_md5_set(s, ri, doff, auth_str,
2318 RIP_AUTH_SIMPLE_SIZE);
2319
2320 ret = rip_send_packet(STREAM_DATA(s),
2321 stream_get_endp(s), to, ifc);
2322
2323 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2324 rip_packet_dump(
2325 (struct rip_packet *)STREAM_DATA(s),
2326 stream_get_endp(s), "SEND");
2327 num = 0;
2328 stream_reset(s);
2329 }
2330 }
2331
2332 /* Flush unwritten RTE. */
2333 if (num != 0) {
2334 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
2335 rip_auth_md5_set(s, ri, doff, auth_str,
2336 RIP_AUTH_SIMPLE_SIZE);
2337
2338 ret = rip_send_packet(STREAM_DATA(s), stream_get_endp(s), to,
2339 ifc);
2340
2341 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2342 rip_packet_dump((struct rip_packet *)STREAM_DATA(s),
2343 stream_get_endp(s), "SEND");
2344 stream_reset(s);
2345 }
2346
2347 /* Statistics updates. */
2348 ri->sent_updates++;
2349 }
2350
2351 /* Send RIP packet to the interface. */
2352 static void rip_update_interface(struct connected *ifc, uint8_t version,
2353 int route_type)
2354 {
2355 struct interface *ifp = ifc->ifp;
2356 struct rip_interface *ri = ifp->info;
2357 struct sockaddr_in to;
2358
2359 /* When RIP version is 2 and multicast enable interface. */
2360 if (version == RIPv2 && !ri->v2_broadcast && if_is_multicast(ifp)) {
2361 if (IS_RIP_DEBUG_EVENT)
2362 zlog_debug("multicast announce on %s ", ifp->name);
2363
2364 rip_output_process(ifc, NULL, route_type, version);
2365 return;
2366 }
2367
2368 /* If we can't send multicast packet, send it with unicast. */
2369 if (if_is_broadcast(ifp) || if_is_pointopoint(ifp)) {
2370 if (ifc->address->family == AF_INET) {
2371 /* Destination address and port setting. */
2372 memset(&to, 0, sizeof(to));
2373 if (ifc->destination)
2374 /* use specified broadcast or peer destination
2375 * addr */
2376 to.sin_addr = ifc->destination->u.prefix4;
2377 else if (ifc->address->prefixlen < IPV4_MAX_BITLEN)
2378 /* calculate the appropriate broadcast address
2379 */
2380 to.sin_addr.s_addr = ipv4_broadcast_addr(
2381 ifc->address->u.prefix4.s_addr,
2382 ifc->address->prefixlen);
2383 else
2384 /* do not know where to send the packet */
2385 return;
2386 to.sin_port = htons(RIP_PORT_DEFAULT);
2387
2388 if (IS_RIP_DEBUG_EVENT)
2389 zlog_debug("%s announce to %pI4 on %s",
2390 CONNECTED_PEER(ifc) ? "unicast"
2391 : "broadcast",
2392 &to.sin_addr, ifp->name);
2393
2394 rip_output_process(ifc, &to, route_type, version);
2395 }
2396 }
2397 }
2398
2399 /* Update send to all interface and neighbor. */
2400 static void rip_update_process(struct rip *rip, int route_type)
2401 {
2402 struct listnode *ifnode, *ifnnode;
2403 struct connected *connected;
2404 struct interface *ifp;
2405 struct rip_interface *ri;
2406 struct route_node *rp;
2407 struct sockaddr_in to;
2408 struct prefix *p;
2409
2410 /* Send RIP update to each interface. */
2411 FOR_ALL_INTERFACES (rip->vrf, ifp) {
2412 if (if_is_loopback(ifp))
2413 continue;
2414
2415 if (!if_is_operative(ifp))
2416 continue;
2417
2418 /* Fetch RIP interface information. */
2419 ri = ifp->info;
2420
2421 /* When passive interface is specified, suppress announce to the
2422 interface. */
2423 if (ri->passive)
2424 continue;
2425
2426 if (!ri->running)
2427 continue;
2428
2429 /*
2430 * If there is no version configuration in the
2431 * interface, use rip's version setting.
2432 */
2433 int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ? rip->version_send
2434 : ri->ri_send);
2435
2436 if (IS_RIP_DEBUG_EVENT)
2437 zlog_debug("SEND UPDATE to %s ifindex %d", ifp->name,
2438 ifp->ifindex);
2439
2440 /* send update on each connected network */
2441 for (ALL_LIST_ELEMENTS(ifp->connected, ifnode, ifnnode,
2442 connected)) {
2443 if (connected->address->family == AF_INET) {
2444 if (vsend & RIPv1)
2445 rip_update_interface(connected, RIPv1,
2446 route_type);
2447 if ((vsend & RIPv2) && if_is_multicast(ifp))
2448 rip_update_interface(connected, RIPv2,
2449 route_type);
2450 }
2451 }
2452 }
2453
2454 /* RIP send updates to each neighbor. */
2455 for (rp = route_top(rip->neighbor); rp; rp = route_next(rp)) {
2456 if (rp->info == NULL)
2457 continue;
2458
2459 p = &rp->p;
2460
2461 connected = if_lookup_address(&p->u.prefix4, AF_INET,
2462 rip->vrf->vrf_id);
2463 if (!connected) {
2464 zlog_warn(
2465 "Neighbor %pI4 doesn't have connected interface!",
2466 &p->u.prefix4);
2467 continue;
2468 }
2469
2470 /* Set destination address and port */
2471 memset(&to, 0, sizeof(struct sockaddr_in));
2472 to.sin_addr = p->u.prefix4;
2473 to.sin_port = htons(RIP_PORT_DEFAULT);
2474
2475 /* RIP version is rip's configuration. */
2476 rip_output_process(connected, &to, route_type,
2477 rip->version_send);
2478 }
2479 }
2480
2481 /* RIP's periodical timer. */
2482 static void rip_update(struct event *t)
2483 {
2484 struct rip *rip = EVENT_ARG(t);
2485
2486 if (IS_RIP_DEBUG_EVENT)
2487 zlog_debug("update timer fire!");
2488
2489 /* Process update output. */
2490 rip_update_process(rip, rip_all_route);
2491
2492 /* Triggered updates may be suppressed if a regular update is due by
2493 the time the triggered update would be sent. */
2494 EVENT_OFF(rip->t_triggered_interval);
2495 rip->trigger = 0;
2496
2497 /* Register myself. */
2498 rip_event(rip, RIP_UPDATE_EVENT, 0);
2499 }
2500
2501 /* Walk down the RIP routing table then clear changed flag. */
2502 static void rip_clear_changed_flag(struct rip *rip)
2503 {
2504 struct route_node *rp;
2505 struct rip_info *rinfo = NULL;
2506 struct list *list = NULL;
2507 struct listnode *listnode = NULL;
2508
2509 for (rp = route_top(rip->table); rp; rp = route_next(rp)) {
2510 list = rp->info;
2511
2512 if (list == NULL)
2513 continue;
2514
2515 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
2516 UNSET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
2517 /* This flag can be set only on the first entry. */
2518 break;
2519 }
2520 }
2521 }
2522
2523 /* Triggered update interval timer. */
2524 static void rip_triggered_interval(struct event *t)
2525 {
2526 struct rip *rip = EVENT_ARG(t);
2527
2528 if (rip->trigger) {
2529 rip->trigger = 0;
2530 rip_triggered_update(t);
2531 }
2532 }
2533
2534 /* Execute triggered update. */
2535 static void rip_triggered_update(struct event *t)
2536 {
2537 struct rip *rip = EVENT_ARG(t);
2538 int interval;
2539
2540 /* Cancel interval timer. */
2541 EVENT_OFF(rip->t_triggered_interval);
2542 rip->trigger = 0;
2543
2544 /* Logging triggered update. */
2545 if (IS_RIP_DEBUG_EVENT)
2546 zlog_debug("triggered update!");
2547
2548 /* Split Horizon processing is done when generating triggered
2549 updates as well as normal updates (see section 2.6). */
2550 rip_update_process(rip, rip_changed_route);
2551
2552 /* Once all of the triggered updates have been generated, the route
2553 change flags should be cleared. */
2554 rip_clear_changed_flag(rip);
2555
2556 /* After a triggered update is sent, a timer should be set for a
2557 random interval between 1 and 5 seconds. If other changes that
2558 would trigger updates occur before the timer expires, a single
2559 update is triggered when the timer expires. */
2560 interval = (frr_weak_random() % 5) + 1;
2561
2562 event_add_timer(master, rip_triggered_interval, rip, interval,
2563 &rip->t_triggered_interval);
2564 }
2565
2566 /* Withdraw redistributed route. */
2567 void rip_redistribute_withdraw(struct rip *rip, int type)
2568 {
2569 struct route_node *rp;
2570 struct rip_info *rinfo = NULL;
2571 struct list *list = NULL;
2572
2573 for (rp = route_top(rip->table); rp; rp = route_next(rp)) {
2574 list = rp->info;
2575
2576 if (list == NULL)
2577 continue;
2578
2579 rinfo = listgetdata(listhead(list));
2580
2581 if (rinfo->type != type)
2582 continue;
2583
2584 if (rinfo->sub_type == RIP_ROUTE_INTERFACE)
2585 continue;
2586
2587 /* Perform poisoned reverse. */
2588 rinfo->metric = RIP_METRIC_INFINITY;
2589 RIP_TIMER_ON(rinfo->t_garbage_collect, rip_garbage_collect,
2590 rip->garbage_time);
2591 EVENT_OFF(rinfo->t_timeout);
2592 rinfo->flags |= RIP_RTF_CHANGED;
2593
2594 if (IS_RIP_DEBUG_EVENT) {
2595 struct prefix_ipv4 *p = (struct prefix_ipv4 *)&rp->p;
2596
2597 zlog_debug(
2598 "Poisone %pFX on the interface %s with an infinity metric [withdraw]",
2599 p,
2600 ifindex2ifname(rinfo->nh.ifindex,
2601 rip->vrf->vrf_id));
2602 }
2603
2604 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
2605 }
2606 }
2607
2608 struct rip *rip_lookup_by_vrf_id(vrf_id_t vrf_id)
2609 {
2610 struct vrf *vrf;
2611
2612 vrf = vrf_lookup_by_id(vrf_id);
2613 if (!vrf)
2614 return NULL;
2615
2616 return vrf->info;
2617 }
2618
2619 struct rip *rip_lookup_by_vrf_name(const char *vrf_name)
2620 {
2621 struct rip rip;
2622
2623 rip.vrf_name = (char *)vrf_name;
2624
2625 return RB_FIND(rip_instance_head, &rip_instances, &rip);
2626 }
2627
2628 /* Create new RIP instance and set it to global variable. */
2629 struct rip *rip_create(const char *vrf_name, struct vrf *vrf, int socket)
2630 {
2631 struct rip *rip;
2632
2633 rip = XCALLOC(MTYPE_RIP, sizeof(struct rip));
2634 rip->vrf_name = XSTRDUP(MTYPE_RIP_VRF_NAME, vrf_name);
2635
2636 /* Set initial value. */
2637 rip->ecmp = yang_get_default_bool("%s/allow-ecmp", RIP_INSTANCE);
2638 rip->default_metric =
2639 yang_get_default_uint8("%s/default-metric", RIP_INSTANCE);
2640 rip->distance =
2641 yang_get_default_uint8("%s/distance/default", RIP_INSTANCE);
2642 rip->passive_default =
2643 yang_get_default_bool("%s/passive-default", RIP_INSTANCE);
2644 rip->garbage_time = yang_get_default_uint32("%s/timers/flush-interval",
2645 RIP_INSTANCE);
2646 rip->timeout_time = yang_get_default_uint32(
2647 "%s/timers/holddown-interval", RIP_INSTANCE);
2648 rip->update_time = yang_get_default_uint32("%s/timers/update-interval",
2649 RIP_INSTANCE);
2650 rip->version_send =
2651 yang_get_default_enum("%s/version/send", RIP_INSTANCE);
2652 rip->version_recv =
2653 yang_get_default_enum("%s/version/receive", RIP_INSTANCE);
2654
2655 /* Initialize RIP data structures. */
2656 rip->table = route_table_init();
2657 route_table_set_info(rip->table, rip);
2658 rip->neighbor = route_table_init();
2659 rip->peer_list = list_new();
2660 rip->peer_list->cmp = (int (*)(void *, void *))rip_peer_list_cmp;
2661 rip->peer_list->del = rip_peer_list_del;
2662 rip->distance_table = route_table_init();
2663 rip->distance_table->cleanup = rip_distance_table_node_cleanup;
2664 rip->enable_interface = vector_init(1);
2665 rip->enable_network = route_table_init();
2666 rip->passive_nondefault = vector_init(1);
2667 rip->offset_list_master = list_new();
2668 rip->offset_list_master->cmp = (int (*)(void *, void *))offset_list_cmp;
2669 rip->offset_list_master->del = (void (*)(void *))offset_list_free;
2670
2671 /* Distribute list install. */
2672 rip->distribute_ctx = distribute_list_ctx_create(vrf);
2673 distribute_list_add_hook(rip->distribute_ctx, rip_distribute_update);
2674 distribute_list_delete_hook(rip->distribute_ctx, rip_distribute_update);
2675
2676 /* if rmap install. */
2677 rip->if_rmap_ctx = if_rmap_ctx_create(vrf_name);
2678 if_rmap_hook_add(rip->if_rmap_ctx, rip_if_rmap_update);
2679 if_rmap_hook_delete(rip->if_rmap_ctx, rip_if_rmap_update);
2680
2681 /* Make output stream. */
2682 rip->obuf = stream_new(1500);
2683
2684 /* Enable the routing instance if possible. */
2685 if (vrf && vrf_is_enabled(vrf))
2686 rip_instance_enable(rip, vrf, socket);
2687 else {
2688 rip->vrf = NULL;
2689 rip->sock = -1;
2690 }
2691
2692 RB_INSERT(rip_instance_head, &rip_instances, rip);
2693
2694 return rip;
2695 }
2696
2697 /* Sned RIP request to the destination. */
2698 int rip_request_send(struct sockaddr_in *to, struct interface *ifp,
2699 uint8_t version, struct connected *connected)
2700 {
2701 struct rte *rte;
2702 struct rip_packet rip_packet;
2703 struct listnode *node, *nnode;
2704
2705 memset(&rip_packet, 0, sizeof(rip_packet));
2706
2707 rip_packet.command = RIP_REQUEST;
2708 rip_packet.version = version;
2709 rte = rip_packet.rte;
2710 rte->metric = htonl(RIP_METRIC_INFINITY);
2711
2712 if (connected) {
2713 /*
2714 * connected is only sent for ripv1 case, or when
2715 * interface does not support multicast. Caller loops
2716 * over each connected address for this case.
2717 */
2718 if (rip_send_packet((uint8_t *)&rip_packet, sizeof(rip_packet),
2719 to, connected)
2720 != sizeof(rip_packet))
2721 return -1;
2722 else
2723 return sizeof(rip_packet);
2724 }
2725
2726 /* send request on each connected network */
2727 for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, connected)) {
2728 struct prefix_ipv4 *p;
2729
2730 p = (struct prefix_ipv4 *)connected->address;
2731
2732 if (p->family != AF_INET)
2733 continue;
2734
2735 if (rip_send_packet((uint8_t *)&rip_packet, sizeof(rip_packet),
2736 to, connected)
2737 != sizeof(rip_packet))
2738 return -1;
2739 }
2740 return sizeof(rip_packet);
2741 }
2742
2743 static int rip_update_jitter(unsigned long time)
2744 {
2745 #define JITTER_BOUND 4
2746 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2747 Given that, we cannot let time be less than JITTER_BOUND seconds.
2748 The RIPv2 RFC says jitter should be small compared to
2749 update_time. We consider 1/JITTER_BOUND to be small.
2750 */
2751
2752 int jitter_input = time;
2753 int jitter;
2754
2755 if (jitter_input < JITTER_BOUND)
2756 jitter_input = JITTER_BOUND;
2757
2758 jitter = (((frr_weak_random() % ((jitter_input * 2) + 1))
2759 - jitter_input));
2760
2761 return jitter / JITTER_BOUND;
2762 }
2763
2764 void rip_event(struct rip *rip, enum rip_event event, int sock)
2765 {
2766 int jitter = 0;
2767
2768 switch (event) {
2769 case RIP_READ:
2770 event_add_read(master, rip_read, rip, sock, &rip->t_read);
2771 break;
2772 case RIP_UPDATE_EVENT:
2773 EVENT_OFF(rip->t_update);
2774 jitter = rip_update_jitter(rip->update_time);
2775 event_add_timer(master, rip_update, rip,
2776 sock ? 2 : rip->update_time + jitter,
2777 &rip->t_update);
2778 break;
2779 case RIP_TRIGGERED_UPDATE:
2780 if (rip->t_triggered_interval)
2781 rip->trigger = 1;
2782 else
2783 event_add_event(master, rip_triggered_update, rip, 0,
2784 &rip->t_triggered_update);
2785 break;
2786 default:
2787 break;
2788 }
2789 }
2790
2791 struct rip_distance *rip_distance_new(void)
2792 {
2793 return XCALLOC(MTYPE_RIP_DISTANCE, sizeof(struct rip_distance));
2794 }
2795
2796 void rip_distance_free(struct rip_distance *rdistance)
2797 {
2798 if (rdistance->access_list)
2799 free(rdistance->access_list);
2800 XFREE(MTYPE_RIP_DISTANCE, rdistance);
2801 }
2802
2803 static void rip_distance_table_node_cleanup(struct route_table *table,
2804 struct route_node *node)
2805 {
2806 struct rip_distance *rdistance;
2807
2808 rdistance = node->info;
2809 if (rdistance)
2810 rip_distance_free(rdistance);
2811 }
2812
2813 /* Apply RIP information to distance method. */
2814 uint8_t rip_distance_apply(struct rip *rip, struct rip_info *rinfo)
2815 {
2816 struct route_node *rn;
2817 struct prefix_ipv4 p;
2818 struct rip_distance *rdistance;
2819 struct access_list *alist;
2820
2821 memset(&p, 0, sizeof(p));
2822 p.family = AF_INET;
2823 p.prefix = rinfo->from;
2824 p.prefixlen = IPV4_MAX_BITLEN;
2825
2826 /* Check source address. */
2827 rn = route_node_match(rip->distance_table, (struct prefix *)&p);
2828 if (rn) {
2829 rdistance = rn->info;
2830 route_unlock_node(rn);
2831
2832 if (rdistance->access_list) {
2833 alist = access_list_lookup(AFI_IP,
2834 rdistance->access_list);
2835 if (alist == NULL)
2836 return 0;
2837 if (access_list_apply(alist, &rinfo->rp->p)
2838 == FILTER_DENY)
2839 return 0;
2840 }
2841 return rdistance->distance;
2842 }
2843
2844 return rip->distance;
2845 }
2846
2847 static void rip_distance_show(struct vty *vty, struct rip *rip)
2848 {
2849 struct route_node *rn;
2850 struct rip_distance *rdistance;
2851 int header = 1;
2852 char buf[BUFSIZ];
2853
2854 vty_out(vty, " Distance: (default is %u)\n",
2855 rip->distance ? rip->distance : ZEBRA_RIP_DISTANCE_DEFAULT);
2856
2857 for (rn = route_top(rip->distance_table); rn; rn = route_next(rn)) {
2858 rdistance = rn->info;
2859
2860 if (rdistance == NULL)
2861 continue;
2862
2863 if (header) {
2864 vty_out(vty, " Address Distance List\n");
2865 header = 0;
2866 }
2867 snprintfrr(buf, sizeof(buf), "%pFX", &rn->p);
2868 vty_out(vty, " %-20s %4d %s\n", buf, rdistance->distance,
2869 rdistance->access_list ? rdistance->access_list : "");
2870 }
2871 }
2872
2873 /* Update ECMP routes to zebra when ECMP is disabled. */
2874 void rip_ecmp_disable(struct rip *rip)
2875 {
2876 struct route_node *rp;
2877 struct rip_info *rinfo, *tmp_rinfo;
2878 struct list *list;
2879 struct listnode *node, *nextnode;
2880
2881 for (rp = route_top(rip->table); rp; rp = route_next(rp)) {
2882 list = rp->info;
2883
2884 if (!list)
2885 continue;
2886 if (listcount(list) == 0)
2887 continue;
2888
2889 rinfo = listgetdata(listhead(list));
2890 if (!rip_route_rte(rinfo))
2891 continue;
2892
2893 /* Drop all other entries, except the first one. */
2894 for (ALL_LIST_ELEMENTS(list, node, nextnode, tmp_rinfo)) {
2895 if (tmp_rinfo == rinfo)
2896 continue;
2897
2898 EVENT_OFF(tmp_rinfo->t_timeout);
2899 EVENT_OFF(tmp_rinfo->t_garbage_collect);
2900 list_delete_node(list, node);
2901 rip_info_free(tmp_rinfo);
2902 }
2903
2904 /* Update zebra. */
2905 rip_zebra_ipv4_add(rip, rp);
2906
2907 /* Set the route change flag. */
2908 SET_FLAG(rinfo->flags, RIP_RTF_CHANGED);
2909
2910 /* Signal the output process to trigger an update. */
2911 rip_event(rip, RIP_TRIGGERED_UPDATE, 0);
2912 }
2913 }
2914
2915 /* Print out routes update time. */
2916 static void rip_vty_out_uptime(struct vty *vty, struct rip_info *rinfo)
2917 {
2918 time_t clock;
2919 struct tm tm;
2920 #define TIME_BUF 25
2921 char timebuf[TIME_BUF];
2922 struct event *thread;
2923
2924 if ((thread = rinfo->t_timeout) != NULL) {
2925 clock = event_timer_remain_second(thread);
2926 gmtime_r(&clock, &tm);
2927 strftime(timebuf, TIME_BUF, "%M:%S", &tm);
2928 vty_out(vty, "%5s", timebuf);
2929 } else if ((thread = rinfo->t_garbage_collect) != NULL) {
2930 clock = event_timer_remain_second(thread);
2931 gmtime_r(&clock, &tm);
2932 strftime(timebuf, TIME_BUF, "%M:%S", &tm);
2933 vty_out(vty, "%5s", timebuf);
2934 }
2935 }
2936
2937 static const char *rip_route_type_print(int sub_type)
2938 {
2939 switch (sub_type) {
2940 case RIP_ROUTE_RTE:
2941 return "n";
2942 case RIP_ROUTE_STATIC:
2943 return "s";
2944 case RIP_ROUTE_DEFAULT:
2945 return "d";
2946 case RIP_ROUTE_REDISTRIBUTE:
2947 return "r";
2948 case RIP_ROUTE_INTERFACE:
2949 return "i";
2950 default:
2951 return "?";
2952 }
2953 }
2954
2955 DEFUN (show_ip_rip,
2956 show_ip_rip_cmd,
2957 "show ip rip [vrf NAME]",
2958 SHOW_STR
2959 IP_STR
2960 "Show RIP routes\n"
2961 VRF_CMD_HELP_STR)
2962 {
2963 struct rip *rip;
2964 struct route_node *np;
2965 struct rip_info *rinfo = NULL;
2966 struct list *list = NULL;
2967 struct listnode *listnode = NULL;
2968 const char *vrf_name;
2969 int idx = 0;
2970
2971 if (argv_find(argv, argc, "vrf", &idx))
2972 vrf_name = argv[idx + 1]->arg;
2973 else
2974 vrf_name = VRF_DEFAULT_NAME;
2975
2976 rip = rip_lookup_by_vrf_name(vrf_name);
2977 if (!rip) {
2978 vty_out(vty, "%% RIP instance not found\n");
2979 return CMD_SUCCESS;
2980 }
2981 if (!rip->enabled) {
2982 vty_out(vty, "%% RIP instance is disabled\n");
2983 return CMD_SUCCESS;
2984 }
2985
2986 vty_out(vty,
2987 "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP\n"
2988 "Sub-codes:\n"
2989 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,\n"
2990 " (i) - interface\n\n"
2991 " Network Next Hop Metric From Tag Time\n");
2992
2993 for (np = route_top(rip->table); np; np = route_next(np)) {
2994 list = np->info;
2995
2996 if (!list)
2997 continue;
2998
2999 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
3000 int len;
3001
3002 len = vty_out(vty, "%c(%s) %pFX",
3003 /* np->lock, For debugging. */
3004 zebra_route_char(rinfo->type),
3005 rip_route_type_print(rinfo->sub_type),
3006 &np->p);
3007
3008 len = 24 - len;
3009
3010 if (len > 0)
3011 vty_out(vty, "%*s", len, " ");
3012
3013 switch (rinfo->nh.type) {
3014 case NEXTHOP_TYPE_IPV4:
3015 case NEXTHOP_TYPE_IPV4_IFINDEX:
3016 vty_out(vty, "%-20pI4 %2d ",
3017 &rinfo->nh.gate.ipv4, rinfo->metric);
3018 break;
3019 case NEXTHOP_TYPE_IFINDEX:
3020 vty_out(vty, "0.0.0.0 %2d ",
3021 rinfo->metric);
3022 break;
3023 case NEXTHOP_TYPE_BLACKHOLE:
3024 vty_out(vty, "blackhole %2d ",
3025 rinfo->metric);
3026 break;
3027 case NEXTHOP_TYPE_IPV6:
3028 case NEXTHOP_TYPE_IPV6_IFINDEX:
3029 vty_out(vty, "V6 Address Hidden %2d ",
3030 rinfo->metric);
3031 break;
3032 }
3033
3034 /* Route which exist in kernel routing table. */
3035 if ((rinfo->type == ZEBRA_ROUTE_RIP) &&
3036 (rinfo->sub_type == RIP_ROUTE_RTE)) {
3037 vty_out(vty, "%-15pI4 ", &rinfo->from);
3038 vty_out(vty, "%3" ROUTE_TAG_PRI " ",
3039 (route_tag_t)rinfo->tag);
3040 rip_vty_out_uptime(vty, rinfo);
3041 } else if (rinfo->metric == RIP_METRIC_INFINITY) {
3042 vty_out(vty, "self ");
3043 vty_out(vty, "%3" ROUTE_TAG_PRI " ",
3044 (route_tag_t)rinfo->tag);
3045 rip_vty_out_uptime(vty, rinfo);
3046 } else {
3047 if (rinfo->external_metric) {
3048 len = vty_out(
3049 vty, "self (%s:%d)",
3050 zebra_route_string(rinfo->type),
3051 rinfo->external_metric);
3052 len = 16 - len;
3053 if (len > 0)
3054 vty_out(vty, "%*s", len, " ");
3055 } else
3056 vty_out(vty, "self ");
3057 vty_out(vty, "%3" ROUTE_TAG_PRI,
3058 (route_tag_t)rinfo->tag);
3059 }
3060
3061 vty_out(vty, "\n");
3062 }
3063 }
3064 return CMD_SUCCESS;
3065 }
3066
3067 /* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3068 DEFUN (show_ip_rip_status,
3069 show_ip_rip_status_cmd,
3070 "show ip rip [vrf NAME] status",
3071 SHOW_STR
3072 IP_STR
3073 "Show RIP routes\n"
3074 VRF_CMD_HELP_STR
3075 "IP routing protocol process parameters and statistics\n")
3076 {
3077 struct rip *rip;
3078 struct interface *ifp;
3079 struct rip_interface *ri;
3080 extern const struct message ri_version_msg[];
3081 const char *send_version;
3082 const char *receive_version;
3083 const char *vrf_name;
3084 int idx = 0;
3085
3086 if (argv_find(argv, argc, "vrf", &idx))
3087 vrf_name = argv[idx + 1]->arg;
3088 else
3089 vrf_name = VRF_DEFAULT_NAME;
3090
3091 rip = rip_lookup_by_vrf_name(vrf_name);
3092 if (!rip) {
3093 vty_out(vty, "%% RIP instance not found\n");
3094 return CMD_SUCCESS;
3095 }
3096 if (!rip->enabled) {
3097 vty_out(vty, "%% RIP instance is disabled\n");
3098 return CMD_SUCCESS;
3099 }
3100
3101 vty_out(vty, "Routing Protocol is \"rip\"\n");
3102 vty_out(vty, " Sending updates every %u seconds with +/-50%%,",
3103 rip->update_time);
3104 vty_out(vty, " next due in %lu seconds\n",
3105 event_timer_remain_second(rip->t_update));
3106 vty_out(vty, " Timeout after %u seconds,", rip->timeout_time);
3107 vty_out(vty, " garbage collect after %u seconds\n", rip->garbage_time);
3108
3109 /* Filtering status show. */
3110 config_show_distribute(vty, rip->distribute_ctx);
3111
3112 /* Default metric information. */
3113 vty_out(vty, " Default redistribution metric is %u\n",
3114 rip->default_metric);
3115
3116 /* Redistribute information. */
3117 vty_out(vty, " Redistributing:");
3118 rip_show_redistribute_config(vty, rip);
3119 vty_out(vty, "\n");
3120
3121 vty_out(vty, " Default version control: send version %s,",
3122 lookup_msg(ri_version_msg, rip->version_send, NULL));
3123 if (rip->version_recv == RI_RIP_VERSION_1_AND_2)
3124 vty_out(vty, " receive any version \n");
3125 else
3126 vty_out(vty, " receive version %s \n",
3127 lookup_msg(ri_version_msg, rip->version_recv, NULL));
3128
3129 vty_out(vty, " Interface Send Recv Key-chain\n");
3130
3131 FOR_ALL_INTERFACES (rip->vrf, ifp) {
3132 ri = ifp->info;
3133
3134 if (!ri->running)
3135 continue;
3136
3137 if (ri->enable_network || ri->enable_interface) {
3138 if (ri->ri_send == RI_RIP_UNSPEC)
3139 send_version =
3140 lookup_msg(ri_version_msg,
3141 rip->version_send, NULL);
3142 else
3143 send_version = lookup_msg(ri_version_msg,
3144 ri->ri_send, NULL);
3145
3146 if (ri->ri_receive == RI_RIP_UNSPEC)
3147 receive_version =
3148 lookup_msg(ri_version_msg,
3149 rip->version_recv, NULL);
3150 else
3151 receive_version = lookup_msg(
3152 ri_version_msg, ri->ri_receive, NULL);
3153
3154 vty_out(vty, " %-17s%-3s %-3s %s\n", ifp->name,
3155 send_version, receive_version,
3156 ri->key_chain ? ri->key_chain : "");
3157 }
3158 }
3159
3160 vty_out(vty, " Routing for Networks:\n");
3161 rip_show_network_config(vty, rip);
3162
3163 int found_passive = 0;
3164 FOR_ALL_INTERFACES (rip->vrf, ifp) {
3165 ri = ifp->info;
3166
3167 if ((ri->enable_network || ri->enable_interface) &&
3168 ri->passive) {
3169 if (!found_passive) {
3170 vty_out(vty, " Passive Interface(s):\n");
3171 found_passive = 1;
3172 }
3173 vty_out(vty, " %s\n", ifp->name);
3174 }
3175 }
3176
3177 vty_out(vty, " Routing Information Sources:\n");
3178 vty_out(vty,
3179 " Gateway BadPackets BadRoutes Distance Last Update\n");
3180 rip_peer_display(vty, rip);
3181
3182 rip_distance_show(vty, rip);
3183
3184 return CMD_SUCCESS;
3185 }
3186
3187 /* RIP configuration write function. */
3188 static int config_write_rip(struct vty *vty)
3189 {
3190 struct rip *rip;
3191 int write = 0;
3192
3193 RB_FOREACH(rip, rip_instance_head, &rip_instances) {
3194 char xpath[XPATH_MAXLEN];
3195 struct lyd_node *dnode;
3196
3197 snprintf(xpath, sizeof(xpath),
3198 "/frr-ripd:ripd/instance[vrf='%s']", rip->vrf_name);
3199
3200 dnode = yang_dnode_get(running_config->dnode, xpath);
3201 assert(dnode);
3202
3203 nb_cli_show_dnode_cmds(vty, dnode, false);
3204
3205 /* Distribute configuration. */
3206 config_write_distribute(vty, rip->distribute_ctx);
3207
3208 vty_out(vty, "exit\n");
3209
3210 write = 1;
3211 }
3212
3213 return write;
3214 }
3215
3216 static int config_write_rip(struct vty *vty);
3217 /* RIP node structure. */
3218 static struct cmd_node rip_node = {
3219 .name = "rip",
3220 .node = RIP_NODE,
3221 .parent_node = CONFIG_NODE,
3222 .prompt = "%s(config-router)# ",
3223 .config_write = config_write_rip,
3224 };
3225
3226 /* Distribute-list update functions. */
3227 static void rip_distribute_update(struct distribute_ctx *ctx,
3228 struct distribute *dist)
3229 {
3230 struct interface *ifp;
3231 struct rip_interface *ri;
3232 struct access_list *alist;
3233 struct prefix_list *plist;
3234
3235 if (!ctx->vrf || !dist->ifname)
3236 return;
3237
3238 ifp = if_lookup_by_name(dist->ifname, ctx->vrf->vrf_id);
3239 if (ifp == NULL)
3240 return;
3241
3242 ri = ifp->info;
3243
3244 if (dist->list[DISTRIBUTE_V4_IN]) {
3245 alist = access_list_lookup(AFI_IP,
3246 dist->list[DISTRIBUTE_V4_IN]);
3247 if (alist)
3248 ri->list[RIP_FILTER_IN] = alist;
3249 else
3250 ri->list[RIP_FILTER_IN] = NULL;
3251 } else
3252 ri->list[RIP_FILTER_IN] = NULL;
3253
3254 if (dist->list[DISTRIBUTE_V4_OUT]) {
3255 alist = access_list_lookup(AFI_IP,
3256 dist->list[DISTRIBUTE_V4_OUT]);
3257 if (alist)
3258 ri->list[RIP_FILTER_OUT] = alist;
3259 else
3260 ri->list[RIP_FILTER_OUT] = NULL;
3261 } else
3262 ri->list[RIP_FILTER_OUT] = NULL;
3263
3264 if (dist->prefix[DISTRIBUTE_V4_IN]) {
3265 plist = prefix_list_lookup(AFI_IP,
3266 dist->prefix[DISTRIBUTE_V4_IN]);
3267 if (plist)
3268 ri->prefix[RIP_FILTER_IN] = plist;
3269 else
3270 ri->prefix[RIP_FILTER_IN] = NULL;
3271 } else
3272 ri->prefix[RIP_FILTER_IN] = NULL;
3273
3274 if (dist->prefix[DISTRIBUTE_V4_OUT]) {
3275 plist = prefix_list_lookup(AFI_IP,
3276 dist->prefix[DISTRIBUTE_V4_OUT]);
3277 if (plist)
3278 ri->prefix[RIP_FILTER_OUT] = plist;
3279 else
3280 ri->prefix[RIP_FILTER_OUT] = NULL;
3281 } else
3282 ri->prefix[RIP_FILTER_OUT] = NULL;
3283 }
3284
3285 void rip_distribute_update_interface(struct interface *ifp)
3286 {
3287 struct rip_interface *ri = ifp->info;
3288 struct rip *rip = ri->rip;
3289 struct distribute *dist;
3290
3291 if (!rip)
3292 return;
3293 dist = distribute_lookup(rip->distribute_ctx, ifp->name);
3294 if (dist)
3295 rip_distribute_update(rip->distribute_ctx, dist);
3296 }
3297
3298 /* Update all interface's distribute list. */
3299 /* ARGSUSED */
3300 static void rip_distribute_update_all(struct prefix_list *notused)
3301 {
3302 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
3303 struct interface *ifp;
3304
3305 FOR_ALL_INTERFACES (vrf, ifp)
3306 rip_distribute_update_interface(ifp);
3307 }
3308 /* ARGSUSED */
3309 static void rip_distribute_update_all_wrapper(struct access_list *notused)
3310 {
3311 rip_distribute_update_all(NULL);
3312 }
3313
3314 /* Delete all added rip route. */
3315 void rip_clean(struct rip *rip)
3316 {
3317 rip_interfaces_clean(rip);
3318
3319 if (rip->enabled)
3320 rip_instance_disable(rip);
3321
3322 stream_free(rip->obuf);
3323
3324 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
3325 if (rip->redist[i].route_map.name)
3326 free(rip->redist[i].route_map.name);
3327
3328 route_table_finish(rip->table);
3329 route_table_finish(rip->neighbor);
3330 list_delete(&rip->peer_list);
3331 distribute_list_delete(&rip->distribute_ctx);
3332 if_rmap_ctx_delete(rip->if_rmap_ctx);
3333
3334 rip_clean_network(rip);
3335 rip_passive_nondefault_clean(rip);
3336 vector_free(rip->enable_interface);
3337 route_table_finish(rip->enable_network);
3338 vector_free(rip->passive_nondefault);
3339 list_delete(&rip->offset_list_master);
3340 route_table_finish(rip->distance_table);
3341
3342 RB_REMOVE(rip_instance_head, &rip_instances, rip);
3343 XFREE(MTYPE_TMP, rip->default_bfd_profile);
3344 XFREE(MTYPE_RIP_VRF_NAME, rip->vrf_name);
3345 XFREE(MTYPE_RIP, rip);
3346 }
3347
3348 static void rip_if_rmap_update(struct if_rmap_ctx *ctx,
3349 struct if_rmap *if_rmap)
3350 {
3351 struct interface *ifp = NULL;
3352 struct rip_interface *ri;
3353 struct route_map *rmap;
3354 struct vrf *vrf = NULL;
3355
3356 if (ctx->name)
3357 vrf = vrf_lookup_by_name(ctx->name);
3358 if (vrf)
3359 ifp = if_lookup_by_name(if_rmap->ifname, vrf->vrf_id);
3360 if (ifp == NULL)
3361 return;
3362
3363 ri = ifp->info;
3364 if (if_rmap->routemap[IF_RMAP_IN]) {
3365 rmap = route_map_lookup_by_name(if_rmap->routemap[IF_RMAP_IN]);
3366 if (rmap)
3367 ri->routemap[IF_RMAP_IN] = rmap;
3368 else
3369 ri->routemap[IF_RMAP_IN] = NULL;
3370 } else
3371 ri->routemap[RIP_FILTER_IN] = NULL;
3372
3373 if (if_rmap->routemap[IF_RMAP_OUT]) {
3374 rmap = route_map_lookup_by_name(if_rmap->routemap[IF_RMAP_OUT]);
3375 if (rmap)
3376 ri->routemap[IF_RMAP_OUT] = rmap;
3377 else
3378 ri->routemap[IF_RMAP_OUT] = NULL;
3379 } else
3380 ri->routemap[RIP_FILTER_OUT] = NULL;
3381 }
3382
3383 void rip_if_rmap_update_interface(struct interface *ifp)
3384 {
3385 struct rip_interface *ri = ifp->info;
3386 struct rip *rip = ri->rip;
3387 struct if_rmap *if_rmap;
3388 struct if_rmap_ctx *ctx;
3389
3390 if (!rip)
3391 return;
3392 ctx = rip->if_rmap_ctx;
3393 if (!ctx)
3394 return;
3395 if_rmap = if_rmap_lookup(ctx, ifp->name);
3396 if (if_rmap)
3397 rip_if_rmap_update(ctx, if_rmap);
3398 }
3399
3400 static void rip_routemap_update_redistribute(struct rip *rip)
3401 {
3402 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
3403 if (rip->redist[i].route_map.name) {
3404 rip->redist[i].route_map.map = route_map_lookup_by_name(
3405 rip->redist[i].route_map.name);
3406 route_map_counter_increment(
3407 rip->redist[i].route_map.map);
3408 }
3409 }
3410 }
3411
3412 /* ARGSUSED */
3413 static void rip_routemap_update(const char *notused)
3414 {
3415 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
3416 struct rip *rip;
3417 struct interface *ifp;
3418
3419 FOR_ALL_INTERFACES (vrf, ifp)
3420 rip_if_rmap_update_interface(ifp);
3421
3422 rip = vrf->info;
3423 if (rip)
3424 rip_routemap_update_redistribute(rip);
3425 }
3426
3427 /* Link RIP instance to VRF. */
3428 static void rip_vrf_link(struct rip *rip, struct vrf *vrf)
3429 {
3430 struct interface *ifp;
3431
3432 rip->vrf = vrf;
3433 rip->distribute_ctx->vrf = vrf;
3434 vrf->info = rip;
3435
3436 FOR_ALL_INTERFACES (vrf, ifp)
3437 rip_interface_sync(ifp);
3438 }
3439
3440 /* Unlink RIP instance from VRF. */
3441 static void rip_vrf_unlink(struct rip *rip, struct vrf *vrf)
3442 {
3443 struct interface *ifp;
3444
3445 rip->vrf = NULL;
3446 rip->distribute_ctx->vrf = NULL;
3447 vrf->info = NULL;
3448
3449 FOR_ALL_INTERFACES (vrf, ifp)
3450 rip_interface_sync(ifp);
3451 }
3452
3453 static void rip_instance_enable(struct rip *rip, struct vrf *vrf, int sock)
3454 {
3455 rip->sock = sock;
3456
3457 rip_vrf_link(rip, vrf);
3458 rip->enabled = true;
3459
3460 /* Resend all redistribute requests. */
3461 rip_redistribute_enable(rip);
3462
3463 /* Create read and timer thread. */
3464 rip_event(rip, RIP_READ, rip->sock);
3465 rip_event(rip, RIP_UPDATE_EVENT, 1);
3466
3467 rip_zebra_vrf_register(vrf);
3468 }
3469
3470 static void rip_instance_disable(struct rip *rip)
3471 {
3472 struct vrf *vrf = rip->vrf;
3473 struct route_node *rp;
3474
3475 /* Clear RIP routes */
3476 for (rp = route_top(rip->table); rp; rp = route_next(rp)) {
3477 struct rip_info *rinfo;
3478 struct list *list;
3479 struct listnode *listnode;
3480
3481 if ((list = rp->info) == NULL)
3482 continue;
3483
3484 rinfo = listgetdata(listhead(list));
3485 if (rip_route_rte(rinfo))
3486 rip_zebra_ipv4_delete(rip, rp);
3487
3488 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
3489 EVENT_OFF(rinfo->t_timeout);
3490 EVENT_OFF(rinfo->t_garbage_collect);
3491 rip_info_free(rinfo);
3492 }
3493 list_delete(&list);
3494 rp->info = NULL;
3495 route_unlock_node(rp);
3496 }
3497
3498 /* Flush all redistribute requests. */
3499 rip_redistribute_disable(rip);
3500
3501 /* Cancel RIP related timers. */
3502 EVENT_OFF(rip->t_update);
3503 EVENT_OFF(rip->t_triggered_update);
3504 EVENT_OFF(rip->t_triggered_interval);
3505
3506 /* Cancel read thread. */
3507 EVENT_OFF(rip->t_read);
3508
3509 /* Close RIP socket. */
3510 close(rip->sock);
3511 rip->sock = -1;
3512
3513 /* Clear existing peers. */
3514 list_delete_all_node(rip->peer_list);
3515
3516 rip_zebra_vrf_deregister(vrf);
3517
3518 rip_vrf_unlink(rip, vrf);
3519 rip->enabled = false;
3520 }
3521
3522 static int rip_vrf_new(struct vrf *vrf)
3523 {
3524 if (IS_RIP_DEBUG_EVENT)
3525 zlog_debug("%s: VRF created: %s(%u)", __func__, vrf->name,
3526 vrf->vrf_id);
3527
3528 return 0;
3529 }
3530
3531 static int rip_vrf_delete(struct vrf *vrf)
3532 {
3533 struct rip *rip;
3534
3535 if (IS_RIP_DEBUG_EVENT)
3536 zlog_debug("%s: VRF deleted: %s(%u)", __func__, vrf->name,
3537 vrf->vrf_id);
3538
3539 rip = rip_lookup_by_vrf_name(vrf->name);
3540 if (!rip)
3541 return 0;
3542
3543 rip_clean(rip);
3544
3545 return 0;
3546 }
3547
3548 static int rip_vrf_enable(struct vrf *vrf)
3549 {
3550 struct rip *rip;
3551 int socket;
3552
3553 rip = rip_lookup_by_vrf_name(vrf->name);
3554 if (!rip || rip->enabled)
3555 return 0;
3556
3557 if (IS_RIP_DEBUG_EVENT)
3558 zlog_debug("%s: VRF %s(%u) enabled", __func__, vrf->name,
3559 vrf->vrf_id);
3560
3561 /* Activate the VRF RIP instance. */
3562 if (!rip->enabled) {
3563 socket = rip_create_socket(vrf);
3564 if (socket < 0)
3565 return -1;
3566
3567 rip_instance_enable(rip, vrf, socket);
3568 }
3569
3570 return 0;
3571 }
3572
3573 static int rip_vrf_disable(struct vrf *vrf)
3574 {
3575 struct rip *rip;
3576
3577 rip = rip_lookup_by_vrf_name(vrf->name);
3578 if (!rip || !rip->enabled)
3579 return 0;
3580
3581 if (IS_RIP_DEBUG_EVENT)
3582 zlog_debug("%s: VRF %s(%u) disabled", __func__, vrf->name,
3583 vrf->vrf_id);
3584
3585 /* Deactivate the VRF RIP instance. */
3586 if (rip->enabled)
3587 rip_instance_disable(rip);
3588
3589 return 0;
3590 }
3591
3592 void rip_vrf_init(void)
3593 {
3594 vrf_init(rip_vrf_new, rip_vrf_enable, rip_vrf_disable, rip_vrf_delete);
3595
3596 vrf_cmd_init(NULL);
3597 }
3598
3599 void rip_vrf_terminate(void)
3600 {
3601 vrf_terminate();
3602 }
3603
3604 /* Allocate new rip structure and set default value. */
3605 void rip_init(void)
3606 {
3607 /* Install top nodes. */
3608 install_node(&rip_node);
3609
3610 /* Install rip commands. */
3611 install_element(VIEW_NODE, &show_ip_rip_cmd);
3612 install_element(VIEW_NODE, &show_ip_rip_status_cmd);
3613
3614 install_default(RIP_NODE);
3615
3616 /* Debug related init. */
3617 rip_debug_init();
3618
3619 /* Access list install. */
3620 access_list_init();
3621 access_list_add_hook(rip_distribute_update_all_wrapper);
3622 access_list_delete_hook(rip_distribute_update_all_wrapper);
3623
3624 /* Prefix list initialize.*/
3625 prefix_list_init();
3626 prefix_list_add_hook(rip_distribute_update_all);
3627 prefix_list_delete_hook(rip_distribute_update_all);
3628
3629 /* Route-map */
3630 rip_route_map_init();
3631
3632 route_map_add_hook(rip_routemap_update);
3633 route_map_delete_hook(rip_routemap_update);
3634
3635 if_rmap_init(RIP_NODE);
3636 }