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