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