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