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