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