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