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