blob: 518e48616846989ede0b04fa7a1e3fae07c909b9 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* RIP version 1 and 2.
vincentfbf5d032005-09-29 11:25:50 +00002 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
paul718e3742002-12-13 20:15:29 +00003 * 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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
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"
hasso1af81932004-09-26 16:11:14 +000035#include "sockopt.h"
paul718e3742002-12-13 20:15:29 +000036#include "routemap.h"
hasso16705132003-05-25 14:49:19 +000037#include "if_rmap.h"
paul718e3742002-12-13 20:15:29 +000038#include "plist.h"
39#include "distribute.h"
vincentc1a03d42005-09-28 15:47:44 +000040#include "md5.h"
paul718e3742002-12-13 20:15:29 +000041#include "keychain.h"
pauledd7c242003-06-04 13:59:38 +000042#include "privs.h"
paul718e3742002-12-13 20:15:29 +000043
44#include "ripd/ripd.h"
45#include "ripd/rip_debug.h"
46
paul0b3acf42004-09-17 08:39:08 +000047/* UDP receive buffer size */
48#define RIP_UDP_RCV_BUF 41600
49
50/* privileges global */
pauledd7c242003-06-04 13:59:38 +000051extern struct zebra_privs_t ripd_privs;
52
paul718e3742002-12-13 20:15:29 +000053/* RIP Structure. */
54struct rip *rip = NULL;
55
56/* RIP neighbor address table. */
57struct route_table *rip_neighbor_table;
58
59/* RIP route changes. */
60long rip_global_route_changes = 0;
61
62/* RIP queries. */
63long rip_global_queries = 0;
64
65/* Prototypes. */
pauldc63bfd2005-10-25 23:31:05 +000066static void rip_event (enum rip_event, int);
67static void rip_output_process (struct connected *, struct sockaddr_in *, int, u_char);
68static int rip_triggered_update (struct thread *);
69static int rip_update_jitter (unsigned long);
70
paul718e3742002-12-13 20:15:29 +000071/* RIP output routes type. */
72enum
73{
74 rip_all_route,
75 rip_changed_route
76};
77
78/* RIP command strings. */
79struct message rip_msg[] =
80{
81 {RIP_REQUEST, "REQUEST"},
82 {RIP_RESPONSE, "RESPONSE"},
83 {RIP_TRACEON, "TRACEON"},
84 {RIP_TRACEOFF, "TRACEOFF"},
85 {RIP_POLL, "POLL"},
86 {RIP_POLL_ENTRY, "POLL ENTRY"},
87 {0, NULL}
88};
paul718e3742002-12-13 20:15:29 +000089
90/* Utility function to set boradcast option to the socket. */
pauldc63bfd2005-10-25 23:31:05 +000091static int
paul718e3742002-12-13 20:15:29 +000092sockopt_broadcast (int sock)
93{
94 int ret;
95 int on = 1;
96
97 ret = setsockopt (sock, SOL_SOCKET, SO_BROADCAST, (char *) &on, sizeof on);
98 if (ret < 0)
99 {
100 zlog_warn ("can't set sockopt SO_BROADCAST to socket %d", sock);
101 return -1;
102 }
103 return 0;
104}
105
pauldc63bfd2005-10-25 23:31:05 +0000106static int
paul718e3742002-12-13 20:15:29 +0000107rip_route_rte (struct rip_info *rinfo)
108{
109 return (rinfo->type == ZEBRA_ROUTE_RIP && rinfo->sub_type == RIP_ROUTE_RTE);
110}
111
pauldc63bfd2005-10-25 23:31:05 +0000112static struct rip_info *
paul718e3742002-12-13 20:15:29 +0000113rip_info_new ()
114{
115 struct rip_info *new;
116
117 new = XMALLOC (MTYPE_RIP_INFO, sizeof (struct rip_info));
118 memset (new, 0, sizeof (struct rip_info));
119 return new;
120}
121
122void
123rip_info_free (struct rip_info *rinfo)
124{
125 XFREE (MTYPE_RIP_INFO, rinfo);
126}
127
128/* RIP route garbage collect timer. */
pauldc63bfd2005-10-25 23:31:05 +0000129static int
paul718e3742002-12-13 20:15:29 +0000130rip_garbage_collect (struct thread *t)
131{
132 struct rip_info *rinfo;
133 struct route_node *rp;
134
135 rinfo = THREAD_ARG (t);
136 rinfo->t_garbage_collect = NULL;
137
138 /* Off timeout timer. */
139 RIP_TIMER_OFF (rinfo->t_timeout);
140
141 /* Get route_node pointer. */
142 rp = rinfo->rp;
143
144 /* Unlock route_node. */
145 rp->info = NULL;
146 route_unlock_node (rp);
147
148 /* Free RIP routing information. */
149 rip_info_free (rinfo);
150
151 return 0;
152}
153
154/* Timeout RIP routes. */
pauldc63bfd2005-10-25 23:31:05 +0000155static int
paul718e3742002-12-13 20:15:29 +0000156rip_timeout (struct thread *t)
157{
158 struct rip_info *rinfo;
159 struct route_node *rn;
160
161 rinfo = THREAD_ARG (t);
162 rinfo->t_timeout = NULL;
163
164 rn = rinfo->rp;
165
166 /* - The garbage-collection timer is set for 120 seconds. */
167 RIP_TIMER_ON (rinfo->t_garbage_collect, rip_garbage_collect,
168 rip->garbage_time);
169
170 rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rn->p, &rinfo->nexthop,
171 rinfo->metric);
172 /* - The metric for the route is set to 16 (infinity). This causes
173 the route to be removed from service. */
174 rinfo->metric = RIP_METRIC_INFINITY;
175 rinfo->flags &= ~RIP_RTF_FIB;
176
177 /* - The route change flag is to indicate that this entry has been
178 changed. */
179 rinfo->flags |= RIP_RTF_CHANGED;
180
181 /* - The output process is signalled to trigger a response. */
182 rip_event (RIP_TRIGGERED_UPDATE, 0);
183
184 return 0;
185}
186
pauldc63bfd2005-10-25 23:31:05 +0000187static void
paul718e3742002-12-13 20:15:29 +0000188rip_timeout_update (struct rip_info *rinfo)
189{
190 if (rinfo->metric != RIP_METRIC_INFINITY)
191 {
192 RIP_TIMER_OFF (rinfo->t_timeout);
193 RIP_TIMER_ON (rinfo->t_timeout, rip_timeout, rip->timeout_time);
194 }
195}
196
pauldc63bfd2005-10-25 23:31:05 +0000197static int
paul718e3742002-12-13 20:15:29 +0000198rip_incoming_filter (struct prefix_ipv4 *p, struct rip_interface *ri)
199{
200 struct distribute *dist;
201 struct access_list *alist;
202 struct prefix_list *plist;
203
204 /* Input distribute-list filtering. */
205 if (ri->list[RIP_FILTER_IN])
206 {
207 if (access_list_apply (ri->list[RIP_FILTER_IN],
208 (struct prefix *) p) == FILTER_DENY)
209 {
210 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000211 zlog_debug ("%s/%d filtered by distribute in",
paul718e3742002-12-13 20:15:29 +0000212 inet_ntoa (p->prefix), p->prefixlen);
213 return -1;
214 }
215 }
216 if (ri->prefix[RIP_FILTER_IN])
217 {
218 if (prefix_list_apply (ri->prefix[RIP_FILTER_IN],
219 (struct prefix *) p) == PREFIX_DENY)
220 {
221 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000222 zlog_debug ("%s/%d filtered by prefix-list in",
paul718e3742002-12-13 20:15:29 +0000223 inet_ntoa (p->prefix), p->prefixlen);
224 return -1;
225 }
226 }
227
228 /* All interface filter check. */
229 dist = distribute_lookup (NULL);
230 if (dist)
231 {
232 if (dist->list[DISTRIBUTE_IN])
233 {
234 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_IN]);
235
236 if (alist)
237 {
238 if (access_list_apply (alist,
239 (struct prefix *) p) == FILTER_DENY)
240 {
241 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000242 zlog_debug ("%s/%d filtered by distribute in",
paul718e3742002-12-13 20:15:29 +0000243 inet_ntoa (p->prefix), p->prefixlen);
244 return -1;
245 }
246 }
247 }
248 if (dist->prefix[DISTRIBUTE_IN])
249 {
250 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_IN]);
251
252 if (plist)
253 {
254 if (prefix_list_apply (plist,
255 (struct prefix *) p) == PREFIX_DENY)
256 {
257 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000258 zlog_debug ("%s/%d filtered by prefix-list in",
paul718e3742002-12-13 20:15:29 +0000259 inet_ntoa (p->prefix), p->prefixlen);
260 return -1;
261 }
262 }
263 }
264 }
265 return 0;
266}
267
pauldc63bfd2005-10-25 23:31:05 +0000268static int
paul718e3742002-12-13 20:15:29 +0000269rip_outgoing_filter (struct prefix_ipv4 *p, struct rip_interface *ri)
270{
271 struct distribute *dist;
272 struct access_list *alist;
273 struct prefix_list *plist;
274
275 if (ri->list[RIP_FILTER_OUT])
276 {
277 if (access_list_apply (ri->list[RIP_FILTER_OUT],
278 (struct prefix *) p) == FILTER_DENY)
279 {
280 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000281 zlog_debug ("%s/%d is filtered by distribute out",
paul718e3742002-12-13 20:15:29 +0000282 inet_ntoa (p->prefix), p->prefixlen);
283 return -1;
284 }
285 }
286 if (ri->prefix[RIP_FILTER_OUT])
287 {
288 if (prefix_list_apply (ri->prefix[RIP_FILTER_OUT],
289 (struct prefix *) p) == PREFIX_DENY)
290 {
291 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000292 zlog_debug ("%s/%d is filtered by prefix-list out",
paul718e3742002-12-13 20:15:29 +0000293 inet_ntoa (p->prefix), p->prefixlen);
294 return -1;
295 }
296 }
297
298 /* All interface filter check. */
299 dist = distribute_lookup (NULL);
300 if (dist)
301 {
302 if (dist->list[DISTRIBUTE_OUT])
303 {
304 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_OUT]);
305
306 if (alist)
307 {
308 if (access_list_apply (alist,
309 (struct prefix *) p) == FILTER_DENY)
310 {
311 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000312 zlog_debug ("%s/%d filtered by distribute out",
paul718e3742002-12-13 20:15:29 +0000313 inet_ntoa (p->prefix), p->prefixlen);
314 return -1;
315 }
316 }
317 }
318 if (dist->prefix[DISTRIBUTE_OUT])
319 {
320 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_OUT]);
321
322 if (plist)
323 {
324 if (prefix_list_apply (plist,
325 (struct prefix *) p) == PREFIX_DENY)
326 {
327 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000328 zlog_debug ("%s/%d filtered by prefix-list out",
paul718e3742002-12-13 20:15:29 +0000329 inet_ntoa (p->prefix), p->prefixlen);
330 return -1;
331 }
332 }
333 }
334 }
335 return 0;
336}
337
338/* Check nexthop address validity. */
339static int
340rip_nexthop_check (struct in_addr *addr)
341{
hasso52dc7ee2004-09-23 19:18:23 +0000342 struct listnode *node;
343 struct listnode *cnode;
paul718e3742002-12-13 20:15:29 +0000344 struct interface *ifp;
345 struct connected *ifc;
346 struct prefix *p;
347
348 /* If nexthop address matches local configured address then it is
349 invalid nexthop. */
paul1eb8ef22005-04-07 07:30:20 +0000350 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000351 {
paul1eb8ef22005-04-07 07:30:20 +0000352 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, ifc))
paul718e3742002-12-13 20:15:29 +0000353 {
paul718e3742002-12-13 20:15:29 +0000354 p = ifc->address;
355
356 if (p->family == AF_INET
357 && IPV4_ADDR_SAME (&p->u.prefix4, addr))
358 return -1;
359 }
360 }
361 return 0;
362}
363
364/* RIP add route to routing table. */
pauldc63bfd2005-10-25 23:31:05 +0000365static void
paul718e3742002-12-13 20:15:29 +0000366rip_rte_process (struct rte *rte, struct sockaddr_in *from,
paula87552c2004-05-03 20:00:17 +0000367 struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000368{
369 int ret;
370 struct prefix_ipv4 p;
371 struct route_node *rp;
paulb94f9db2004-05-01 20:45:38 +0000372 struct rip_info *rinfo, rinfotmp;
paul718e3742002-12-13 20:15:29 +0000373 struct rip_interface *ri;
374 struct in_addr *nexthop;
375 u_char oldmetric;
376 int same = 0;
vincentfbf5d032005-09-29 11:25:50 +0000377 int route_reuse = 0;
378 unsigned char old_dist, new_dist;
paul718e3742002-12-13 20:15:29 +0000379
380 /* Make prefix structure. */
381 memset (&p, 0, sizeof (struct prefix_ipv4));
382 p.family = AF_INET;
383 p.prefix = rte->prefix;
384 p.prefixlen = ip_masklen (rte->mask);
385
386 /* Make sure mask is applied. */
387 apply_mask_ipv4 (&p);
388
389 /* Apply input filters. */
390 ri = ifp->info;
391
392 ret = rip_incoming_filter (&p, ri);
393 if (ret < 0)
394 return;
395
hasso16705132003-05-25 14:49:19 +0000396 /* Modify entry according to the interface routemap. */
397 if (ri->routemap[RIP_FILTER_IN])
398 {
399 int ret;
400 struct rip_info newinfo;
401
402 memset (&newinfo, 0, sizeof (newinfo));
403 newinfo.type = ZEBRA_ROUTE_RIP;
404 newinfo.sub_type = RIP_ROUTE_RTE;
paula87552c2004-05-03 20:00:17 +0000405 newinfo.nexthop = rte->nexthop;
406 newinfo.from = from->sin_addr;
407 newinfo.ifindex = ifp->ifindex;
hasso16705132003-05-25 14:49:19 +0000408 newinfo.metric = rte->metric;
409 newinfo.metric_out = rte->metric; /* XXX */
paula87552c2004-05-03 20:00:17 +0000410 newinfo.tag = ntohs (rte->tag); /* XXX */
hasso16705132003-05-25 14:49:19 +0000411
412 /* The object should be of the type of rip_info */
paula87552c2004-05-03 20:00:17 +0000413 ret = route_map_apply (ri->routemap[RIP_FILTER_IN],
414 (struct prefix *) &p, RMAP_RIP, &newinfo);
hasso16705132003-05-25 14:49:19 +0000415
416 if (ret == RMAP_DENYMATCH)
paula87552c2004-05-03 20:00:17 +0000417 {
418 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000419 zlog_debug ("RIP %s/%d is filtered by route-map in",
paula87552c2004-05-03 20:00:17 +0000420 inet_ntoa (p.prefix), p.prefixlen);
421 return;
422 }
hasso16705132003-05-25 14:49:19 +0000423
424 /* Get back the object */
paula87552c2004-05-03 20:00:17 +0000425 rte->nexthop = newinfo.nexthop_out;
426 rte->tag = htons (newinfo.tag_out); /* XXX */
427 rte->metric = newinfo.metric_out; /* XXX: the routemap uses the metric_out field */
hasso16705132003-05-25 14:49:19 +0000428 }
429
paul718e3742002-12-13 20:15:29 +0000430 /* Once the entry has been validated, update the metric by
431 adding the cost of the network on wich the message
432 arrived. If the result is greater than infinity, use infinity
433 (RFC2453 Sec. 3.9.2) */
434 /* Zebra ripd can handle offset-list in. */
435 ret = rip_offset_list_apply_in (&p, ifp, &rte->metric);
436
437 /* If offset-list does not modify the metric use interface's
438 metric. */
paula87552c2004-05-03 20:00:17 +0000439 if (!ret)
paul718e3742002-12-13 20:15:29 +0000440 rte->metric += ifp->metric;
441
442 if (rte->metric > RIP_METRIC_INFINITY)
443 rte->metric = RIP_METRIC_INFINITY;
444
445 /* Set nexthop pointer. */
446 if (rte->nexthop.s_addr == 0)
447 nexthop = &from->sin_addr;
448 else
449 nexthop = &rte->nexthop;
450
hasso16705132003-05-25 14:49:19 +0000451 /* Check if nexthop address is myself, then do nothing. */
paul718e3742002-12-13 20:15:29 +0000452 if (rip_nexthop_check (nexthop) < 0)
453 {
454 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +0000455 zlog_debug ("Nexthop address %s is myself", inet_ntoa (*nexthop));
paul718e3742002-12-13 20:15:29 +0000456 return;
457 }
458
459 /* Get index for the prefix. */
460 rp = route_node_get (rip->table, (struct prefix *) &p);
461
462 /* Check to see whether there is already RIP route on the table. */
463 rinfo = rp->info;
464
465 if (rinfo)
466 {
paul718e3742002-12-13 20:15:29 +0000467 /* Local static route. */
468 if (rinfo->type == ZEBRA_ROUTE_RIP
paula87552c2004-05-03 20:00:17 +0000469 && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
470 (rinfo->sub_type == RIP_ROUTE_DEFAULT))
471 && rinfo->metric != RIP_METRIC_INFINITY)
vincentfbf5d032005-09-29 11:25:50 +0000472 {
473 route_unlock_node (rp);
474 return;
475 }
476
477 /* Redistributed route check. */
478 if (rinfo->type != ZEBRA_ROUTE_RIP
479 && rinfo->metric != RIP_METRIC_INFINITY)
480 {
481 /* Fill in a minimaly temporary rip_info structure, for a future
482 rip_distance_apply() use) */
483 memset (&rinfotmp, 0, sizeof (rinfotmp));
484 IPV4_ADDR_COPY (&rinfotmp.from, &from->sin_addr);
485 rinfotmp.rp = rinfo->rp;
486 new_dist = rip_distance_apply (&rinfotmp);
487 new_dist = new_dist ? new_dist : ZEBRA_RIP_DISTANCE_DEFAULT;
488 old_dist = rinfo->distance;
vincent7a383332006-01-30 18:12:42 +0000489 /* Only connected routes may have a valid NULL distance */
490 if (rinfo->type != ZEBRA_ROUTE_CONNECT)
491 old_dist = old_dist ? old_dist : ZEBRA_RIP_DISTANCE_DEFAULT;
vincentfbf5d032005-09-29 11:25:50 +0000492 /* If imported route does not have STRICT precedence,
493 mark it as a ghost */
494 if (new_dist > old_dist
495 || rte->metric == RIP_METRIC_INFINITY)
496 {
497 route_unlock_node (rp);
498 return;
499 }
500 else
501 {
502 RIP_TIMER_OFF (rinfo->t_timeout);
503 RIP_TIMER_OFF (rinfo->t_garbage_collect);
504
505 rp->info = NULL;
506 if (rip_route_rte (rinfo))
507 rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p,
508 &rinfo->nexthop, rinfo->metric);
509 rip_info_free (rinfo);
510 rinfo = NULL;
511 route_reuse = 1;
512 }
513 }
paul718e3742002-12-13 20:15:29 +0000514 }
paula87552c2004-05-03 20:00:17 +0000515
516 if (!rinfo)
paul718e3742002-12-13 20:15:29 +0000517 {
518 /* Now, check to see whether there is already an explicit route
paula87552c2004-05-03 20:00:17 +0000519 for the destination prefix. If there is no such route, add
520 this route to the routing table, unless the metric is
521 infinity (there is no point in adding a route which
522 unusable). */
paul718e3742002-12-13 20:15:29 +0000523 if (rte->metric != RIP_METRIC_INFINITY)
paula87552c2004-05-03 20:00:17 +0000524 {
525 rinfo = rip_info_new ();
paul718e3742002-12-13 20:15:29 +0000526
paula87552c2004-05-03 20:00:17 +0000527 /* - Setting the destination prefix and length to those in
528 the RTE. */
529 rinfo->rp = rp;
paul718e3742002-12-13 20:15:29 +0000530
paula87552c2004-05-03 20:00:17 +0000531 /* - Setting the metric to the newly calculated metric (as
532 described above). */
533 rinfo->metric = rte->metric;
534 rinfo->tag = ntohs (rte->tag);
paul718e3742002-12-13 20:15:29 +0000535
paula87552c2004-05-03 20:00:17 +0000536 /* - Set the next hop address to be the address of the router
537 from which the datagram came or the next hop address
538 specified by a next hop RTE. */
539 IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
540 IPV4_ADDR_COPY (&rinfo->from, &from->sin_addr);
541 rinfo->ifindex = ifp->ifindex;
paul718e3742002-12-13 20:15:29 +0000542
paula87552c2004-05-03 20:00:17 +0000543 /* - Initialize the timeout for the route. If the
544 garbage-collection timer is running for this route, stop it
545 (see section 2.3 for a discussion of the timers). */
546 rip_timeout_update (rinfo);
paul718e3742002-12-13 20:15:29 +0000547
paula87552c2004-05-03 20:00:17 +0000548 /* - Set the route change flag. */
549 rinfo->flags |= RIP_RTF_CHANGED;
paul718e3742002-12-13 20:15:29 +0000550
paula87552c2004-05-03 20:00:17 +0000551 /* - Signal the output process to trigger an update (see section
552 2.5). */
553 rip_event (RIP_TRIGGERED_UPDATE, 0);
paul718e3742002-12-13 20:15:29 +0000554
paula87552c2004-05-03 20:00:17 +0000555 /* Finally, route goes into the kernel. */
556 rinfo->type = ZEBRA_ROUTE_RIP;
557 rinfo->sub_type = RIP_ROUTE_RTE;
paul718e3742002-12-13 20:15:29 +0000558
paula87552c2004-05-03 20:00:17 +0000559 /* Set distance value. */
560 rinfo->distance = rip_distance_apply (rinfo);
561
562 rp->info = rinfo;
563 rip_zebra_ipv4_add (&p, &rinfo->nexthop, rinfo->metric,
564 rinfo->distance);
565 rinfo->flags |= RIP_RTF_FIB;
566 }
vincentfbf5d032005-09-29 11:25:50 +0000567
568 /* Unlock temporary lock, i.e. same behaviour */
569 if (route_reuse)
570 route_unlock_node (rp);
paul718e3742002-12-13 20:15:29 +0000571 }
572 else
573 {
574 /* Route is there but we are not sure the route is RIP or not. */
575 rinfo = rp->info;
paula87552c2004-05-03 20:00:17 +0000576
paul718e3742002-12-13 20:15:29 +0000577 /* If there is an existing route, compare the next hop address
paula87552c2004-05-03 20:00:17 +0000578 to the address of the router from which the datagram came.
579 If this datagram is from the same router as the existing
580 route, reinitialize the timeout. */
hasso16705132003-05-25 14:49:19 +0000581 same = (IPV4_ADDR_SAME (&rinfo->from, &from->sin_addr)
paula87552c2004-05-03 20:00:17 +0000582 && (rinfo->ifindex == ifp->ifindex));
paul718e3742002-12-13 20:15:29 +0000583
584 if (same)
paula87552c2004-05-03 20:00:17 +0000585 rip_timeout_update (rinfo);
paul718e3742002-12-13 20:15:29 +0000586
paulb94f9db2004-05-01 20:45:38 +0000587
588 /* Fill in a minimaly temporary rip_info structure, for a future
589 rip_distance_apply() use) */
paula87552c2004-05-03 20:00:17 +0000590 memset (&rinfotmp, 0, sizeof (rinfotmp));
paulb94f9db2004-05-01 20:45:38 +0000591 IPV4_ADDR_COPY (&rinfotmp.from, &from->sin_addr);
paula87552c2004-05-03 20:00:17 +0000592 rinfotmp.rp = rinfo->rp;
paulb94f9db2004-05-01 20:45:38 +0000593
594
paul718e3742002-12-13 20:15:29 +0000595 /* Next, compare the metrics. If the datagram is from the same
paula87552c2004-05-03 20:00:17 +0000596 router as the existing route, and the new metric is different
597 than the old one; or, if the new metric is lower than the old
598 one, or if the tag has been changed; or if there is a route
599 with a lower administrave distance; or an update of the
600 distance on the actual route; do the following actions: */
601 if ((same && rinfo->metric != rte->metric)
602 || (rte->metric < rinfo->metric)
603 || ((same)
604 && (rinfo->metric == rte->metric)
605 && ntohs (rte->tag) != rinfo->tag)
606 || (rinfo->distance > rip_distance_apply (&rinfotmp))
607 || ((rinfo->distance != rip_distance_apply (rinfo)) && same))
608 {
609 /* - Adopt the route from the datagram. That is, put the
610 new metric in, and adjust the next hop address (if
611 necessary). */
612 oldmetric = rinfo->metric;
613 rinfo->metric = rte->metric;
614 rinfo->tag = ntohs (rte->tag);
615 IPV4_ADDR_COPY (&rinfo->from, &from->sin_addr);
616 rinfo->ifindex = ifp->ifindex;
617 rinfo->distance = rip_distance_apply (rinfo);
paul718e3742002-12-13 20:15:29 +0000618
paula87552c2004-05-03 20:00:17 +0000619 /* Should a new route to this network be established
620 while the garbage-collection timer is running, the
621 new route will replace the one that is about to be
622 deleted. In this case the garbage-collection timer
623 must be cleared. */
paul718e3742002-12-13 20:15:29 +0000624
paula87552c2004-05-03 20:00:17 +0000625 if (oldmetric == RIP_METRIC_INFINITY &&
626 rinfo->metric < RIP_METRIC_INFINITY)
627 {
628 rinfo->type = ZEBRA_ROUTE_RIP;
629 rinfo->sub_type = RIP_ROUTE_RTE;
paul718e3742002-12-13 20:15:29 +0000630
paula87552c2004-05-03 20:00:17 +0000631 RIP_TIMER_OFF (rinfo->t_garbage_collect);
paul718e3742002-12-13 20:15:29 +0000632
paula87552c2004-05-03 20:00:17 +0000633 if (!IPV4_ADDR_SAME (&rinfo->nexthop, nexthop))
634 IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
paul718e3742002-12-13 20:15:29 +0000635
paula87552c2004-05-03 20:00:17 +0000636 rip_zebra_ipv4_add (&p, nexthop, rinfo->metric,
637 rinfo->distance);
638 rinfo->flags |= RIP_RTF_FIB;
639 }
paul718e3742002-12-13 20:15:29 +0000640
paula87552c2004-05-03 20:00:17 +0000641 /* Update nexthop and/or metric value. */
642 if (oldmetric != RIP_METRIC_INFINITY)
643 {
644 rip_zebra_ipv4_delete (&p, &rinfo->nexthop, oldmetric);
645 rip_zebra_ipv4_add (&p, nexthop, rinfo->metric,
646 rinfo->distance);
647 rinfo->flags |= RIP_RTF_FIB;
paul718e3742002-12-13 20:15:29 +0000648
paula87552c2004-05-03 20:00:17 +0000649 if (!IPV4_ADDR_SAME (&rinfo->nexthop, nexthop))
650 IPV4_ADDR_COPY (&rinfo->nexthop, nexthop);
651 }
paul718e3742002-12-13 20:15:29 +0000652
paula87552c2004-05-03 20:00:17 +0000653 /* - Set the route change flag and signal the output process
654 to trigger an update. */
655 rinfo->flags |= RIP_RTF_CHANGED;
656 rip_event (RIP_TRIGGERED_UPDATE, 0);
paul718e3742002-12-13 20:15:29 +0000657
paula87552c2004-05-03 20:00:17 +0000658 /* - If the new metric is infinity, start the deletion
659 process (described above); */
660 if (rinfo->metric == RIP_METRIC_INFINITY)
661 {
662 /* If the new metric is infinity, the deletion process
663 begins for the route, which is no longer used for
664 routing packets. Note that the deletion process is
665 started only when the metric is first set to
666 infinity. If the metric was already infinity, then a
667 new deletion process is not started. */
668 if (oldmetric != RIP_METRIC_INFINITY)
669 {
670 /* - The garbage-collection timer is set for 120 seconds. */
671 RIP_TIMER_ON (rinfo->t_garbage_collect,
672 rip_garbage_collect, rip->garbage_time);
673 RIP_TIMER_OFF (rinfo->t_timeout);
paul718e3742002-12-13 20:15:29 +0000674
paula87552c2004-05-03 20:00:17 +0000675 /* - The metric for the route is set to 16
676 (infinity). This causes the route to be removed
677 from service. */
678 rip_zebra_ipv4_delete (&p, &rinfo->nexthop, oldmetric);
679 rinfo->flags &= ~RIP_RTF_FIB;
paul718e3742002-12-13 20:15:29 +0000680
paula87552c2004-05-03 20:00:17 +0000681 /* - The route change flag is to indicate that this
682 entry has been changed. */
683 /* - The output process is signalled to trigger a
paul718e3742002-12-13 20:15:29 +0000684 response. */
paula87552c2004-05-03 20:00:17 +0000685 ; /* Above processes are already done previously. */
686 }
687 }
688 else
689 {
690 /* otherwise, re-initialize the timeout. */
691 rip_timeout_update (rinfo);
692 }
693 }
paul718e3742002-12-13 20:15:29 +0000694 /* Unlock tempolary lock of the route. */
695 route_unlock_node (rp);
696 }
697}
698
699/* Dump RIP packet */
pauldc63bfd2005-10-25 23:31:05 +0000700static void
hasso8a676be2004-10-08 06:36:38 +0000701rip_packet_dump (struct rip_packet *packet, int size, const char *sndrcv)
paul718e3742002-12-13 20:15:29 +0000702{
703 caddr_t lim;
704 struct rte *rte;
hasso8a676be2004-10-08 06:36:38 +0000705 const char *command_str;
paul718e3742002-12-13 20:15:29 +0000706 char pbuf[BUFSIZ], nbuf[BUFSIZ];
707 u_char netmask = 0;
708 u_char *p;
709
710 /* Set command string. */
711 if (packet->command > 0 && packet->command < RIP_COMMAND_MAX)
712 command_str = lookup (rip_msg, packet->command);
713 else
714 command_str = "unknown";
715
716 /* Dump packet header. */
ajs5d6c3772004-12-08 19:24:06 +0000717 zlog_debug ("%s %s version %d packet size %d",
paul718e3742002-12-13 20:15:29 +0000718 sndrcv, command_str, packet->version, size);
719
720 /* Dump each routing table entry. */
721 rte = packet->rte;
722
723 for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
724 {
725 if (packet->version == RIPv2)
726 {
727 netmask = ip_masklen (rte->mask);
728
paulca5e5162004-06-06 22:06:33 +0000729 if (rte->family == htons (RIP_FAMILY_AUTH))
paul718e3742002-12-13 20:15:29 +0000730 {
paulca5e5162004-06-06 22:06:33 +0000731 if (rte->tag == htons (RIP_AUTH_SIMPLE_PASSWORD))
paul718e3742002-12-13 20:15:29 +0000732 {
733 p = (u_char *)&rte->prefix;
734
ajs5d6c3772004-12-08 19:24:06 +0000735 zlog_debug (" family 0x%X type %d auth string: %s",
paul718e3742002-12-13 20:15:29 +0000736 ntohs (rte->family), ntohs (rte->tag), p);
737 }
paulca5e5162004-06-06 22:06:33 +0000738 else if (rte->tag == htons (RIP_AUTH_MD5))
paul718e3742002-12-13 20:15:29 +0000739 {
740 struct rip_md5_info *md5;
741
742 md5 = (struct rip_md5_info *) &packet->rte;
743
ajs5d6c3772004-12-08 19:24:06 +0000744 zlog_debug (" family 0x%X type %d (MD5 authentication)",
paul718e3742002-12-13 20:15:29 +0000745 ntohs (md5->family), ntohs (md5->type));
ajs5d6c3772004-12-08 19:24:06 +0000746 zlog_debug (" RIP-2 packet len %d Key ID %d"
paulca5e5162004-06-06 22:06:33 +0000747 " Auth Data len %d",
748 ntohs (md5->packet_len), md5->keyid,
749 md5->auth_len);
ajs5d6c3772004-12-08 19:24:06 +0000750 zlog_debug (" Sequence Number %ld",
paulca5e5162004-06-06 22:06:33 +0000751 (u_long) ntohl (md5->sequence));
paul718e3742002-12-13 20:15:29 +0000752 }
paulca5e5162004-06-06 22:06:33 +0000753 else if (rte->tag == htons (RIP_AUTH_DATA))
paul718e3742002-12-13 20:15:29 +0000754 {
755 p = (u_char *)&rte->prefix;
756
ajs5d6c3772004-12-08 19:24:06 +0000757 zlog_debug (" family 0x%X type %d (MD5 data)",
paul718e3742002-12-13 20:15:29 +0000758 ntohs (rte->family), ntohs (rte->tag));
ajs5d6c3772004-12-08 19:24:06 +0000759 zlog_debug (" MD5: %02X%02X%02X%02X%02X%02X%02X%02X"
paul718e3742002-12-13 20:15:29 +0000760 "%02X%02X%02X%02X%02X%02X%02X",
paulca5e5162004-06-06 22:06:33 +0000761 p[0], p[1], p[2], p[3], p[4], p[5], p[6],
762 p[7], p[9], p[10], p[11], p[12], p[13],
763 p[14], p[15]);
paul718e3742002-12-13 20:15:29 +0000764 }
765 else
766 {
ajs5d6c3772004-12-08 19:24:06 +0000767 zlog_debug (" family 0x%X type %d (Unknown auth type)",
paul718e3742002-12-13 20:15:29 +0000768 ntohs (rte->family), ntohs (rte->tag));
769 }
770 }
771 else
ajs5d6c3772004-12-08 19:24:06 +0000772 zlog_debug (" %s/%d -> %s family %d tag %d metric %ld",
paulca5e5162004-06-06 22:06:33 +0000773 inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
774 netmask, inet_ntop (AF_INET, &rte->nexthop, nbuf,
775 BUFSIZ), ntohs (rte->family),
776 ntohs (rte->tag), (u_long) ntohl (rte->metric));
paul718e3742002-12-13 20:15:29 +0000777 }
778 else
779 {
ajs5d6c3772004-12-08 19:24:06 +0000780 zlog_debug (" %s family %d tag %d metric %ld",
paul718e3742002-12-13 20:15:29 +0000781 inet_ntop (AF_INET, &rte->prefix, pbuf, BUFSIZ),
782 ntohs (rte->family), ntohs (rte->tag),
783 (u_long)ntohl (rte->metric));
784 }
785 }
786}
787
788/* Check if the destination address is valid (unicast; not net 0
789 or 127) (RFC2453 Section 3.9.2 - Page 26). But we don't
790 check net 0 because we accept default route. */
pauldc63bfd2005-10-25 23:31:05 +0000791static int
paul718e3742002-12-13 20:15:29 +0000792rip_destination_check (struct in_addr addr)
793{
794 u_int32_t destination;
795
796 /* Convert to host byte order. */
797 destination = ntohl (addr.s_addr);
798
799 if (IPV4_NET127 (destination))
800 return 0;
801
802 /* Net 0 may match to the default route. */
803 if (IPV4_NET0 (destination) && destination != 0)
804 return 0;
805
806 /* Unicast address must belong to class A, B, C. */
807 if (IN_CLASSA (destination))
808 return 1;
809 if (IN_CLASSB (destination))
810 return 1;
811 if (IN_CLASSC (destination))
812 return 1;
813
814 return 0;
815}
816
817/* RIP version 2 authentication. */
pauldc63bfd2005-10-25 23:31:05 +0000818static int
paul718e3742002-12-13 20:15:29 +0000819rip_auth_simple_password (struct rte *rte, struct sockaddr_in *from,
820 struct interface *ifp)
821{
822 struct rip_interface *ri;
823 char *auth_str;
824
825 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +0000826 zlog_debug ("RIPv2 simple password authentication from %s",
paul718e3742002-12-13 20:15:29 +0000827 inet_ntoa (from->sin_addr));
828
829 ri = ifp->info;
830
831 if (ri->auth_type != RIP_AUTH_SIMPLE_PASSWORD
paulca5e5162004-06-06 22:06:33 +0000832 || rte->tag != htons(RIP_AUTH_SIMPLE_PASSWORD))
paul718e3742002-12-13 20:15:29 +0000833 return 0;
834
835 /* Simple password authentication. */
836 if (ri->auth_str)
837 {
838 auth_str = (char *) &rte->prefix;
839
840 if (strncmp (auth_str, ri->auth_str, 16) == 0)
841 return 1;
842 }
843 if (ri->key_chain)
844 {
845 struct keychain *keychain;
846 struct key *key;
847
848 keychain = keychain_lookup (ri->key_chain);
849 if (keychain == NULL)
850 return 0;
851
852 key = key_match_for_accept (keychain, (char *) &rte->prefix);
853 if (key)
854 return 1;
855 }
856 return 0;
857}
858
859/* RIP version 2 authentication with MD5. */
pauldc63bfd2005-10-25 23:31:05 +0000860static int
paul718e3742002-12-13 20:15:29 +0000861rip_auth_md5 (struct rip_packet *packet, struct sockaddr_in *from,
paulca5e5162004-06-06 22:06:33 +0000862 int length, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000863{
864 struct rip_interface *ri;
865 struct rip_md5_info *md5;
866 struct rip_md5_data *md5data;
867 struct keychain *keychain;
868 struct key *key;
vincentc1a03d42005-09-28 15:47:44 +0000869 MD5_CTX ctx;
paul718e3742002-12-13 20:15:29 +0000870 u_char digest[RIP_AUTH_MD5_SIZE];
871 u_int16_t packet_len;
paul98fd1e62006-01-17 17:26:25 +0000872 char auth_str[RIP_AUTH_MD5_SIZE];
paul718e3742002-12-13 20:15:29 +0000873
874 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +0000875 zlog_debug ("RIPv2 MD5 authentication from %s",
paulca5e5162004-06-06 22:06:33 +0000876 inet_ntoa (from->sin_addr));
paul718e3742002-12-13 20:15:29 +0000877
878 ri = ifp->info;
879 md5 = (struct rip_md5_info *) &packet->rte;
880
881 /* Check auth type. */
paulca5e5162004-06-06 22:06:33 +0000882 if (ri->auth_type != RIP_AUTH_MD5 || md5->type != htons(RIP_AUTH_MD5))
paul718e3742002-12-13 20:15:29 +0000883 return 0;
884
paulca5e5162004-06-06 22:06:33 +0000885 /* If the authentication length is less than 16, then it must be wrong for
886 * any interpretation of rfc2082. Some implementations also interpret
887 * this as RIP_HEADER_SIZE+ RIP_AUTH_MD5_SIZE, aka RIP_AUTH_MD5_COMPAT_SIZE.
paul98fd1e62006-01-17 17:26:25 +0000888 */
paulca5e5162004-06-06 22:06:33 +0000889 if ( !((md5->auth_len == RIP_AUTH_MD5_SIZE)
890 || (md5->auth_len == RIP_AUTH_MD5_COMPAT_SIZE)))
paulc2bfbcc2004-06-04 01:42:38 +0000891 {
892 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +0000893 zlog_debug ("RIPv2 MD5 authentication, strange authentication "
paulca5e5162004-06-06 22:06:33 +0000894 "length field %d", md5->auth_len);
paul718e3742002-12-13 20:15:29 +0000895 return 0;
paulc2bfbcc2004-06-04 01:42:38 +0000896 }
paul718e3742002-12-13 20:15:29 +0000897
paulca5e5162004-06-06 22:06:33 +0000898 /* grab and verify check packet length */
899 packet_len = ntohs (md5->packet_len);
900
901 if (packet_len > (length - RIP_HEADER_SIZE - RIP_AUTH_MD5_SIZE))
902 {
903 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +0000904 zlog_debug ("RIPv2 MD5 authentication, packet length field %d "
paulca5e5162004-06-06 22:06:33 +0000905 "greater than received length %d!",
906 md5->packet_len, length);
907 return 0;
908 }
909
910 /* retrieve authentication data */
911 md5data = (struct rip_md5_data *) (((u_char *) packet) + packet_len);
paul98fd1e62006-01-17 17:26:25 +0000912
913 memset (auth_str, 0, RIP_AUTH_MD5_SIZE);
paulca5e5162004-06-06 22:06:33 +0000914
paul718e3742002-12-13 20:15:29 +0000915 if (ri->key_chain)
916 {
917 keychain = keychain_lookup (ri->key_chain);
918 if (keychain == NULL)
919 return 0;
920
921 key = key_lookup_for_accept (keychain, md5->keyid);
922 if (key == NULL)
923 return 0;
924
paul98fd1e62006-01-17 17:26:25 +0000925 strncpy (auth_str, key->string, RIP_AUTH_MD5_SIZE);
paul718e3742002-12-13 20:15:29 +0000926 }
paul98fd1e62006-01-17 17:26:25 +0000927 else if (ri->auth_str)
928 strncpy (auth_str, ri->auth_str, RIP_AUTH_MD5_SIZE);
paul718e3742002-12-13 20:15:29 +0000929
930 if (! auth_str)
931 return 0;
paul98fd1e62006-01-17 17:26:25 +0000932
paul718e3742002-12-13 20:15:29 +0000933 /* MD5 digest authentication. */
vincentc1a03d42005-09-28 15:47:44 +0000934 memset (&ctx, 0, sizeof(ctx));
935 MD5Init(&ctx);
paul98fd1e62006-01-17 17:26:25 +0000936 MD5Update(&ctx, packet, packet_len + RIP_HEADER_SIZE);
937 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
vincentc1a03d42005-09-28 15:47:44 +0000938 MD5Final(digest, &ctx);
paul98fd1e62006-01-17 17:26:25 +0000939
940 if (memcmp (md5data->digest, digest, RIP_AUTH_MD5_SIZE) == 0)
paul718e3742002-12-13 20:15:29 +0000941 return packet_len;
942 else
943 return 0;
944}
945
paulb14ee002005-02-04 23:42:41 +0000946/* Pick correct auth string for sends, prepare auth_str buffer for use.
947 * (left justified and padded).
948 *
949 * presumes one of ri or key is valid, and that the auth strings they point
950 * to are nul terminated. If neither are present, auth_str will be fully
951 * zero padded.
952 *
953 */
954static void
955rip_auth_prepare_str_send (struct rip_interface *ri, struct key *key,
956 char *auth_str, int len)
paul718e3742002-12-13 20:15:29 +0000957{
paulb14ee002005-02-04 23:42:41 +0000958 assert (ri || key);
paul718e3742002-12-13 20:15:29 +0000959
paulb14ee002005-02-04 23:42:41 +0000960 memset (auth_str, 0, len);
961 if (key && key->string)
962 strncpy (auth_str, key->string, len);
963 else if (ri->auth_str)
964 strncpy (auth_str, ri->auth_str, len);
paul718e3742002-12-13 20:15:29 +0000965
paulb14ee002005-02-04 23:42:41 +0000966 return;
967}
paul718e3742002-12-13 20:15:29 +0000968
paulb14ee002005-02-04 23:42:41 +0000969/* Write RIPv2 simple password authentication information
970 *
971 * auth_str is presumed to be 2 bytes and correctly prepared
972 * (left justified and zero padded).
973 */
974static void
975rip_auth_simple_write (struct stream *s, char *auth_str, int len)
976{
977 assert (s && len == RIP_AUTH_SIMPLE_SIZE);
paul718e3742002-12-13 20:15:29 +0000978
paulb14ee002005-02-04 23:42:41 +0000979 stream_putw (s, RIP_FAMILY_AUTH);
980 stream_putw (s, RIP_AUTH_SIMPLE_PASSWORD);
981 stream_put (s, auth_str, RIP_AUTH_SIMPLE_SIZE);
982
983 return;
984}
985
986/* write RIPv2 MD5 "authentication header"
987 * (uses the auth key data field)
988 *
989 * Digest offset field is set to 0.
990 *
991 * returns: offset of the digest offset field, which must be set when
992 * length to the auth-data MD5 digest is known.
993 */
994static size_t
995rip_auth_md5_ah_write (struct stream *s, struct rip_interface *ri,
996 struct key *key)
997{
paul98fd1e62006-01-17 17:26:25 +0000998 size_t doff = 0;
paulb14ee002005-02-04 23:42:41 +0000999
1000 assert (s && ri && ri->auth_type == RIP_AUTH_MD5);
paul718e3742002-12-13 20:15:29 +00001001
1002 /* MD5 authentication. */
paulca5e5162004-06-06 22:06:33 +00001003 stream_putw (s, RIP_FAMILY_AUTH);
paul718e3742002-12-13 20:15:29 +00001004 stream_putw (s, RIP_AUTH_MD5);
1005
paulb14ee002005-02-04 23:42:41 +00001006 /* MD5 AH digest offset field.
1007 *
1008 * Set to placeholder value here, to true value when RIP-2 Packet length
1009 * is known. Actual value is set in .....().
1010 */
paul98fd1e62006-01-17 17:26:25 +00001011 doff = stream_get_endp(s);
paulb14ee002005-02-04 23:42:41 +00001012 stream_putw (s, 0);
paul718e3742002-12-13 20:15:29 +00001013
1014 /* Key ID. */
1015 if (key)
1016 stream_putc (s, key->index % 256);
1017 else
1018 stream_putc (s, 1);
1019
paulca5e5162004-06-06 22:06:33 +00001020 /* Auth Data Len. Set 16 for MD5 authentication data. Older ripds
1021 * however expect RIP_HEADER_SIZE + RIP_AUTH_MD5_SIZE so we allow for this
1022 * to be configurable.
1023 */
1024 stream_putc (s, ri->md5_auth_len);
paul718e3742002-12-13 20:15:29 +00001025
1026 /* Sequence Number (non-decreasing). */
1027 /* RFC2080: The value used in the sequence number is
1028 arbitrary, but two suggestions are the time of the
1029 message's creation or a simple message counter. */
1030 stream_putl (s, time (NULL));
1031
1032 /* Reserved field must be zero. */
1033 stream_putl (s, 0);
1034 stream_putl (s, 0);
1035
paul98fd1e62006-01-17 17:26:25 +00001036 return doff;
paulb14ee002005-02-04 23:42:41 +00001037}
paul718e3742002-12-13 20:15:29 +00001038
paulb14ee002005-02-04 23:42:41 +00001039/* If authentication is in used, write the appropriate header
1040 * returns stream offset to which length must later be written
1041 * or 0 if this is not required
1042 */
1043static size_t
1044rip_auth_header_write (struct stream *s, struct rip_interface *ri,
1045 struct key *key, char *auth_str, int len)
1046{
1047 assert (ri->auth_type != RIP_NO_AUTH);
1048
1049 switch (ri->auth_type)
1050 {
1051 case RIP_AUTH_SIMPLE_PASSWORD:
1052 rip_auth_prepare_str_send (ri, key, auth_str, len);
1053 rip_auth_simple_write (s, auth_str, len);
1054 return 0;
1055 case RIP_AUTH_MD5:
1056 return rip_auth_md5_ah_write (s, ri, key);
1057 }
1058 assert (1);
paul98fd1e62006-01-17 17:26:25 +00001059 return 0;
paulb14ee002005-02-04 23:42:41 +00001060}
1061
1062/* Write RIPv2 MD5 authentication data trailer */
1063static void
1064rip_auth_md5_set (struct stream *s, struct rip_interface *ri, size_t doff,
1065 char *auth_str, int authlen)
1066{
1067 unsigned long len;
vincentc1a03d42005-09-28 15:47:44 +00001068 MD5_CTX ctx;
paulb14ee002005-02-04 23:42:41 +00001069 unsigned char digest[RIP_AUTH_MD5_SIZE];
1070
1071 /* Make it sure this interface is configured as MD5
1072 authentication. */
1073 assert ((ri->auth_type == RIP_AUTH_MD5) && (authlen == RIP_AUTH_MD5_SIZE));
1074 assert (doff > 0);
1075
1076 /* Get packet length. */
1077 len = stream_get_endp(s);
1078
1079 /* Check packet length. */
1080 if (len < (RIP_HEADER_SIZE + RIP_RTE_SIZE))
1081 {
1082 zlog_err ("rip_auth_md5_set(): packet length %ld is less than minimum length.", len);
1083 return;
1084 }
1085
1086 /* Set the digest offset length in the header */
1087 stream_putw_at (s, doff, len);
1088
paul718e3742002-12-13 20:15:29 +00001089 /* Set authentication data. */
paulca5e5162004-06-06 22:06:33 +00001090 stream_putw (s, RIP_FAMILY_AUTH);
1091 stream_putw (s, RIP_AUTH_DATA);
paul718e3742002-12-13 20:15:29 +00001092
1093 /* Generate a digest for the RIP packet. */
vincentc1a03d42005-09-28 15:47:44 +00001094 memset(&ctx, 0, sizeof(ctx));
1095 MD5Init(&ctx);
paul98fd1e62006-01-17 17:26:25 +00001096 MD5Update(&ctx, STREAM_DATA (s), stream_get_endp (s));
vincentc1a03d42005-09-28 15:47:44 +00001097 MD5Update(&ctx, auth_str, RIP_AUTH_MD5_SIZE);
1098 MD5Final(digest, &ctx);
paul718e3742002-12-13 20:15:29 +00001099
1100 /* Copy the digest to the packet. */
1101 stream_write (s, digest, RIP_AUTH_MD5_SIZE);
1102}
1103
1104/* RIP routing information. */
pauldc63bfd2005-10-25 23:31:05 +00001105static void
paul718e3742002-12-13 20:15:29 +00001106rip_response_process (struct rip_packet *packet, int size,
paulc49ad8f2004-10-22 10:27:28 +00001107 struct sockaddr_in *from, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001108{
1109 caddr_t lim;
1110 struct rte *rte;
paul727d1042002-12-13 20:50:29 +00001111 struct prefix_ipv4 ifaddr;
1112 struct prefix_ipv4 ifaddrclass;
paul727d1042002-12-13 20:50:29 +00001113 int subnetted;
paul718e3742002-12-13 20:15:29 +00001114
paul727d1042002-12-13 20:50:29 +00001115 /* We don't know yet. */
1116 subnetted = -1;
1117
paul718e3742002-12-13 20:15:29 +00001118 /* The Response must be ignored if it is not from the RIP
1119 port. (RFC2453 - Sec. 3.9.2)*/
paulca5e5162004-06-06 22:06:33 +00001120 if (from->sin_port != htons(RIP_PORT_DEFAULT))
paul718e3742002-12-13 20:15:29 +00001121 {
1122 zlog_info ("response doesn't come from RIP port: %d",
1123 from->sin_port);
1124 rip_peer_bad_packet (from);
1125 return;
1126 }
1127
1128 /* The datagram's IPv4 source address should be checked to see
1129 whether the datagram is from a valid neighbor; the source of the
ajs35a60c22005-10-30 23:51:32 +00001130 datagram must be on a directly connected network (RFC2453 - Sec. 3.9.2) */
1131 if (if_lookup_address(from->sin_addr) == NULL)
paul718e3742002-12-13 20:15:29 +00001132 {
1133 zlog_info ("This datagram doesn't came from a valid neighbor: %s",
1134 inet_ntoa (from->sin_addr));
1135 rip_peer_bad_packet (from);
1136 return;
1137 }
1138
1139 /* It is also worth checking to see whether the response is from one
1140 of the router's own addresses. */
1141
1142 ; /* Alredy done in rip_read () */
1143
1144 /* Update RIP peer. */
1145 rip_peer_update (from, packet->version);
1146
1147 /* Set RTE pointer. */
1148 rte = packet->rte;
1149
1150 for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++)
1151 {
1152 /* RIPv2 authentication check. */
1153 /* If the Address Family Identifier of the first (and only the
1154 first) entry in the message is 0xFFFF, then the remainder of
1155 the entry contains the authentication. */
1156 /* If the packet gets here it means authentication enabled */
1157 /* Check is done in rip_read(). So, just skipping it */
1158 if (packet->version == RIPv2 &&
1159 rte == packet->rte &&
paulca5e5162004-06-06 22:06:33 +00001160 rte->family == htons(RIP_FAMILY_AUTH))
paul718e3742002-12-13 20:15:29 +00001161 continue;
1162
paulca5e5162004-06-06 22:06:33 +00001163 if (rte->family != htons(AF_INET))
paul718e3742002-12-13 20:15:29 +00001164 {
1165 /* Address family check. RIP only supports AF_INET. */
1166 zlog_info ("Unsupported family %d from %s.",
1167 ntohs (rte->family), inet_ntoa (from->sin_addr));
1168 continue;
1169 }
1170
1171 /* - is the destination address valid (e.g., unicast; not net 0
1172 or 127) */
1173 if (! rip_destination_check (rte->prefix))
1174 {
1175 zlog_info ("Network is net 0 or net 127 or it is not unicast network");
1176 rip_peer_bad_route (from);
1177 continue;
1178 }
1179
1180 /* Convert metric value to host byte order. */
1181 rte->metric = ntohl (rte->metric);
1182
1183 /* - is the metric valid (i.e., between 1 and 16, inclusive) */
1184 if (! (rte->metric >= 1 && rte->metric <= 16))
1185 {
1186 zlog_info ("Route's metric is not in the 1-16 range.");
1187 rip_peer_bad_route (from);
1188 continue;
1189 }
1190
1191 /* RIPv1 does not have nexthop value. */
1192 if (packet->version == RIPv1 && rte->nexthop.s_addr != 0)
1193 {
1194 zlog_info ("RIPv1 packet with nexthop value %s",
1195 inet_ntoa (rte->nexthop));
1196 rip_peer_bad_route (from);
1197 continue;
1198 }
1199
1200 /* That is, if the provided information is ignored, a possibly
1201 sub-optimal, but absolutely valid, route may be taken. If
1202 the received Next Hop is not directly reachable, it should be
1203 treated as 0.0.0.0. */
1204 if (packet->version == RIPv2 && rte->nexthop.s_addr != 0)
1205 {
1206 u_int32_t addrval;
1207
1208 /* Multicast address check. */
1209 addrval = ntohl (rte->nexthop.s_addr);
1210 if (IN_CLASSD (addrval))
1211 {
1212 zlog_info ("Nexthop %s is multicast address, skip this rte",
1213 inet_ntoa (rte->nexthop));
1214 continue;
1215 }
1216
1217 if (! if_lookup_address (rte->nexthop))
1218 {
1219 struct route_node *rn;
1220 struct rip_info *rinfo;
1221
1222 rn = route_node_match_ipv4 (rip->table, &rte->nexthop);
1223
1224 if (rn)
1225 {
1226 rinfo = rn->info;
1227
1228 if (rinfo->type == ZEBRA_ROUTE_RIP
1229 && rinfo->sub_type == RIP_ROUTE_RTE)
1230 {
1231 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001232 zlog_debug ("Next hop %s is on RIP network. Set nexthop to the packet's originator", inet_ntoa (rte->nexthop));
paul718e3742002-12-13 20:15:29 +00001233 rte->nexthop = rinfo->from;
1234 }
1235 else
1236 {
1237 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001238 zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
paul718e3742002-12-13 20:15:29 +00001239 rte->nexthop.s_addr = 0;
1240 }
1241
1242 route_unlock_node (rn);
1243 }
1244 else
1245 {
1246 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001247 zlog_debug ("Next hop %s is not directly reachable. Treat it as 0.0.0.0", inet_ntoa (rte->nexthop));
paul718e3742002-12-13 20:15:29 +00001248 rte->nexthop.s_addr = 0;
1249 }
1250
1251 }
1252 }
1253
1254 /* For RIPv1, there won't be a valid netmask.
1255
1256 This is a best guess at the masks. If everyone was using old
1257 Ciscos before the 'ip subnet zero' option, it would be almost
1258 right too :-)
1259
1260 Cisco summarize ripv1 advertisments to the classful boundary
1261 (/16 for class B's) except when the RIP packet does to inside
1262 the classful network in question. */
1263
1264 if ((packet->version == RIPv1 && rte->prefix.s_addr != 0)
1265 || (packet->version == RIPv2
1266 && (rte->prefix.s_addr != 0 && rte->mask.s_addr == 0)))
1267 {
1268 u_int32_t destination;
1269
paul727d1042002-12-13 20:50:29 +00001270 if (subnetted == -1)
paulc49ad8f2004-10-22 10:27:28 +00001271 {
1272 memcpy (&ifaddr, ifc->address, sizeof (struct prefix_ipv4));
1273 memcpy (&ifaddrclass, &ifaddr, sizeof (struct prefix_ipv4));
1274 apply_classful_mask_ipv4 (&ifaddrclass);
1275 subnetted = 0;
1276 if (ifaddr.prefixlen > ifaddrclass.prefixlen)
1277 subnetted = 1;
1278 }
paul727d1042002-12-13 20:50:29 +00001279
paul718e3742002-12-13 20:15:29 +00001280 destination = ntohl (rte->prefix.s_addr);
1281
paul727d1042002-12-13 20:50:29 +00001282 if (IN_CLASSA (destination))
paul718e3742002-12-13 20:15:29 +00001283 masklen2ip (8, &rte->mask);
paul727d1042002-12-13 20:50:29 +00001284 else if (IN_CLASSB (destination))
1285 masklen2ip (16, &rte->mask);
1286 else if (IN_CLASSC (destination))
1287 masklen2ip (24, &rte->mask);
1288
1289 if (subnetted == 1)
1290 masklen2ip (ifaddrclass.prefixlen,
1291 (struct in_addr *) &destination);
1292 if ((subnetted == 1) && ((rte->prefix.s_addr & destination) ==
1293 ifaddrclass.prefix.s_addr))
1294 {
1295 masklen2ip (ifaddr.prefixlen, &rte->mask);
1296 if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
1297 masklen2ip (32, &rte->mask);
1298 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001299 zlog_debug ("Subnetted route %s", inet_ntoa (rte->prefix));
paul727d1042002-12-13 20:50:29 +00001300 }
1301 else
1302 {
1303 if ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr)
1304 continue;
1305 }
1306
1307 if (IS_RIP_DEBUG_EVENT)
1308 {
ajs5d6c3772004-12-08 19:24:06 +00001309 zlog_debug ("Resultant route %s", inet_ntoa (rte->prefix));
1310 zlog_debug ("Resultant mask %s", inet_ntoa (rte->mask));
paul718e3742002-12-13 20:15:29 +00001311 }
1312 }
1313
1314 /* In case of RIPv2, if prefix in RTE is not netmask applied one
1315 ignore the entry. */
1316 if ((packet->version == RIPv2)
1317 && (rte->mask.s_addr != 0)
1318 && ((rte->prefix.s_addr & rte->mask.s_addr) != rte->prefix.s_addr))
1319 {
1320 zlog_warn ("RIPv2 address %s is not mask /%d applied one",
1321 inet_ntoa (rte->prefix), ip_masklen (rte->mask));
1322 rip_peer_bad_route (from);
1323 continue;
1324 }
1325
1326 /* Default route's netmask is ignored. */
1327 if (packet->version == RIPv2
1328 && (rte->prefix.s_addr == 0)
1329 && (rte->mask.s_addr != 0))
1330 {
1331 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001332 zlog_debug ("Default route with non-zero netmask. Set zero to netmask");
paul718e3742002-12-13 20:15:29 +00001333 rte->mask.s_addr = 0;
1334 }
1335
1336 /* Routing table updates. */
paulc49ad8f2004-10-22 10:27:28 +00001337 rip_rte_process (rte, from, ifc->ifp);
paul718e3742002-12-13 20:15:29 +00001338 }
1339}
1340
paula4e987e2005-06-03 17:46:49 +00001341/* Make socket for RIP protocol. */
paulf69bd9d2005-06-03 18:01:50 +00001342static int
paul2c61ae32005-08-16 15:22:14 +00001343rip_create_socket (struct sockaddr_in *from)
paula4e987e2005-06-03 17:46:49 +00001344{
1345 int ret;
1346 int sock;
1347 struct sockaddr_in addr;
paulf69bd9d2005-06-03 18:01:50 +00001348
paul2c61ae32005-08-16 15:22:14 +00001349 memset (&addr, 0, sizeof (struct sockaddr_in));
1350
1351 if (!from)
paulf69bd9d2005-06-03 18:01:50 +00001352 {
paulf69bd9d2005-06-03 18:01:50 +00001353 addr.sin_family = AF_INET;
1354 addr.sin_addr.s_addr = INADDR_ANY;
paul2c61ae32005-08-16 15:22:14 +00001355#ifdef HAVE_SINLEN
1356 addr.sin_len = sizeof (struct sockaddr_in);
1357#endif /* HAVE_SINLEN */
jardin38d3c162005-10-19 19:29:59 +00001358 } else {
1359 memcpy(&addr, from, sizeof(addr));
paulf69bd9d2005-06-03 18:01:50 +00001360 }
1361
paul2c61ae32005-08-16 15:22:14 +00001362 /* sending port must always be the RIP port */
1363 addr.sin_port = htons (RIP_PORT_DEFAULT);
1364
paula4e987e2005-06-03 17:46:49 +00001365 /* Make datagram socket. */
1366 sock = socket (AF_INET, SOCK_DGRAM, 0);
1367 if (sock < 0)
1368 {
1369 zlog_err("Cannot create UDP socket: %s", safe_strerror(errno));
1370 exit (1);
1371 }
1372
1373 sockopt_broadcast (sock);
1374 sockopt_reuseaddr (sock);
1375 sockopt_reuseport (sock);
paula4e987e2005-06-03 17:46:49 +00001376#ifdef RIP_RECVMSG
1377 setsockopt_pktinfo (sock);
1378#endif /* RIP_RECVMSG */
1379
1380 if (ripd_privs.change (ZPRIVS_RAISE))
1381 zlog_err ("rip_create_socket: could not raise privs");
paulf69bd9d2005-06-03 18:01:50 +00001382 setsockopt_so_recvbuf (sock, RIP_UDP_RCV_BUF);
1383 if ( (ret = bind (sock, (struct sockaddr *) & addr, sizeof (addr))) < 0)
1384
paula4e987e2005-06-03 17:46:49 +00001385 {
1386 int save_errno = errno;
1387 if (ripd_privs.change (ZPRIVS_LOWER))
1388 zlog_err ("rip_create_socket: could not lower privs");
paul2c61ae32005-08-16 15:22:14 +00001389
1390 zlog_err("%s: Can't bind socket %d to %s port %d: %s", __func__,
1391 sock, inet_ntoa(addr.sin_addr),
1392 (int) ntohs(addr.sin_port),
1393 safe_strerror(save_errno));
1394
paulf69bd9d2005-06-03 18:01:50 +00001395 close (sock);
paula4e987e2005-06-03 17:46:49 +00001396 return ret;
1397 }
paulf69bd9d2005-06-03 18:01:50 +00001398
paula4e987e2005-06-03 17:46:49 +00001399 if (ripd_privs.change (ZPRIVS_LOWER))
1400 zlog_err ("rip_create_socket: could not lower privs");
1401
1402 return sock;
1403}
1404
paulc49ad8f2004-10-22 10:27:28 +00001405/* RIP packet send to destination address, on interface denoted by
1406 * by connected argument. NULL to argument denotes destination should be
1407 * should be RIP multicast group
1408 */
pauldc63bfd2005-10-25 23:31:05 +00001409static int
paulc49ad8f2004-10-22 10:27:28 +00001410rip_send_packet (u_char * buf, int size, struct sockaddr_in *to,
1411 struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001412{
paul931cd542004-01-23 15:31:42 +00001413 int ret, send_sock;
paul718e3742002-12-13 20:15:29 +00001414 struct sockaddr_in sin;
paulc49ad8f2004-10-22 10:27:28 +00001415
1416 assert (ifc != NULL);
1417
paul931cd542004-01-23 15:31:42 +00001418 if (IS_RIP_DEBUG_PACKET)
1419 {
paulf69bd9d2005-06-03 18:01:50 +00001420#define ADDRESS_SIZE 20
1421 char dst[ADDRESS_SIZE];
1422 dst[ADDRESS_SIZE - 1] = '\0';
1423
paul931cd542004-01-23 15:31:42 +00001424 if (to)
1425 {
paulf69bd9d2005-06-03 18:01:50 +00001426 strncpy (dst, inet_ntoa(to->sin_addr), ADDRESS_SIZE - 1);
paul931cd542004-01-23 15:31:42 +00001427 }
1428 else
1429 {
1430 sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
paulf69bd9d2005-06-03 18:01:50 +00001431 strncpy (dst, inet_ntoa(sin.sin_addr), ADDRESS_SIZE - 1);
paul931cd542004-01-23 15:31:42 +00001432 }
paulf69bd9d2005-06-03 18:01:50 +00001433#undef ADDRESS_SIZE
ajs5d6c3772004-12-08 19:24:06 +00001434 zlog_debug("rip_send_packet %s > %s (%s)",
paulc49ad8f2004-10-22 10:27:28 +00001435 inet_ntoa(ifc->address->u.prefix4),
1436 dst, ifc->ifp->name);
paul931cd542004-01-23 15:31:42 +00001437 }
paulf69bd9d2005-06-03 18:01:50 +00001438
paulc49ad8f2004-10-22 10:27:28 +00001439 if ( CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY) )
paul931cd542004-01-23 15:31:42 +00001440 {
1441 /*
1442 * ZEBRA_IFA_SECONDARY is set on linux when an interface is configured
1443 * with multiple addresses on the same subnet: the first address
1444 * on the subnet is configured "primary", and all subsequent addresses
1445 * on that subnet are treated as "secondary" addresses.
1446 * In order to avoid routing-table bloat on other rip listeners,
1447 * we do not send out RIP packets with ZEBRA_IFA_SECONDARY source addrs.
1448 * XXX Since Linux is the only system for which the ZEBRA_IFA_SECONDARY
1449 * flag is set, we would end up sending a packet for a "secondary"
1450 * source address on non-linux systems.
1451 */
1452 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00001453 zlog_debug("duplicate dropped");
paul931cd542004-01-23 15:31:42 +00001454 return 0;
1455 }
1456
paul718e3742002-12-13 20:15:29 +00001457 /* Make destination address. */
1458 memset (&sin, 0, sizeof (struct sockaddr_in));
1459 sin.sin_family = AF_INET;
1460#ifdef HAVE_SIN_LEN
1461 sin.sin_len = sizeof (struct sockaddr_in);
1462#endif /* HAVE_SIN_LEN */
1463
1464 /* When destination is specified, use it's port and address. */
1465 if (to)
1466 {
paul718e3742002-12-13 20:15:29 +00001467 sin.sin_port = to->sin_port;
1468 sin.sin_addr = to->sin_addr;
paul931cd542004-01-23 15:31:42 +00001469 send_sock = rip->sock;
paul718e3742002-12-13 20:15:29 +00001470 }
1471 else
1472 {
paul2c61ae32005-08-16 15:22:14 +00001473 struct sockaddr_in from;
1474
paul718e3742002-12-13 20:15:29 +00001475 sin.sin_port = htons (RIP_PORT_DEFAULT);
1476 sin.sin_addr.s_addr = htonl (INADDR_RIP_GROUP);
paul2c61ae32005-08-16 15:22:14 +00001477
1478 /* multicast send should bind to local interface address */
1479 from.sin_family = AF_INET;
1480 from.sin_port = htons (RIP_PORT_DEFAULT);
1481 from.sin_addr = ifc->address->u.prefix4;
1482#ifdef HAVE_SIN_LEN
1483 from.sin_len = sizeof (struct sockaddr_in);
1484#endif /* HAVE_SIN_LEN */
1485
paul931cd542004-01-23 15:31:42 +00001486 /*
1487 * we have to open a new socket for each packet because this
1488 * is the most portable way to bind to a different source
1489 * ipv4 address for each packet.
1490 */
paul2c61ae32005-08-16 15:22:14 +00001491 if ( (send_sock = rip_create_socket (&from)) < 0)
paul931cd542004-01-23 15:31:42 +00001492 {
paulf69bd9d2005-06-03 18:01:50 +00001493 zlog_warn("rip_send_packet could not create socket.");
paul931cd542004-01-23 15:31:42 +00001494 return -1;
paulf69bd9d2005-06-03 18:01:50 +00001495 }
paulc49ad8f2004-10-22 10:27:28 +00001496 rip_interface_multicast_set (send_sock, ifc);
paul718e3742002-12-13 20:15:29 +00001497 }
1498
paul931cd542004-01-23 15:31:42 +00001499 ret = sendto (send_sock, buf, size, 0, (struct sockaddr *)&sin,
paul718e3742002-12-13 20:15:29 +00001500 sizeof (struct sockaddr_in));
1501
1502 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001503 zlog_debug ("SEND to %s.%d", inet_ntoa(sin.sin_addr),
paulcc1131a2003-10-15 23:20:17 +00001504 ntohs (sin.sin_port));
paul718e3742002-12-13 20:15:29 +00001505
1506 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +00001507 zlog_warn ("can't send packet : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001508
paul931cd542004-01-23 15:31:42 +00001509 if (!to)
1510 close(send_sock);
1511
paul718e3742002-12-13 20:15:29 +00001512 return ret;
1513}
1514
1515/* Add redistributed route to RIP table. */
1516void
1517rip_redistribute_add (int type, int sub_type, struct prefix_ipv4 *p,
vincentfbf5d032005-09-29 11:25:50 +00001518 unsigned int ifindex, struct in_addr *nexthop,
1519 unsigned int metric, unsigned char distance)
paul718e3742002-12-13 20:15:29 +00001520{
1521 int ret;
1522 struct route_node *rp;
1523 struct rip_info *rinfo;
1524
1525 /* Redistribute route */
1526 ret = rip_destination_check (p->prefix);
1527 if (! ret)
1528 return;
1529
1530 rp = route_node_get (rip->table, (struct prefix *) p);
1531
1532 rinfo = rp->info;
1533
1534 if (rinfo)
1535 {
1536 if (rinfo->type == ZEBRA_ROUTE_CONNECT
1537 && rinfo->sub_type == RIP_ROUTE_INTERFACE
1538 && rinfo->metric != RIP_METRIC_INFINITY)
1539 {
1540 route_unlock_node (rp);
1541 return;
1542 }
1543
1544 /* Manually configured RIP route check. */
1545 if (rinfo->type == ZEBRA_ROUTE_RIP
hasso16705132003-05-25 14:49:19 +00001546 && ((rinfo->sub_type == RIP_ROUTE_STATIC) ||
1547 (rinfo->sub_type == RIP_ROUTE_DEFAULT)) )
paul718e3742002-12-13 20:15:29 +00001548 {
hasso16705132003-05-25 14:49:19 +00001549 if (type != ZEBRA_ROUTE_RIP || ((sub_type != RIP_ROUTE_STATIC) &&
1550 (sub_type != RIP_ROUTE_DEFAULT)))
paul718e3742002-12-13 20:15:29 +00001551 {
1552 route_unlock_node (rp);
1553 return;
1554 }
1555 }
1556
1557 RIP_TIMER_OFF (rinfo->t_timeout);
1558 RIP_TIMER_OFF (rinfo->t_garbage_collect);
1559
1560 if (rip_route_rte (rinfo))
1561 rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p, &rinfo->nexthop,
1562 rinfo->metric);
1563 rp->info = NULL;
1564 rip_info_free (rinfo);
1565
1566 route_unlock_node (rp);
1567 }
1568
1569 rinfo = rip_info_new ();
1570
1571 rinfo->type = type;
1572 rinfo->sub_type = sub_type;
1573 rinfo->ifindex = ifindex;
1574 rinfo->metric = 1;
vincentfbf5d032005-09-29 11:25:50 +00001575 rinfo->external_metric = metric;
1576 rinfo->distance = distance;
paul718e3742002-12-13 20:15:29 +00001577 rinfo->rp = rp;
1578
1579 if (nexthop)
1580 rinfo->nexthop = *nexthop;
1581
1582 rinfo->flags |= RIP_RTF_FIB;
1583 rp->info = rinfo;
1584
1585 rinfo->flags |= RIP_RTF_CHANGED;
1586
hasso16705132003-05-25 14:49:19 +00001587 if (IS_RIP_DEBUG_EVENT) {
1588 if (!nexthop)
ajs5d6c3772004-12-08 19:24:06 +00001589 zlog_debug ("Redistribute new prefix %s/%d on the interface %s",
hasso16705132003-05-25 14:49:19 +00001590 inet_ntoa(p->prefix), p->prefixlen,
1591 ifindex2ifname(ifindex));
1592 else
ajs5d6c3772004-12-08 19:24:06 +00001593 zlog_debug ("Redistribute new prefix %s/%d with nexthop %s on the interface %s",
hasso16705132003-05-25 14:49:19 +00001594 inet_ntoa(p->prefix), p->prefixlen, inet_ntoa(rinfo->nexthop),
1595 ifindex2ifname(ifindex));
1596 }
1597
1598
paul718e3742002-12-13 20:15:29 +00001599 rip_event (RIP_TRIGGERED_UPDATE, 0);
1600}
1601
1602/* Delete redistributed route from RIP table. */
1603void
1604rip_redistribute_delete (int type, int sub_type, struct prefix_ipv4 *p,
1605 unsigned int ifindex)
1606{
1607 int ret;
1608 struct route_node *rp;
1609 struct rip_info *rinfo;
1610
1611 ret = rip_destination_check (p->prefix);
1612 if (! ret)
1613 return;
1614
1615 rp = route_node_lookup (rip->table, (struct prefix *) p);
1616 if (rp)
1617 {
1618 rinfo = rp->info;
1619
1620 if (rinfo != NULL
1621 && rinfo->type == type
1622 && rinfo->sub_type == sub_type
1623 && rinfo->ifindex == ifindex)
1624 {
1625 /* Perform poisoned reverse. */
1626 rinfo->metric = RIP_METRIC_INFINITY;
1627 RIP_TIMER_ON (rinfo->t_garbage_collect,
1628 rip_garbage_collect, rip->garbage_time);
1629 RIP_TIMER_OFF (rinfo->t_timeout);
1630 rinfo->flags |= RIP_RTF_CHANGED;
1631
hasso16705132003-05-25 14:49:19 +00001632 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001633 zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [delete]",
hasso16705132003-05-25 14:49:19 +00001634 inet_ntoa(p->prefix), p->prefixlen,
1635 ifindex2ifname(ifindex));
1636
paul718e3742002-12-13 20:15:29 +00001637 rip_event (RIP_TRIGGERED_UPDATE, 0);
1638 }
1639 }
1640}
1641
1642/* Response to request called from rip_read ().*/
pauldc63bfd2005-10-25 23:31:05 +00001643static void
paul718e3742002-12-13 20:15:29 +00001644rip_request_process (struct rip_packet *packet, int size,
paulc49ad8f2004-10-22 10:27:28 +00001645 struct sockaddr_in *from, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +00001646{
1647 caddr_t lim;
1648 struct rte *rte;
1649 struct prefix_ipv4 p;
1650 struct route_node *rp;
1651 struct rip_info *rinfo;
1652 struct rip_interface *ri;
1653
hasso16705132003-05-25 14:49:19 +00001654 /* Does not reponse to the requests on the loopback interfaces */
paulc49ad8f2004-10-22 10:27:28 +00001655 if (if_is_loopback (ifc->ifp))
hasso16705132003-05-25 14:49:19 +00001656 return;
1657
hasso429a0f82004-02-22 23:42:22 +00001658 /* Check RIP process is enabled on this interface. */
paulc49ad8f2004-10-22 10:27:28 +00001659 ri = ifc->ifp->info;
hasso16705132003-05-25 14:49:19 +00001660 if (! ri->running)
1661 return;
paul718e3742002-12-13 20:15:29 +00001662
1663 /* When passive interface is specified, suppress responses */
1664 if (ri->passive)
1665 return;
paulc49ad8f2004-10-22 10:27:28 +00001666
paul718e3742002-12-13 20:15:29 +00001667 /* RIP peer update. */
1668 rip_peer_update (from, packet->version);
1669
1670 lim = ((caddr_t) packet) + size;
1671 rte = packet->rte;
1672
1673 /* The Request is processed entry by entry. If there are no
1674 entries, no response is given. */
1675 if (lim == (caddr_t) rte)
1676 return;
1677
1678 /* There is one special case. If there is exactly one entry in the
1679 request, and it has an address family identifier of zero and a
1680 metric of infinity (i.e., 16), then this is a request to send the
1681 entire routing table. */
1682 if (lim == ((caddr_t) (rte + 1)) &&
1683 ntohs (rte->family) == 0 &&
1684 ntohl (rte->metric) == RIP_METRIC_INFINITY)
1685 {
paulcc1131a2003-10-15 23:20:17 +00001686 struct prefix_ipv4 saddr;
1687
1688 /* saddr will be used for determining which routes to split-horizon.
1689 Since the source address we'll pick will be on the same subnet as the
1690 destination, for the purpose of split-horizoning, we'll
1691 pretend that "from" is our source address. */
1692 saddr.family = AF_INET;
1693 saddr.prefixlen = IPV4_MAX_BITLEN;
1694 saddr.prefix = from->sin_addr;
1695
paul718e3742002-12-13 20:15:29 +00001696 /* All route with split horizon */
paulc49ad8f2004-10-22 10:27:28 +00001697 rip_output_process (ifc, from, rip_all_route, packet->version);
paul718e3742002-12-13 20:15:29 +00001698 }
1699 else
1700 {
1701 /* Examine the list of RTEs in the Request one by one. For each
1702 entry, look up the destination in the router's routing
1703 database and, if there is a route, put that route's metric in
1704 the metric field of the RTE. If there is no explicit route
1705 to the specified destination, put infinity in the metric
1706 field. Once all the entries have been filled in, change the
1707 command from Request to Response and send the datagram back
1708 to the requestor. */
1709 p.family = AF_INET;
1710
1711 for (; ((caddr_t) rte) < lim; rte++)
1712 {
1713 p.prefix = rte->prefix;
1714 p.prefixlen = ip_masklen (rte->mask);
1715 apply_mask_ipv4 (&p);
1716
1717 rp = route_node_lookup (rip->table, (struct prefix *) &p);
1718 if (rp)
1719 {
1720 rinfo = rp->info;
1721 rte->metric = htonl (rinfo->metric);
1722 route_unlock_node (rp);
1723 }
1724 else
1725 rte->metric = htonl (RIP_METRIC_INFINITY);
1726 }
1727 packet->command = RIP_RESPONSE;
1728
paulc49ad8f2004-10-22 10:27:28 +00001729 rip_send_packet ((u_char *)packet, size, from, ifc);
paul718e3742002-12-13 20:15:29 +00001730 }
1731 rip_global_queries++;
1732}
1733
1734#if RIP_RECVMSG
1735/* Set IPv6 packet info to the socket. */
1736static int
1737setsockopt_pktinfo (int sock)
1738{
1739 int ret;
1740 int val = 1;
1741
1742 ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
1743 if (ret < 0)
ajs6099b3b2004-11-20 02:06:59 +00001744 zlog_warn ("Can't setsockopt IP_PKTINFO : %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001745 return ret;
1746}
1747
1748/* Read RIP packet by recvmsg function. */
1749int
1750rip_recvmsg (int sock, u_char *buf, int size, struct sockaddr_in *from,
1751 int *ifindex)
1752{
1753 int ret;
1754 struct msghdr msg;
1755 struct iovec iov;
1756 struct cmsghdr *ptr;
1757 char adata[1024];
1758
1759 msg.msg_name = (void *) from;
1760 msg.msg_namelen = sizeof (struct sockaddr_in);
1761 msg.msg_iov = &iov;
1762 msg.msg_iovlen = 1;
1763 msg.msg_control = (void *) adata;
1764 msg.msg_controllen = sizeof adata;
1765 iov.iov_base = buf;
1766 iov.iov_len = size;
1767
1768 ret = recvmsg (sock, &msg, 0);
1769 if (ret < 0)
1770 return ret;
1771
ajsb99760a2005-01-04 16:24:43 +00001772 for (ptr = ZCMSG_FIRSTHDR(&msg); ptr != NULL; ptr = CMSG_NXTHDR(&msg, ptr))
paul718e3742002-12-13 20:15:29 +00001773 if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO)
1774 {
1775 struct in_pktinfo *pktinfo;
1776 int i;
1777
1778 pktinfo = (struct in_pktinfo *) CMSG_DATA (ptr);
1779 i = pktinfo->ipi_ifindex;
1780 }
1781 return ret;
1782}
1783
1784/* RIP packet read function. */
1785int
1786rip_read_new (struct thread *t)
1787{
1788 int ret;
1789 int sock;
1790 char buf[RIP_PACKET_MAXSIZ];
1791 struct sockaddr_in from;
1792 unsigned int ifindex;
1793
1794 /* Fetch socket then register myself. */
1795 sock = THREAD_FD (t);
1796 rip_event (RIP_READ, sock);
1797
1798 /* Read RIP packet. */
1799 ret = rip_recvmsg (sock, buf, RIP_PACKET_MAXSIZ, &from, (int *)&ifindex);
1800 if (ret < 0)
1801 {
ajs6099b3b2004-11-20 02:06:59 +00001802 zlog_warn ("Can't read RIP packet: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001803 return ret;
1804 }
1805
1806 return ret;
1807}
1808#endif /* RIP_RECVMSG */
1809
1810/* First entry point of RIP packet. */
pauldc63bfd2005-10-25 23:31:05 +00001811static int
paul718e3742002-12-13 20:15:29 +00001812rip_read (struct thread *t)
1813{
1814 int sock;
1815 int ret;
1816 int rtenum;
1817 union rip_buf rip_buf;
1818 struct rip_packet *packet;
1819 struct sockaddr_in from;
paul11dde9c2004-05-31 14:00:00 +00001820 int len;
1821 socklen_t fromlen;
paul718e3742002-12-13 20:15:29 +00001822 struct interface *ifp;
paulc49ad8f2004-10-22 10:27:28 +00001823 struct connected *ifc;
paul718e3742002-12-13 20:15:29 +00001824 struct rip_interface *ri;
1825
1826 /* Fetch socket then register myself. */
1827 sock = THREAD_FD (t);
1828 rip->t_read = NULL;
1829
1830 /* Add myself to tne next event */
1831 rip_event (RIP_READ, sock);
1832
1833 /* RIPd manages only IPv4. */
1834 memset (&from, 0, sizeof (struct sockaddr_in));
1835 fromlen = sizeof (struct sockaddr_in);
1836
1837 len = recvfrom (sock, (char *)&rip_buf.buf, sizeof (rip_buf.buf), 0,
1838 (struct sockaddr *) &from, &fromlen);
1839 if (len < 0)
1840 {
ajs6099b3b2004-11-20 02:06:59 +00001841 zlog_info ("recvfrom failed: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001842 return len;
1843 }
1844
1845 /* Check is this packet comming from myself? */
paul31a476c2003-09-29 19:54:53 +00001846 if (if_check_address (from.sin_addr))
paul718e3742002-12-13 20:15:29 +00001847 {
1848 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00001849 zlog_debug ("ignore packet comes from myself");
paul718e3742002-12-13 20:15:29 +00001850 return -1;
1851 }
1852
1853 /* Which interface is this packet comes from. */
1854 ifp = if_lookup_address (from.sin_addr);
paulc49ad8f2004-10-22 10:27:28 +00001855
paul718e3742002-12-13 20:15:29 +00001856 /* RIP packet received */
1857 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001858 zlog_debug ("RECV packet from %s port %d on %s",
paul718e3742002-12-13 20:15:29 +00001859 inet_ntoa (from.sin_addr), ntohs (from.sin_port),
1860 ifp ? ifp->name : "unknown");
1861
1862 /* If this packet come from unknown interface, ignore it. */
1863 if (ifp == NULL)
1864 {
ajs766a0ca2004-12-15 14:55:51 +00001865 zlog_info ("rip_read: cannot find interface for packet from %s port %d",
1866 inet_ntoa(from.sin_addr), ntohs (from.sin_port));
paulc49ad8f2004-10-22 10:27:28 +00001867 return -1;
1868 }
1869
1870 ifc = connected_lookup_address (ifp, from.sin_addr);
1871
1872 if (ifc == NULL)
1873 {
ajs766a0ca2004-12-15 14:55:51 +00001874 zlog_info ("rip_read: cannot find connected address for packet from %s "
1875 "port %d on interface %s",
1876 inet_ntoa(from.sin_addr), ntohs (from.sin_port), ifp->name);
paul718e3742002-12-13 20:15:29 +00001877 return -1;
1878 }
1879
1880 /* Packet length check. */
1881 if (len < RIP_PACKET_MINSIZ)
1882 {
1883 zlog_warn ("packet size %d is smaller than minimum size %d",
1884 len, RIP_PACKET_MINSIZ);
1885 rip_peer_bad_packet (&from);
1886 return len;
1887 }
1888 if (len > RIP_PACKET_MAXSIZ)
1889 {
1890 zlog_warn ("packet size %d is larger than max size %d",
1891 len, RIP_PACKET_MAXSIZ);
1892 rip_peer_bad_packet (&from);
1893 return len;
1894 }
1895
1896 /* Packet alignment check. */
1897 if ((len - RIP_PACKET_MINSIZ) % 20)
1898 {
1899 zlog_warn ("packet size %d is wrong for RIP packet alignment", len);
1900 rip_peer_bad_packet (&from);
1901 return len;
1902 }
1903
1904 /* Set RTE number. */
1905 rtenum = ((len - RIP_PACKET_MINSIZ) / 20);
1906
1907 /* For easy to handle. */
1908 packet = &rip_buf.rip_packet;
1909
1910 /* RIP version check. */
1911 if (packet->version == 0)
1912 {
1913 zlog_info ("version 0 with command %d received.", packet->command);
1914 rip_peer_bad_packet (&from);
1915 return -1;
1916 }
1917
1918 /* Dump RIP packet. */
1919 if (IS_RIP_DEBUG_RECV)
1920 rip_packet_dump (packet, len, "RECV");
1921
1922 /* RIP version adjust. This code should rethink now. RFC1058 says
1923 that "Version 1 implementations are to ignore this extra data and
1924 process only the fields specified in this document.". So RIPv3
1925 packet should be treated as RIPv1 ignoring must be zero field. */
1926 if (packet->version > RIPv2)
1927 packet->version = RIPv2;
1928
1929 /* Is RIP running or is this RIP neighbor ?*/
1930 ri = ifp->info;
1931 if (! ri->running && ! rip_neighbor_lookup (&from))
1932 {
1933 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001934 zlog_debug ("RIP is not enabled on interface %s.", ifp->name);
paul718e3742002-12-13 20:15:29 +00001935 rip_peer_bad_packet (&from);
1936 return -1;
1937 }
1938
Paul Jakma15a2b082006-05-04 07:36:34 +00001939 /* RIP Version check. RFC2453, 4.6 and 5.1 */
1940 int vrecv = ((ri->ri_receive == RI_RIP_UNSPEC) ?
1941 rip->version_recv : ri->ri_receive);
1942 if ((packet->version == RIPv1) && !(vrecv & RIPv1))
paul718e3742002-12-13 20:15:29 +00001943 {
Paul Jakma15a2b082006-05-04 07:36:34 +00001944 if (IS_RIP_DEBUG_PACKET)
1945 zlog_debug (" packet's v%d doesn't fit to if version spec",
1946 packet->version);
1947 rip_peer_bad_packet (&from);
1948 return -1;
paul718e3742002-12-13 20:15:29 +00001949 }
Paul Jakma15a2b082006-05-04 07:36:34 +00001950 if ((packet->version == RIPv2) && !(vrecv & RIPv2))
1951 {
1952 if (IS_RIP_DEBUG_PACKET)
1953 zlog_debug (" packet's v%d doesn't fit to if version spec",
1954 packet->version);
1955 rip_peer_bad_packet (&from);
1956 return -1;
1957 }
1958
paul718e3742002-12-13 20:15:29 +00001959 /* RFC2453 5.2 If the router is not configured to authenticate RIP-2
1960 messages, then RIP-1 and unauthenticated RIP-2 messages will be
1961 accepted; authenticated RIP-2 messages shall be discarded. */
paul718e3742002-12-13 20:15:29 +00001962 if ((ri->auth_type == RIP_NO_AUTH)
1963 && rtenum
paulca5e5162004-06-06 22:06:33 +00001964 && (packet->version == RIPv2)
1965 && (packet->rte->family == htons(RIP_FAMILY_AUTH)))
paul718e3742002-12-13 20:15:29 +00001966 {
1967 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00001968 zlog_debug ("packet RIPv%d is dropped because authentication disabled",
paul718e3742002-12-13 20:15:29 +00001969 packet->version);
1970 rip_peer_bad_packet (&from);
1971 return -1;
1972 }
Paul Jakma15a2b082006-05-04 07:36:34 +00001973
1974 /* RFC:
1975 If the router is configured to authenticate RIP-2 messages, then
paul718e3742002-12-13 20:15:29 +00001976 RIP-1 messages and RIP-2 messages which pass authentication
1977 testing shall be accepted; unauthenticated and failed
1978 authentication RIP-2 messages shall be discarded. For maximum
1979 security, RIP-1 messages should be ignored when authentication is
1980 in use (see section 4.1); otherwise, the routing information from
1981 authenticated messages will be propagated by RIP-1 routers in an
Paul Jakma15a2b082006-05-04 07:36:34 +00001982 unauthenticated manner.
1983 */
1984 /* We make an exception for RIPv1 REQUEST packets, to which we'll
1985 * always reply regardless of authentication settings, because:
1986 *
1987 * - if there other authorised routers on-link, the REQUESTor can
1988 * passively obtain the routing updates anyway
1989 * - if there are no other authorised routers on-link, RIP can
1990 * easily be disabled for the link to prevent giving out information
1991 * on state of this routers RIP routing table..
1992 *
1993 * I.e. if RIPv1 has any place anymore these days, it's as a very
1994 * simple way to distribute routing information (e.g. to embedded
1995 * hosts / appliances) and the ability to give out RIPv1
1996 * routing-information freely, while still requiring RIPv2
1997 * authentication for any RESPONSEs might be vaguely useful.
1998 */
1999 if (ri->auth_type != RIP_NO_AUTH
2000 && packet->version == RIPv1)
paul718e3742002-12-13 20:15:29 +00002001 {
Paul Jakma15a2b082006-05-04 07:36:34 +00002002 /* Discard RIPv1 messages other than REQUESTs */
2003 if (packet->command != RIP_REQUEST)
2004 {
2005 if (IS_RIP_DEBUG_PACKET)
2006 zlog_debug ("RIPv1" " dropped because authentication enabled");
2007 rip_peer_bad_packet (&from);
2008 return -1;
2009 }
2010 }
2011 else if (ri->auth_type != RIP_NO_AUTH)
2012 {
2013 const char *auth_desc;
2014
2015 if (rtenum == 0)
2016 {
2017 /* There definitely is no authentication in the packet. */
2018 if (IS_RIP_DEBUG_PACKET)
2019 zlog_debug ("RIPv2 authentication failed: no auth RTE in packet");
2020 rip_peer_bad_packet (&from);
2021 return -1;
2022 }
2023
2024 /* First RTE must be an Authentication Family RTE */
2025 if (packet->rte->family != htons(RIP_FAMILY_AUTH))
2026 {
2027 if (IS_RIP_DEBUG_PACKET)
2028 zlog_debug ("RIPv2" " dropped because authentication enabled");
paul718e3742002-12-13 20:15:29 +00002029 rip_peer_bad_packet (&from);
2030 return -1;
Paul Jakma15a2b082006-05-04 07:36:34 +00002031 }
2032
paul718e3742002-12-13 20:15:29 +00002033 /* Check RIPv2 authentication. */
Paul Jakma15a2b082006-05-04 07:36:34 +00002034 switch (ntohs(packet->rte->tag))
2035 {
2036 case RIP_AUTH_SIMPLE_PASSWORD:
2037 auth_desc = "simple";
2038 ret = rip_auth_simple_password (packet->rte, &from, ifp);
2039 break;
2040
2041 case RIP_AUTH_MD5:
2042 auth_desc = "MD5";
2043 ret = rip_auth_md5 (packet, &from, len, ifp);
2044 /* Reset RIP packet length to trim MD5 data. */
2045 len = ret;
2046 break;
2047
2048 default:
2049 ret = 0;
2050 auth_desc = "unknown type";
2051 if (IS_RIP_DEBUG_PACKET)
2052 zlog_debug ("RIPv2 Unknown authentication type %d",
2053 ntohs (packet->rte->tag));
2054 }
2055
2056 if (ret)
2057 {
2058 if (IS_RIP_DEBUG_PACKET)
2059 zlog_debug ("RIPv2 %s authentication success", auth_desc);
2060 }
2061 else
2062 {
2063 if (IS_RIP_DEBUG_PACKET)
2064 zlog_debug ("RIPv2 %s authentication failure", auth_desc);
2065 rip_peer_bad_packet (&from);
2066 return -1;
2067 }
paul718e3742002-12-13 20:15:29 +00002068 }
2069
2070 /* Process each command. */
2071 switch (packet->command)
2072 {
2073 case RIP_RESPONSE:
paulc49ad8f2004-10-22 10:27:28 +00002074 rip_response_process (packet, len, &from, ifc);
paul718e3742002-12-13 20:15:29 +00002075 break;
2076 case RIP_REQUEST:
2077 case RIP_POLL:
paulc49ad8f2004-10-22 10:27:28 +00002078 rip_request_process (packet, len, &from, ifc);
paul718e3742002-12-13 20:15:29 +00002079 break;
2080 case RIP_TRACEON:
2081 case RIP_TRACEOFF:
2082 zlog_info ("Obsolete command %s received, please sent it to routed",
2083 lookup (rip_msg, packet->command));
2084 rip_peer_bad_packet (&from);
2085 break;
2086 case RIP_POLL_ENTRY:
2087 zlog_info ("Obsolete command %s received",
2088 lookup (rip_msg, packet->command));
2089 rip_peer_bad_packet (&from);
2090 break;
2091 default:
2092 zlog_info ("Unknown RIP command %d received", packet->command);
2093 rip_peer_bad_packet (&from);
2094 break;
2095 }
2096
2097 return len;
2098}
2099
paul718e3742002-12-13 20:15:29 +00002100/* Write routing table entry to the stream and return next index of
2101 the routing table entry in the stream. */
pauldc63bfd2005-10-25 23:31:05 +00002102static int
paul718e3742002-12-13 20:15:29 +00002103rip_write_rte (int num, struct stream *s, struct prefix_ipv4 *p,
paulb14ee002005-02-04 23:42:41 +00002104 u_char version, struct rip_info *rinfo)
paul718e3742002-12-13 20:15:29 +00002105{
2106 struct in_addr mask;
paul718e3742002-12-13 20:15:29 +00002107
2108 /* Write routing table entry. */
2109 if (version == RIPv1)
2110 {
2111 stream_putw (s, AF_INET);
2112 stream_putw (s, 0);
2113 stream_put_ipv4 (s, p->prefix.s_addr);
2114 stream_put_ipv4 (s, 0);
2115 stream_put_ipv4 (s, 0);
2116 stream_putl (s, rinfo->metric_out);
2117 }
2118 else
2119 {
2120 masklen2ip (p->prefixlen, &mask);
2121
2122 stream_putw (s, AF_INET);
hasso16705132003-05-25 14:49:19 +00002123 stream_putw (s, rinfo->tag_out);
paul718e3742002-12-13 20:15:29 +00002124 stream_put_ipv4 (s, p->prefix.s_addr);
2125 stream_put_ipv4 (s, mask.s_addr);
2126 stream_put_ipv4 (s, rinfo->nexthop_out.s_addr);
2127 stream_putl (s, rinfo->metric_out);
2128 }
2129
2130 return ++num;
2131}
2132
2133/* Send update to the ifp or spcified neighbor. */
2134void
paulc49ad8f2004-10-22 10:27:28 +00002135rip_output_process (struct connected *ifc, struct sockaddr_in *to,
2136 int route_type, u_char version)
paul718e3742002-12-13 20:15:29 +00002137{
2138 int ret;
2139 struct stream *s;
2140 struct route_node *rp;
2141 struct rip_info *rinfo;
2142 struct rip_interface *ri;
2143 struct prefix_ipv4 *p;
2144 struct prefix_ipv4 classfull;
paul727d1042002-12-13 20:50:29 +00002145 struct prefix_ipv4 ifaddrclass;
paulb14ee002005-02-04 23:42:41 +00002146 struct key *key = NULL;
2147 /* this might need to made dynamic if RIP ever supported auth methods
2148 with larger key string sizes */
2149 char auth_str[RIP_AUTH_SIMPLE_SIZE];
pauldc63bfd2005-10-25 23:31:05 +00002150 size_t doff = 0; /* offset of digest offset field */
paul2c61ae32005-08-16 15:22:14 +00002151 int num = 0;
paul718e3742002-12-13 20:15:29 +00002152 int rtemax;
paul01d09082003-06-08 21:22:18 +00002153 int subnetted = 0;
paul718e3742002-12-13 20:15:29 +00002154
2155 /* Logging output event. */
2156 if (IS_RIP_DEBUG_EVENT)
2157 {
2158 if (to)
ajs5d6c3772004-12-08 19:24:06 +00002159 zlog_debug ("update routes to neighbor %s", inet_ntoa (to->sin_addr));
paul718e3742002-12-13 20:15:29 +00002160 else
ajs5d6c3772004-12-08 19:24:06 +00002161 zlog_debug ("update routes on interface %s ifindex %d",
paulc49ad8f2004-10-22 10:27:28 +00002162 ifc->ifp->name, ifc->ifp->ifindex);
paul718e3742002-12-13 20:15:29 +00002163 }
2164
2165 /* Set output stream. */
2166 s = rip->obuf;
2167
2168 /* Reset stream and RTE counter. */
2169 stream_reset (s);
paul718e3742002-12-13 20:15:29 +00002170 rtemax = (RIP_PACKET_MAXSIZ - 4) / 20;
2171
2172 /* Get RIP interface. */
paulc49ad8f2004-10-22 10:27:28 +00002173 ri = ifc->ifp->info;
paul718e3742002-12-13 20:15:29 +00002174
2175 /* If output interface is in simple password authentication mode, we
2176 need space for authentication data. */
2177 if (ri->auth_type == RIP_AUTH_SIMPLE_PASSWORD)
2178 rtemax -= 1;
2179
2180 /* If output interface is in MD5 authentication mode, we need space
2181 for authentication header and data. */
2182 if (ri->auth_type == RIP_AUTH_MD5)
2183 rtemax -= 2;
2184
2185 /* If output interface is in simple password authentication mode
2186 and string or keychain is specified we need space for auth. data */
paulb14ee002005-02-04 23:42:41 +00002187 if (ri->auth_type != RIP_NO_AUTH)
paul718e3742002-12-13 20:15:29 +00002188 {
2189 if (ri->key_chain)
2190 {
2191 struct keychain *keychain;
2192
2193 keychain = keychain_lookup (ri->key_chain);
2194 if (keychain)
paulb14ee002005-02-04 23:42:41 +00002195 key = key_lookup_for_send (keychain);
paul718e3742002-12-13 20:15:29 +00002196 }
paulb14ee002005-02-04 23:42:41 +00002197 /* to be passed to auth functions later */
2198 rip_auth_prepare_str_send (ri, key, auth_str, RIP_AUTH_SIMPLE_SIZE);
paul718e3742002-12-13 20:15:29 +00002199 }
2200
paul727d1042002-12-13 20:50:29 +00002201 if (version == RIPv1)
2202 {
paulc49ad8f2004-10-22 10:27:28 +00002203 memcpy (&ifaddrclass, ifc->address, sizeof (struct prefix_ipv4));
paul727d1042002-12-13 20:50:29 +00002204 apply_classful_mask_ipv4 (&ifaddrclass);
2205 subnetted = 0;
paulc49ad8f2004-10-22 10:27:28 +00002206 if (ifc->address->prefixlen > ifaddrclass.prefixlen)
paul01d09082003-06-08 21:22:18 +00002207 subnetted = 1;
paul727d1042002-12-13 20:50:29 +00002208 }
2209
paul718e3742002-12-13 20:15:29 +00002210 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2211 if ((rinfo = rp->info) != NULL)
2212 {
paul727d1042002-12-13 20:50:29 +00002213 /* For RIPv1, if we are subnetted, output subnets in our network */
2214 /* that have the same mask as the output "interface". For other */
2215 /* networks, only the classfull version is output. */
paul718e3742002-12-13 20:15:29 +00002216
2217 if (version == RIPv1)
2218 {
paul727d1042002-12-13 20:50:29 +00002219 p = (struct prefix_ipv4 *) &rp->p;
paul718e3742002-12-13 20:15:29 +00002220
2221 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00002222 zlog_debug("RIPv1 mask check, %s/%d considered for output",
paul727d1042002-12-13 20:50:29 +00002223 inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
paul718e3742002-12-13 20:15:29 +00002224
paul727d1042002-12-13 20:50:29 +00002225 if (subnetted &&
2226 prefix_match ((struct prefix *) &ifaddrclass, &rp->p))
2227 {
paulc49ad8f2004-10-22 10:27:28 +00002228 if ((ifc->address->prefixlen != rp->p.prefixlen) &&
paul727d1042002-12-13 20:50:29 +00002229 (rp->p.prefixlen != 32))
2230 continue;
2231 }
2232 else
2233 {
2234 memcpy (&classfull, &rp->p, sizeof(struct prefix_ipv4));
2235 apply_classful_mask_ipv4(&classfull);
2236 if (rp->p.u.prefix4.s_addr != 0 &&
2237 classfull.prefixlen != rp->p.prefixlen)
2238 continue;
2239 }
paul718e3742002-12-13 20:15:29 +00002240 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00002241 zlog_debug("RIPv1 mask check, %s/%d made it through",
paul727d1042002-12-13 20:50:29 +00002242 inet_ntoa (rp->p.u.prefix4), rp->p.prefixlen);
paul718e3742002-12-13 20:15:29 +00002243 }
2244 else
2245 p = (struct prefix_ipv4 *) &rp->p;
2246
2247 /* Apply output filters. */
2248 ret = rip_outgoing_filter (p, ri);
2249 if (ret < 0)
2250 continue;
2251
2252 /* Changed route only output. */
2253 if (route_type == rip_changed_route &&
2254 (! (rinfo->flags & RIP_RTF_CHANGED)))
2255 continue;
2256
2257 /* Split horizon. */
2258 /* if (split_horizon == rip_split_horizon) */
hasso16705132003-05-25 14:49:19 +00002259 if (ri->split_horizon == RIP_SPLIT_HORIZON)
paul718e3742002-12-13 20:15:29 +00002260 {
paul42d14d92003-11-17 09:15:18 +00002261 /*
2262 * We perform split horizon for RIP and connected route.
2263 * For rip routes, we want to suppress the route if we would
2264 * end up sending the route back on the interface that we
2265 * learned it from, with a higher metric. For connected routes,
2266 * we suppress the route if the prefix is a subset of the
2267 * source address that we are going to use for the packet
2268 * (in order to handle the case when multiple subnets are
2269 * configured on the same interface).
2270 */
2271 if (rinfo->type == ZEBRA_ROUTE_RIP &&
paulc49ad8f2004-10-22 10:27:28 +00002272 rinfo->ifindex == ifc->ifp->ifindex)
paul42d14d92003-11-17 09:15:18 +00002273 continue;
2274 if (rinfo->type == ZEBRA_ROUTE_CONNECT &&
paulc49ad8f2004-10-22 10:27:28 +00002275 prefix_match((struct prefix *)p, ifc->address))
paul718e3742002-12-13 20:15:29 +00002276 continue;
2277 }
2278
2279 /* Preparation for route-map. */
2280 rinfo->metric_set = 0;
2281 rinfo->nexthop_out.s_addr = 0;
2282 rinfo->metric_out = rinfo->metric;
hasso16705132003-05-25 14:49:19 +00002283 rinfo->tag_out = rinfo->tag;
paulc49ad8f2004-10-22 10:27:28 +00002284 rinfo->ifindex_out = ifc->ifp->ifindex;
paul718e3742002-12-13 20:15:29 +00002285
hasso16705132003-05-25 14:49:19 +00002286 /* In order to avoid some local loops,
2287 * if the RIP route has a nexthop via this interface, keep the nexthop,
2288 * otherwise set it to 0. The nexthop should not be propagated
2289 * beyond the local broadcast/multicast area in order
2290 * to avoid an IGP multi-level recursive look-up.
2291 * see (4.4)
2292 */
paulc49ad8f2004-10-22 10:27:28 +00002293 if (rinfo->ifindex == ifc->ifp->ifindex)
paul718e3742002-12-13 20:15:29 +00002294 rinfo->nexthop_out = rinfo->nexthop;
hasso16705132003-05-25 14:49:19 +00002295
2296 /* Interface route-map */
2297 if (ri->routemap[RIP_FILTER_OUT])
2298 {
2299 ret = route_map_apply (ri->routemap[RIP_FILTER_OUT],
2300 (struct prefix *) p, RMAP_RIP,
2301 rinfo);
2302
2303 if (ret == RMAP_DENYMATCH)
2304 {
2305 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00002306 zlog_debug ("RIP %s/%d is filtered by route-map out",
hasso16705132003-05-25 14:49:19 +00002307 inet_ntoa (p->prefix), p->prefixlen);
2308 continue;
2309 }
2310 }
paul718e3742002-12-13 20:15:29 +00002311
hasso16705132003-05-25 14:49:19 +00002312 /* Apply redistribute route map - continue, if deny */
paul718e3742002-12-13 20:15:29 +00002313 if (rip->route_map[rinfo->type].name
2314 && rinfo->sub_type != RIP_ROUTE_INTERFACE)
2315 {
2316 ret = route_map_apply (rip->route_map[rinfo->type].map,
2317 (struct prefix *)p, RMAP_RIP, rinfo);
2318
2319 if (ret == RMAP_DENYMATCH)
2320 {
2321 if (IS_RIP_DEBUG_PACKET)
ajs5d6c3772004-12-08 19:24:06 +00002322 zlog_debug ("%s/%d is filtered by route-map",
paul718e3742002-12-13 20:15:29 +00002323 inet_ntoa (p->prefix), p->prefixlen);
2324 continue;
2325 }
2326 }
2327
2328 /* When route-map does not set metric. */
2329 if (! rinfo->metric_set)
2330 {
2331 /* If redistribute metric is set. */
2332 if (rip->route_map[rinfo->type].metric_config
2333 && rinfo->metric != RIP_METRIC_INFINITY)
2334 {
2335 rinfo->metric_out = rip->route_map[rinfo->type].metric;
2336 }
2337 else
2338 {
2339 /* If the route is not connected or localy generated
2340 one, use default-metric value*/
2341 if (rinfo->type != ZEBRA_ROUTE_RIP
2342 && rinfo->type != ZEBRA_ROUTE_CONNECT
2343 && rinfo->metric != RIP_METRIC_INFINITY)
2344 rinfo->metric_out = rip->default_metric;
2345 }
2346 }
2347
2348 /* Apply offset-list */
2349 if (rinfo->metric != RIP_METRIC_INFINITY)
paulc49ad8f2004-10-22 10:27:28 +00002350 rip_offset_list_apply_out (p, ifc->ifp, &rinfo->metric_out);
paul718e3742002-12-13 20:15:29 +00002351
2352 if (rinfo->metric_out > RIP_METRIC_INFINITY)
2353 rinfo->metric_out = RIP_METRIC_INFINITY;
hasso16705132003-05-25 14:49:19 +00002354
2355 /* Perform split-horizon with poisoned reverse
2356 * for RIP and connected routes.
2357 **/
2358 if (ri->split_horizon == RIP_SPLIT_HORIZON_POISONED_REVERSE) {
paul42d14d92003-11-17 09:15:18 +00002359 /*
2360 * We perform split horizon for RIP and connected route.
2361 * For rip routes, we want to suppress the route if we would
2362 * end up sending the route back on the interface that we
2363 * learned it from, with a higher metric. For connected routes,
2364 * we suppress the route if the prefix is a subset of the
2365 * source address that we are going to use for the packet
2366 * (in order to handle the case when multiple subnets are
2367 * configured on the same interface).
2368 */
2369 if (rinfo->type == ZEBRA_ROUTE_RIP &&
paulc49ad8f2004-10-22 10:27:28 +00002370 rinfo->ifindex == ifc->ifp->ifindex)
hasso16705132003-05-25 14:49:19 +00002371 rinfo->metric_out = RIP_METRIC_INFINITY;
paul42d14d92003-11-17 09:15:18 +00002372 if (rinfo->type == ZEBRA_ROUTE_CONNECT &&
paulc49ad8f2004-10-22 10:27:28 +00002373 prefix_match((struct prefix *)p, ifc->address))
paul42d14d92003-11-17 09:15:18 +00002374 rinfo->metric_out = RIP_METRIC_INFINITY;
hasso16705132003-05-25 14:49:19 +00002375 }
paulb14ee002005-02-04 23:42:41 +00002376
2377 /* Prepare preamble, auth headers, if needs be */
2378 if (num == 0)
2379 {
2380 stream_putc (s, RIP_RESPONSE);
2381 stream_putc (s, version);
2382 stream_putw (s, 0);
2383
paul0cb8a012005-05-29 11:27:24 +00002384 /* auth header for !v1 && !no_auth */
2385 if ( (ri->auth_type != RIP_NO_AUTH) && (version != RIPv1) )
paulb14ee002005-02-04 23:42:41 +00002386 doff = rip_auth_header_write (s, ri, key, auth_str,
2387 RIP_AUTH_SIMPLE_SIZE);
2388 }
2389
paul718e3742002-12-13 20:15:29 +00002390 /* Write RTE to the stream. */
paulb14ee002005-02-04 23:42:41 +00002391 num = rip_write_rte (num, s, p, version, rinfo);
paul718e3742002-12-13 20:15:29 +00002392 if (num == rtemax)
2393 {
2394 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
paulb14ee002005-02-04 23:42:41 +00002395 rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
paul718e3742002-12-13 20:15:29 +00002396
2397 ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s),
paulc49ad8f2004-10-22 10:27:28 +00002398 to, ifc);
paul718e3742002-12-13 20:15:29 +00002399
2400 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2401 rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
2402 stream_get_endp(s), "SEND");
2403 num = 0;
2404 stream_reset (s);
2405 }
2406 }
2407
2408 /* Flush unwritten RTE. */
2409 if (num != 0)
2410 {
2411 if (version == RIPv2 && ri->auth_type == RIP_AUTH_MD5)
paulb14ee002005-02-04 23:42:41 +00002412 rip_auth_md5_set (s, ri, doff, auth_str, RIP_AUTH_SIMPLE_SIZE);
paul718e3742002-12-13 20:15:29 +00002413
paulc49ad8f2004-10-22 10:27:28 +00002414 ret = rip_send_packet (STREAM_DATA (s), stream_get_endp (s), to, ifc);
paul718e3742002-12-13 20:15:29 +00002415
2416 if (ret >= 0 && IS_RIP_DEBUG_SEND)
2417 rip_packet_dump ((struct rip_packet *)STREAM_DATA (s),
2418 stream_get_endp (s), "SEND");
2419 num = 0;
2420 stream_reset (s);
2421 }
2422
2423 /* Statistics updates. */
2424 ri->sent_updates++;
2425}
2426
2427/* Send RIP packet to the interface. */
pauldc63bfd2005-10-25 23:31:05 +00002428static void
paulc49ad8f2004-10-22 10:27:28 +00002429rip_update_interface (struct connected *ifc, u_char version, int route_type)
paul718e3742002-12-13 20:15:29 +00002430{
paul718e3742002-12-13 20:15:29 +00002431 struct sockaddr_in to;
2432
2433 /* When RIP version is 2 and multicast enable interface. */
paulc49ad8f2004-10-22 10:27:28 +00002434 if (version == RIPv2 && if_is_multicast (ifc->ifp))
paul718e3742002-12-13 20:15:29 +00002435 {
2436 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00002437 zlog_debug ("multicast announce on %s ", ifc->ifp->name);
paul718e3742002-12-13 20:15:29 +00002438
paulc49ad8f2004-10-22 10:27:28 +00002439 rip_output_process (ifc, NULL, route_type, version);
paul718e3742002-12-13 20:15:29 +00002440 return;
2441 }
paulc49ad8f2004-10-22 10:27:28 +00002442
paul718e3742002-12-13 20:15:29 +00002443 /* If we can't send multicast packet, send it with unicast. */
paulc49ad8f2004-10-22 10:27:28 +00002444 if (if_is_broadcast (ifc->ifp) || if_is_pointopoint (ifc->ifp))
paul718e3742002-12-13 20:15:29 +00002445 {
paulc49ad8f2004-10-22 10:27:28 +00002446 if (ifc->address->family == AF_INET)
2447 {
2448 /* Destination address and port setting. */
2449 memset (&to, 0, sizeof (struct sockaddr_in));
2450 if (ifc->destination)
2451 /* use specified broadcast or point-to-point destination addr */
2452 to.sin_addr = ifc->destination->u.prefix4;
2453 else
2454 /* calculate the appropriate broadcast address */
2455 to.sin_addr.s_addr =
2456 ipv4_broadcast_addr(ifc->address->u.prefix4.s_addr,
2457 ifc->address->prefixlen);
2458 to.sin_port = htons (RIP_PORT_DEFAULT);
paul718e3742002-12-13 20:15:29 +00002459
paulc49ad8f2004-10-22 10:27:28 +00002460 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00002461 zlog_debug ("%s announce to %s on %s",
paulc49ad8f2004-10-22 10:27:28 +00002462 if_is_pointopoint (ifc->ifp) ? "unicast" : "broadcast",
2463 inet_ntoa (to.sin_addr), ifc->ifp->name);
paul718e3742002-12-13 20:15:29 +00002464
paulc49ad8f2004-10-22 10:27:28 +00002465 rip_output_process (ifc, &to, route_type, version);
2466 }
paul718e3742002-12-13 20:15:29 +00002467 }
2468}
2469
2470/* Update send to all interface and neighbor. */
pauldc63bfd2005-10-25 23:31:05 +00002471static void
paul718e3742002-12-13 20:15:29 +00002472rip_update_process (int route_type)
2473{
paul1eb8ef22005-04-07 07:30:20 +00002474 struct listnode *node;
2475 struct listnode *ifnode, *ifnnode;
paulcc1131a2003-10-15 23:20:17 +00002476 struct connected *connected;
paul718e3742002-12-13 20:15:29 +00002477 struct interface *ifp;
2478 struct rip_interface *ri;
2479 struct route_node *rp;
2480 struct sockaddr_in to;
2481 struct prefix_ipv4 *p;
2482
2483 /* Send RIP update to each interface. */
paul1eb8ef22005-04-07 07:30:20 +00002484 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +00002485 {
paul718e3742002-12-13 20:15:29 +00002486 if (if_is_loopback (ifp))
2487 continue;
2488
paul2e3b2e42002-12-13 21:03:13 +00002489 if (! if_is_operative (ifp))
paul718e3742002-12-13 20:15:29 +00002490 continue;
2491
2492 /* Fetch RIP interface information. */
2493 ri = ifp->info;
2494
2495 /* When passive interface is specified, suppress announce to the
2496 interface. */
2497 if (ri->passive)
2498 continue;
2499
2500 if (ri->running)
2501 {
Andrew J. Schorrb9d92882006-04-28 16:22:36 +00002502 /*
2503 * If there is no version configuration in the interface,
2504 * use rip's version setting.
2505 */
2506 int vsend = ((ri->ri_send == RI_RIP_UNSPEC) ?
2507 rip->version_send : ri->ri_send);
2508
paul718e3742002-12-13 20:15:29 +00002509 if (IS_RIP_DEBUG_EVENT)
Andrew J. Schorrb9d92882006-04-28 16:22:36 +00002510 zlog_debug("SEND UPDATE to %s ifindex %d",
2511 (ifp->name ? ifp->name : "_unknown_"), ifp->ifindex);
paul718e3742002-12-13 20:15:29 +00002512
paulcc1131a2003-10-15 23:20:17 +00002513 /* send update on each connected network */
paul1eb8ef22005-04-07 07:30:20 +00002514 for (ALL_LIST_ELEMENTS (ifp->connected, ifnode, ifnnode, connected))
paulcc1131a2003-10-15 23:20:17 +00002515 {
Andrew J. Schorrb9d92882006-04-28 16:22:36 +00002516 if (connected->address->family == AF_INET)
2517 {
2518 if (vsend & RIPv1)
2519 rip_update_interface (connected, RIPv1, route_type);
2520 if ((vsend & RIPv2) && if_is_multicast(ifp))
2521 rip_update_interface (connected, RIPv2, route_type);
2522 }
2523 }
paul718e3742002-12-13 20:15:29 +00002524 }
2525 }
2526
2527 /* RIP send updates to each neighbor. */
2528 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
2529 if (rp->info != NULL)
2530 {
2531 p = (struct prefix_ipv4 *) &rp->p;
2532
2533 ifp = if_lookup_address (p->prefix);
2534 if (! ifp)
2535 {
paulc49ad8f2004-10-22 10:27:28 +00002536 zlog_warn ("Neighbor %s doesnt have connected interface!",
paul718e3742002-12-13 20:15:29 +00002537 inet_ntoa (p->prefix));
2538 continue;
2539 }
paulc49ad8f2004-10-22 10:27:28 +00002540
2541 if ( (connected = connected_lookup_address (ifp, p->prefix)) == NULL)
2542 {
2543 zlog_warn ("Neighbor %s doesnt have connected network",
2544 inet_ntoa (p->prefix));
2545 continue;
2546 }
2547
paul718e3742002-12-13 20:15:29 +00002548 /* Set destination address and port */
2549 memset (&to, 0, sizeof (struct sockaddr_in));
2550 to.sin_addr = p->prefix;
2551 to.sin_port = htons (RIP_PORT_DEFAULT);
2552
2553 /* RIP version is rip's configuration. */
paulc49ad8f2004-10-22 10:27:28 +00002554 rip_output_process (connected, &to, route_type, rip->version_send);
paul718e3742002-12-13 20:15:29 +00002555 }
2556}
2557
2558/* RIP's periodical timer. */
pauldc63bfd2005-10-25 23:31:05 +00002559static int
paul718e3742002-12-13 20:15:29 +00002560rip_update (struct thread *t)
2561{
2562 /* Clear timer pointer. */
2563 rip->t_update = NULL;
2564
2565 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00002566 zlog_debug ("update timer fire!");
paul718e3742002-12-13 20:15:29 +00002567
2568 /* Process update output. */
2569 rip_update_process (rip_all_route);
2570
2571 /* Triggered updates may be suppressed if a regular update is due by
2572 the time the triggered update would be sent. */
2573 if (rip->t_triggered_interval)
2574 {
2575 thread_cancel (rip->t_triggered_interval);
2576 rip->t_triggered_interval = NULL;
2577 }
2578 rip->trigger = 0;
2579
2580 /* Register myself. */
2581 rip_event (RIP_UPDATE_EVENT, 0);
2582
2583 return 0;
2584}
2585
2586/* Walk down the RIP routing table then clear changed flag. */
pauldc63bfd2005-10-25 23:31:05 +00002587static void
paul216565a2005-10-25 23:35:28 +00002588rip_clear_changed_flag (void)
paul718e3742002-12-13 20:15:29 +00002589{
2590 struct route_node *rp;
2591 struct rip_info *rinfo;
2592
2593 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2594 if ((rinfo = rp->info) != NULL)
2595 if (rinfo->flags & RIP_RTF_CHANGED)
2596 rinfo->flags &= ~RIP_RTF_CHANGED;
2597}
2598
2599/* Triggered update interval timer. */
pauldc63bfd2005-10-25 23:31:05 +00002600static int
paul718e3742002-12-13 20:15:29 +00002601rip_triggered_interval (struct thread *t)
2602{
2603 int rip_triggered_update (struct thread *);
2604
2605 rip->t_triggered_interval = NULL;
2606
2607 if (rip->trigger)
2608 {
2609 rip->trigger = 0;
2610 rip_triggered_update (t);
2611 }
2612 return 0;
2613}
2614
2615/* Execute triggered update. */
pauldc63bfd2005-10-25 23:31:05 +00002616static int
paul718e3742002-12-13 20:15:29 +00002617rip_triggered_update (struct thread *t)
2618{
2619 int interval;
2620
2621 /* Clear thred pointer. */
2622 rip->t_triggered_update = NULL;
2623
2624 /* Cancel interval timer. */
2625 if (rip->t_triggered_interval)
2626 {
2627 thread_cancel (rip->t_triggered_interval);
2628 rip->t_triggered_interval = NULL;
2629 }
2630 rip->trigger = 0;
2631
2632 /* Logging triggered update. */
2633 if (IS_RIP_DEBUG_EVENT)
ajs5d6c3772004-12-08 19:24:06 +00002634 zlog_debug ("triggered update!");
paul718e3742002-12-13 20:15:29 +00002635
2636 /* Split Horizon processing is done when generating triggered
2637 updates as well as normal updates (see section 2.6). */
2638 rip_update_process (rip_changed_route);
2639
2640 /* Once all of the triggered updates have been generated, the route
2641 change flags should be cleared. */
2642 rip_clear_changed_flag ();
2643
2644 /* After a triggered update is sent, a timer should be set for a
2645 random interval between 1 and 5 seconds. If other changes that
2646 would trigger updates occur before the timer expires, a single
2647 update is triggered when the timer expires. */
2648 interval = (random () % 5) + 1;
2649
2650 rip->t_triggered_interval =
2651 thread_add_timer (master, rip_triggered_interval, NULL, interval);
2652
2653 return 0;
2654}
2655
2656/* Withdraw redistributed route. */
2657void
2658rip_redistribute_withdraw (int type)
2659{
2660 struct route_node *rp;
2661 struct rip_info *rinfo;
2662
2663 if (!rip)
2664 return;
2665
2666 for (rp = route_top (rip->table); rp; rp = route_next (rp))
2667 if ((rinfo = rp->info) != NULL)
2668 {
2669 if (rinfo->type == type
2670 && rinfo->sub_type != RIP_ROUTE_INTERFACE)
2671 {
2672 /* Perform poisoned reverse. */
2673 rinfo->metric = RIP_METRIC_INFINITY;
2674 RIP_TIMER_ON (rinfo->t_garbage_collect,
2675 rip_garbage_collect, rip->garbage_time);
2676 RIP_TIMER_OFF (rinfo->t_timeout);
2677 rinfo->flags |= RIP_RTF_CHANGED;
2678
hasso16705132003-05-25 14:49:19 +00002679 if (IS_RIP_DEBUG_EVENT) {
2680 struct prefix_ipv4 *p = (struct prefix_ipv4 *) &rp->p;
2681
ajs5d6c3772004-12-08 19:24:06 +00002682 zlog_debug ("Poisone %s/%d on the interface %s with an infinity metric [withdraw]",
hasso16705132003-05-25 14:49:19 +00002683 inet_ntoa(p->prefix), p->prefixlen,
2684 ifindex2ifname(rinfo->ifindex));
2685 }
2686
paul718e3742002-12-13 20:15:29 +00002687 rip_event (RIP_TRIGGERED_UPDATE, 0);
2688 }
2689 }
2690}
2691
2692/* Create new RIP instance and set it to global variable. */
pauldc63bfd2005-10-25 23:31:05 +00002693static int
2694rip_create (void)
paul718e3742002-12-13 20:15:29 +00002695{
2696 rip = XMALLOC (MTYPE_RIP, sizeof (struct rip));
2697 memset (rip, 0, sizeof (struct rip));
2698
2699 /* Set initial value. */
paulf38a4712003-06-07 01:10:00 +00002700 rip->version_send = RI_RIP_VERSION_2;
2701 rip->version_recv = RI_RIP_VERSION_1_AND_2;
paul718e3742002-12-13 20:15:29 +00002702 rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
2703 rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
2704 rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
2705 rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
2706
2707 /* Initialize RIP routig table. */
2708 rip->table = route_table_init ();
2709 rip->route = route_table_init ();
2710 rip->neighbor = route_table_init ();
2711
2712 /* Make output stream. */
2713 rip->obuf = stream_new (1500);
2714
2715 /* Make socket. */
paulf69bd9d2005-06-03 18:01:50 +00002716 rip->sock = rip_create_socket (NULL);
paul718e3742002-12-13 20:15:29 +00002717 if (rip->sock < 0)
2718 return rip->sock;
2719
2720 /* Create read and timer thread. */
2721 rip_event (RIP_READ, rip->sock);
2722 rip_event (RIP_UPDATE_EVENT, 1);
2723
2724 return 0;
2725}
2726
2727/* Sned RIP request to the destination. */
2728int
2729rip_request_send (struct sockaddr_in *to, struct interface *ifp,
paul931cd542004-01-23 15:31:42 +00002730 u_char version, struct connected *connected)
paul718e3742002-12-13 20:15:29 +00002731{
2732 struct rte *rte;
2733 struct rip_packet rip_packet;
paul1eb8ef22005-04-07 07:30:20 +00002734 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00002735
2736 memset (&rip_packet, 0, sizeof (rip_packet));
2737
2738 rip_packet.command = RIP_REQUEST;
2739 rip_packet.version = version;
2740 rte = rip_packet.rte;
2741 rte->metric = htonl (RIP_METRIC_INFINITY);
2742
paul931cd542004-01-23 15:31:42 +00002743 if (connected)
2744 {
2745 /*
2746 * connected is only sent for ripv1 case, or when
2747 * interface does not support multicast. Caller loops
2748 * over each connected address for this case.
2749 */
paul11dde9c2004-05-31 14:00:00 +00002750 if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
paulc49ad8f2004-10-22 10:27:28 +00002751 to, connected) != sizeof (rip_packet))
paul931cd542004-01-23 15:31:42 +00002752 return -1;
2753 else
2754 return sizeof (rip_packet);
2755 }
2756
paulcc1131a2003-10-15 23:20:17 +00002757 /* send request on each connected network */
paul1eb8ef22005-04-07 07:30:20 +00002758 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, connected))
paulcc1131a2003-10-15 23:20:17 +00002759 {
2760 struct prefix_ipv4 *p;
2761
2762 p = (struct prefix_ipv4 *) connected->address;
2763
2764 if (p->family != AF_INET)
2765 continue;
2766
paul11dde9c2004-05-31 14:00:00 +00002767 if (rip_send_packet ((u_char *) &rip_packet, sizeof (rip_packet),
paulc49ad8f2004-10-22 10:27:28 +00002768 to, connected) != sizeof (rip_packet))
paulcc1131a2003-10-15 23:20:17 +00002769 return -1;
2770 }
2771 return sizeof (rip_packet);
paul718e3742002-12-13 20:15:29 +00002772}
2773
pauldc63bfd2005-10-25 23:31:05 +00002774static int
paul718e3742002-12-13 20:15:29 +00002775rip_update_jitter (unsigned long time)
2776{
paul239389b2004-05-05 14:09:37 +00002777#define JITTER_BOUND 4
2778 /* We want to get the jitter to +/- 1/JITTER_BOUND the interval.
2779 Given that, we cannot let time be less than JITTER_BOUND seconds.
2780 The RIPv2 RFC says jitter should be small compared to
2781 update_time. We consider 1/JITTER_BOUND to be small.
2782 */
2783
2784 int jitter_input = time;
2785 int jitter;
2786
2787 if (jitter_input < JITTER_BOUND)
2788 jitter_input = JITTER_BOUND;
2789
2790 jitter = (((rand () % ((jitter_input * 2) + 1)) - jitter_input));
2791
2792 return jitter/JITTER_BOUND;
paul718e3742002-12-13 20:15:29 +00002793}
2794
2795void
2796rip_event (enum rip_event event, int sock)
2797{
2798 int jitter = 0;
2799
2800 switch (event)
2801 {
2802 case RIP_READ:
2803 rip->t_read = thread_add_read (master, rip_read, NULL, sock);
2804 break;
2805 case RIP_UPDATE_EVENT:
2806 if (rip->t_update)
2807 {
2808 thread_cancel (rip->t_update);
2809 rip->t_update = NULL;
2810 }
2811 jitter = rip_update_jitter (rip->update_time);
2812 rip->t_update =
2813 thread_add_timer (master, rip_update, NULL,
2814 sock ? 2 : rip->update_time + jitter);
2815 break;
2816 case RIP_TRIGGERED_UPDATE:
2817 if (rip->t_triggered_interval)
2818 rip->trigger = 1;
2819 else if (! rip->t_triggered_update)
2820 rip->t_triggered_update =
2821 thread_add_event (master, rip_triggered_update, NULL, 0);
2822 break;
2823 default:
2824 break;
2825 }
2826}
2827
2828DEFUN (router_rip,
2829 router_rip_cmd,
2830 "router rip",
2831 "Enable a routing process\n"
2832 "Routing Information Protocol (RIP)\n")
2833{
2834 int ret;
2835
2836 /* If rip is not enabled before. */
2837 if (! rip)
2838 {
2839 ret = rip_create ();
2840 if (ret < 0)
2841 {
2842 zlog_info ("Can't create RIP");
2843 return CMD_WARNING;
2844 }
2845 }
2846 vty->node = RIP_NODE;
2847 vty->index = rip;
2848
2849 return CMD_SUCCESS;
2850}
2851
2852DEFUN (no_router_rip,
2853 no_router_rip_cmd,
2854 "no router rip",
2855 NO_STR
2856 "Enable a routing process\n"
2857 "Routing Information Protocol (RIP)\n")
2858{
2859 if (rip)
2860 rip_clean ();
2861 return CMD_SUCCESS;
2862}
2863
2864DEFUN (rip_version,
2865 rip_version_cmd,
2866 "version <1-2>",
2867 "Set routing protocol version\n"
2868 "version\n")
2869{
2870 int version;
2871
2872 version = atoi (argv[0]);
2873 if (version != RIPv1 && version != RIPv2)
2874 {
2875 vty_out (vty, "invalid rip version %d%s", version,
2876 VTY_NEWLINE);
2877 return CMD_WARNING;
2878 }
paulf38a4712003-06-07 01:10:00 +00002879 rip->version_send = version;
2880 rip->version_recv = version;
paul718e3742002-12-13 20:15:29 +00002881
2882 return CMD_SUCCESS;
2883}
2884
2885DEFUN (no_rip_version,
2886 no_rip_version_cmd,
2887 "no version",
2888 NO_STR
2889 "Set routing protocol version\n")
2890{
2891 /* Set RIP version to the default. */
paulf38a4712003-06-07 01:10:00 +00002892 rip->version_send = RI_RIP_VERSION_2;
2893 rip->version_recv = RI_RIP_VERSION_1_AND_2;
paul718e3742002-12-13 20:15:29 +00002894
2895 return CMD_SUCCESS;
2896}
2897
2898ALIAS (no_rip_version,
2899 no_rip_version_val_cmd,
2900 "no version <1-2>",
2901 NO_STR
2902 "Set routing protocol version\n"
2903 "version\n")
2904
2905DEFUN (rip_route,
2906 rip_route_cmd,
2907 "route A.B.C.D/M",
2908 "RIP static route configuration\n"
2909 "IP prefix <network>/<length>\n")
2910{
2911 int ret;
2912 struct prefix_ipv4 p;
2913 struct route_node *node;
2914
2915 ret = str2prefix_ipv4 (argv[0], &p);
2916 if (ret < 0)
2917 {
2918 vty_out (vty, "Malformed address%s", VTY_NEWLINE);
2919 return CMD_WARNING;
2920 }
2921 apply_mask_ipv4 (&p);
2922
2923 /* For router rip configuration. */
2924 node = route_node_get (rip->route, (struct prefix *) &p);
2925
2926 if (node->info)
2927 {
2928 vty_out (vty, "There is already same static route.%s", VTY_NEWLINE);
2929 route_unlock_node (node);
2930 return CMD_WARNING;
2931 }
2932
hasso8a676be2004-10-08 06:36:38 +00002933 node->info = (char *)"static";
paul718e3742002-12-13 20:15:29 +00002934
vincentfbf5d032005-09-29 11:25:50 +00002935 rip_redistribute_add (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0, NULL, 0, 0);
paul718e3742002-12-13 20:15:29 +00002936
2937 return CMD_SUCCESS;
2938}
2939
2940DEFUN (no_rip_route,
2941 no_rip_route_cmd,
2942 "no route A.B.C.D/M",
2943 NO_STR
2944 "RIP static route configuration\n"
2945 "IP prefix <network>/<length>\n")
2946{
2947 int ret;
2948 struct prefix_ipv4 p;
2949 struct route_node *node;
2950
2951 ret = str2prefix_ipv4 (argv[0], &p);
2952 if (ret < 0)
2953 {
2954 vty_out (vty, "Malformed address%s", VTY_NEWLINE);
2955 return CMD_WARNING;
2956 }
2957 apply_mask_ipv4 (&p);
2958
2959 /* For router rip configuration. */
2960 node = route_node_lookup (rip->route, (struct prefix *) &p);
2961 if (! node)
2962 {
2963 vty_out (vty, "Can't find route %s.%s", argv[0],
2964 VTY_NEWLINE);
2965 return CMD_WARNING;
2966 }
2967
2968 rip_redistribute_delete (ZEBRA_ROUTE_RIP, RIP_ROUTE_STATIC, &p, 0);
2969 route_unlock_node (node);
2970
2971 node->info = NULL;
2972 route_unlock_node (node);
2973
2974 return CMD_SUCCESS;
2975}
2976
pauldc63bfd2005-10-25 23:31:05 +00002977static void
paul216565a2005-10-25 23:35:28 +00002978rip_update_default_metric (void)
paul718e3742002-12-13 20:15:29 +00002979{
2980 struct route_node *np;
2981 struct rip_info *rinfo;
2982
2983 for (np = route_top (rip->table); np; np = route_next (np))
2984 if ((rinfo = np->info) != NULL)
2985 if (rinfo->type != ZEBRA_ROUTE_RIP && rinfo->type != ZEBRA_ROUTE_CONNECT)
2986 rinfo->metric = rip->default_metric;
2987}
2988
2989DEFUN (rip_default_metric,
2990 rip_default_metric_cmd,
2991 "default-metric <1-16>",
2992 "Set a metric of redistribute routes\n"
2993 "Default metric\n")
2994{
2995 if (rip)
2996 {
2997 rip->default_metric = atoi (argv[0]);
2998 /* rip_update_default_metric (); */
2999 }
3000 return CMD_SUCCESS;
3001}
3002
3003DEFUN (no_rip_default_metric,
3004 no_rip_default_metric_cmd,
3005 "no default-metric",
3006 NO_STR
3007 "Set a metric of redistribute routes\n"
3008 "Default metric\n")
3009{
3010 if (rip)
3011 {
3012 rip->default_metric = RIP_DEFAULT_METRIC_DEFAULT;
3013 /* rip_update_default_metric (); */
3014 }
3015 return CMD_SUCCESS;
3016}
3017
3018ALIAS (no_rip_default_metric,
3019 no_rip_default_metric_val_cmd,
3020 "no default-metric <1-16>",
3021 NO_STR
3022 "Set a metric of redistribute routes\n"
3023 "Default metric\n")
3024
3025DEFUN (rip_timers,
3026 rip_timers_cmd,
3027 "timers basic <5-2147483647> <5-2147483647> <5-2147483647>",
3028 "Adjust routing timers\n"
3029 "Basic routing protocol update timers\n"
3030 "Routing table update timer value in second. Default is 30.\n"
3031 "Routing information timeout timer. Default is 180.\n"
3032 "Garbage collection timer. Default is 120.\n")
3033{
3034 unsigned long update;
3035 unsigned long timeout;
3036 unsigned long garbage;
3037 char *endptr = NULL;
3038 unsigned long RIP_TIMER_MAX = 2147483647;
3039 unsigned long RIP_TIMER_MIN = 5;
3040
3041 update = strtoul (argv[0], &endptr, 10);
3042 if (update > RIP_TIMER_MAX || update < RIP_TIMER_MIN || *endptr != '\0')
3043 {
3044 vty_out (vty, "update timer value error%s", VTY_NEWLINE);
3045 return CMD_WARNING;
3046 }
3047
3048 timeout = strtoul (argv[1], &endptr, 10);
3049 if (timeout > RIP_TIMER_MAX || timeout < RIP_TIMER_MIN || *endptr != '\0')
3050 {
3051 vty_out (vty, "timeout timer value error%s", VTY_NEWLINE);
3052 return CMD_WARNING;
3053 }
3054
3055 garbage = strtoul (argv[2], &endptr, 10);
3056 if (garbage > RIP_TIMER_MAX || garbage < RIP_TIMER_MIN || *endptr != '\0')
3057 {
3058 vty_out (vty, "garbage timer value error%s", VTY_NEWLINE);
3059 return CMD_WARNING;
3060 }
3061
3062 /* Set each timer value. */
3063 rip->update_time = update;
3064 rip->timeout_time = timeout;
3065 rip->garbage_time = garbage;
3066
3067 /* Reset update timer thread. */
3068 rip_event (RIP_UPDATE_EVENT, 0);
3069
3070 return CMD_SUCCESS;
3071}
3072
3073DEFUN (no_rip_timers,
3074 no_rip_timers_cmd,
3075 "no timers basic",
3076 NO_STR
3077 "Adjust routing timers\n"
3078 "Basic routing protocol update timers\n")
3079{
3080 /* Set each timer value to the default. */
3081 rip->update_time = RIP_UPDATE_TIMER_DEFAULT;
3082 rip->timeout_time = RIP_TIMEOUT_TIMER_DEFAULT;
3083 rip->garbage_time = RIP_GARBAGE_TIMER_DEFAULT;
3084
3085 /* Reset update timer thread. */
3086 rip_event (RIP_UPDATE_EVENT, 0);
3087
3088 return CMD_SUCCESS;
3089}
hasso16705132003-05-25 14:49:19 +00003090
3091ALIAS (no_rip_timers,
3092 no_rip_timers_val_cmd,
3093 "no timers basic <0-65535> <0-65535> <0-65535>",
3094 NO_STR
3095 "Adjust routing timers\n"
3096 "Basic routing protocol update timers\n"
3097 "Routing table update timer value in second. Default is 30.\n"
3098 "Routing information timeout timer. Default is 180.\n"
3099 "Garbage collection timer. Default is 120.\n")
3100
paul718e3742002-12-13 20:15:29 +00003101
3102struct route_table *rip_distance_table;
3103
3104struct rip_distance
3105{
3106 /* Distance value for the IP source prefix. */
3107 u_char distance;
3108
3109 /* Name of the access-list to be matched. */
3110 char *access_list;
3111};
3112
pauldc63bfd2005-10-25 23:31:05 +00003113static struct rip_distance *
paul216565a2005-10-25 23:35:28 +00003114rip_distance_new (void)
paul718e3742002-12-13 20:15:29 +00003115{
3116 struct rip_distance *new;
3117 new = XMALLOC (MTYPE_RIP_DISTANCE, sizeof (struct rip_distance));
3118 memset (new, 0, sizeof (struct rip_distance));
3119 return new;
3120}
3121
pauldc63bfd2005-10-25 23:31:05 +00003122static void
paul718e3742002-12-13 20:15:29 +00003123rip_distance_free (struct rip_distance *rdistance)
3124{
3125 XFREE (MTYPE_RIP_DISTANCE, rdistance);
3126}
3127
pauldc63bfd2005-10-25 23:31:05 +00003128static int
hasso98b718a2004-10-11 12:57:57 +00003129rip_distance_set (struct vty *vty, const char *distance_str, const char *ip_str,
3130 const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00003131{
3132 int ret;
3133 struct prefix_ipv4 p;
3134 u_char distance;
3135 struct route_node *rn;
3136 struct rip_distance *rdistance;
3137
3138 ret = str2prefix_ipv4 (ip_str, &p);
3139 if (ret == 0)
3140 {
3141 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
3142 return CMD_WARNING;
3143 }
3144
3145 distance = atoi (distance_str);
3146
3147 /* Get RIP distance node. */
3148 rn = route_node_get (rip_distance_table, (struct prefix *) &p);
3149 if (rn->info)
3150 {
3151 rdistance = rn->info;
3152 route_unlock_node (rn);
3153 }
3154 else
3155 {
3156 rdistance = rip_distance_new ();
3157 rn->info = rdistance;
3158 }
3159
3160 /* Set distance value. */
3161 rdistance->distance = distance;
3162
3163 /* Reset access-list configuration. */
3164 if (rdistance->access_list)
3165 {
3166 free (rdistance->access_list);
3167 rdistance->access_list = NULL;
3168 }
3169 if (access_list_str)
3170 rdistance->access_list = strdup (access_list_str);
3171
3172 return CMD_SUCCESS;
3173}
3174
pauldc63bfd2005-10-25 23:31:05 +00003175static int
hasso98b718a2004-10-11 12:57:57 +00003176rip_distance_unset (struct vty *vty, const char *distance_str,
3177 const char *ip_str, const char *access_list_str)
paul718e3742002-12-13 20:15:29 +00003178{
3179 int ret;
3180 struct prefix_ipv4 p;
3181 u_char distance;
3182 struct route_node *rn;
3183 struct rip_distance *rdistance;
3184
3185 ret = str2prefix_ipv4 (ip_str, &p);
3186 if (ret == 0)
3187 {
3188 vty_out (vty, "Malformed prefix%s", VTY_NEWLINE);
3189 return CMD_WARNING;
3190 }
3191
3192 distance = atoi (distance_str);
3193
3194 rn = route_node_lookup (rip_distance_table, (struct prefix *)&p);
3195 if (! rn)
3196 {
3197 vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE);
3198 return CMD_WARNING;
3199 }
3200
3201 rdistance = rn->info;
3202
3203 if (rdistance->access_list)
3204 free (rdistance->access_list);
3205 rip_distance_free (rdistance);
3206
3207 rn->info = NULL;
3208 route_unlock_node (rn);
3209 route_unlock_node (rn);
3210
3211 return CMD_SUCCESS;
3212}
3213
pauldc63bfd2005-10-25 23:31:05 +00003214static void
paul216565a2005-10-25 23:35:28 +00003215rip_distance_reset (void)
paul718e3742002-12-13 20:15:29 +00003216{
3217 struct route_node *rn;
3218 struct rip_distance *rdistance;
3219
3220 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3221 if ((rdistance = rn->info) != NULL)
3222 {
3223 if (rdistance->access_list)
3224 free (rdistance->access_list);
3225 rip_distance_free (rdistance);
3226 rn->info = NULL;
3227 route_unlock_node (rn);
3228 }
3229}
3230
3231/* Apply RIP information to distance method. */
3232u_char
3233rip_distance_apply (struct rip_info *rinfo)
3234{
3235 struct route_node *rn;
3236 struct prefix_ipv4 p;
3237 struct rip_distance *rdistance;
3238 struct access_list *alist;
3239
3240 if (! rip)
3241 return 0;
3242
3243 memset (&p, 0, sizeof (struct prefix_ipv4));
3244 p.family = AF_INET;
3245 p.prefix = rinfo->from;
3246 p.prefixlen = IPV4_MAX_BITLEN;
3247
3248 /* Check source address. */
3249 rn = route_node_match (rip_distance_table, (struct prefix *) &p);
3250 if (rn)
3251 {
3252 rdistance = rn->info;
3253 route_unlock_node (rn);
3254
3255 if (rdistance->access_list)
3256 {
3257 alist = access_list_lookup (AFI_IP, rdistance->access_list);
3258 if (alist == NULL)
3259 return 0;
3260 if (access_list_apply (alist, &rinfo->rp->p) == FILTER_DENY)
3261 return 0;
3262
3263 return rdistance->distance;
3264 }
3265 else
3266 return rdistance->distance;
3267 }
3268
3269 if (rip->distance)
3270 return rip->distance;
3271
3272 return 0;
3273}
3274
pauldc63bfd2005-10-25 23:31:05 +00003275static void
paul718e3742002-12-13 20:15:29 +00003276rip_distance_show (struct vty *vty)
3277{
3278 struct route_node *rn;
3279 struct rip_distance *rdistance;
3280 int header = 1;
3281 char buf[BUFSIZ];
3282
3283 vty_out (vty, " Distance: (default is %d)%s",
3284 rip->distance ? rip->distance :ZEBRA_RIP_DISTANCE_DEFAULT,
3285 VTY_NEWLINE);
3286
3287 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3288 if ((rdistance = rn->info) != NULL)
3289 {
3290 if (header)
3291 {
3292 vty_out (vty, " Address Distance List%s",
3293 VTY_NEWLINE);
3294 header = 0;
3295 }
3296 sprintf (buf, "%s/%d", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen);
3297 vty_out (vty, " %-20s %4d %s%s",
3298 buf, rdistance->distance,
3299 rdistance->access_list ? rdistance->access_list : "",
3300 VTY_NEWLINE);
3301 }
3302}
3303
3304DEFUN (rip_distance,
3305 rip_distance_cmd,
3306 "distance <1-255>",
3307 "Administrative distance\n"
3308 "Distance value\n")
3309{
3310 rip->distance = atoi (argv[0]);
3311 return CMD_SUCCESS;
3312}
3313
3314DEFUN (no_rip_distance,
3315 no_rip_distance_cmd,
3316 "no distance <1-255>",
3317 NO_STR
3318 "Administrative distance\n"
3319 "Distance value\n")
3320{
3321 rip->distance = 0;
3322 return CMD_SUCCESS;
3323}
3324
3325DEFUN (rip_distance_source,
3326 rip_distance_source_cmd,
3327 "distance <1-255> A.B.C.D/M",
3328 "Administrative distance\n"
3329 "Distance value\n"
3330 "IP source prefix\n")
3331{
3332 rip_distance_set (vty, argv[0], argv[1], NULL);
3333 return CMD_SUCCESS;
3334}
3335
3336DEFUN (no_rip_distance_source,
3337 no_rip_distance_source_cmd,
3338 "no distance <1-255> A.B.C.D/M",
3339 NO_STR
3340 "Administrative distance\n"
3341 "Distance value\n"
3342 "IP source prefix\n")
3343{
3344 rip_distance_unset (vty, argv[0], argv[1], NULL);
3345 return CMD_SUCCESS;
3346}
3347
3348DEFUN (rip_distance_source_access_list,
3349 rip_distance_source_access_list_cmd,
3350 "distance <1-255> A.B.C.D/M WORD",
3351 "Administrative distance\n"
3352 "Distance value\n"
3353 "IP source prefix\n"
3354 "Access list name\n")
3355{
3356 rip_distance_set (vty, argv[0], argv[1], argv[2]);
3357 return CMD_SUCCESS;
3358}
3359
3360DEFUN (no_rip_distance_source_access_list,
3361 no_rip_distance_source_access_list_cmd,
3362 "no distance <1-255> A.B.C.D/M WORD",
3363 NO_STR
3364 "Administrative distance\n"
3365 "Distance value\n"
3366 "IP source prefix\n"
3367 "Access list name\n")
3368{
3369 rip_distance_unset (vty, argv[0], argv[1], argv[2]);
3370 return CMD_SUCCESS;
3371}
3372
3373/* Print out routes update time. */
pauldc63bfd2005-10-25 23:31:05 +00003374static void
paul718e3742002-12-13 20:15:29 +00003375rip_vty_out_uptime (struct vty *vty, struct rip_info *rinfo)
3376{
3377 struct timeval timer_now;
3378 time_t clock;
3379 struct tm *tm;
3380#define TIME_BUF 25
3381 char timebuf [TIME_BUF];
3382 struct thread *thread;
3383
3384 gettimeofday (&timer_now, NULL);
3385
3386 if ((thread = rinfo->t_timeout) != NULL)
3387 {
3388 clock = thread->u.sands.tv_sec - timer_now.tv_sec;
3389 tm = gmtime (&clock);
3390 strftime (timebuf, TIME_BUF, "%M:%S", tm);
3391 vty_out (vty, "%5s", timebuf);
3392 }
3393 else if ((thread = rinfo->t_garbage_collect) != NULL)
3394 {
3395 clock = thread->u.sands.tv_sec - timer_now.tv_sec;
3396 tm = gmtime (&clock);
3397 strftime (timebuf, TIME_BUF, "%M:%S", tm);
3398 vty_out (vty, "%5s", timebuf);
3399 }
3400}
3401
pauldc63bfd2005-10-25 23:31:05 +00003402static const char *
paul718e3742002-12-13 20:15:29 +00003403rip_route_type_print (int sub_type)
3404{
3405 switch (sub_type)
3406 {
3407 case RIP_ROUTE_RTE:
3408 return "n";
3409 case RIP_ROUTE_STATIC:
3410 return "s";
3411 case RIP_ROUTE_DEFAULT:
3412 return "d";
3413 case RIP_ROUTE_REDISTRIBUTE:
3414 return "r";
3415 case RIP_ROUTE_INTERFACE:
3416 return "i";
3417 default:
3418 return "?";
3419 }
3420}
3421
3422DEFUN (show_ip_rip,
3423 show_ip_rip_cmd,
3424 "show ip rip",
3425 SHOW_STR
3426 IP_STR
3427 "Show RIP routes\n")
3428{
3429 struct route_node *np;
3430 struct rip_info *rinfo;
3431
3432 if (! rip)
3433 return CMD_SUCCESS;
3434
hasso16705132003-05-25 14:49:19 +00003435 vty_out (vty, "Codes: R - RIP, C - connected, S - Static, O - OSPF, B - BGP%s"
3436 "Sub-codes:%s"
3437 " (n) - normal, (s) - static, (d) - default, (r) - redistribute,%s"
paul718e3742002-12-13 20:15:29 +00003438 " (i) - interface%s%s"
hassoa1455d82004-03-03 19:36:24 +00003439 " Network Next Hop Metric From Tag Time%s",
hasso16705132003-05-25 14:49:19 +00003440 VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00003441
3442 for (np = route_top (rip->table); np; np = route_next (np))
3443 if ((rinfo = np->info) != NULL)
3444 {
3445 int len;
3446
ajsf52d13c2005-10-01 17:38:06 +00003447 len = vty_out (vty, "%c(%s) %s/%d",
paul718e3742002-12-13 20:15:29 +00003448 /* np->lock, For debugging. */
ajsf52d13c2005-10-01 17:38:06 +00003449 zebra_route_char(rinfo->type),
paul718e3742002-12-13 20:15:29 +00003450 rip_route_type_print (rinfo->sub_type),
3451 inet_ntoa (np->p.u.prefix4), np->p.prefixlen);
3452
hassoa1455d82004-03-03 19:36:24 +00003453 len = 24 - len;
paul718e3742002-12-13 20:15:29 +00003454
3455 if (len > 0)
3456 vty_out (vty, "%*s", len, " ");
3457
3458 if (rinfo->nexthop.s_addr)
3459 vty_out (vty, "%-20s %2d ", inet_ntoa (rinfo->nexthop),
3460 rinfo->metric);
3461 else
3462 vty_out (vty, "0.0.0.0 %2d ", rinfo->metric);
3463
3464 /* Route which exist in kernel routing table. */
3465 if ((rinfo->type == ZEBRA_ROUTE_RIP) &&
3466 (rinfo->sub_type == RIP_ROUTE_RTE))
3467 {
3468 vty_out (vty, "%-15s ", inet_ntoa (rinfo->from));
hasso16705132003-05-25 14:49:19 +00003469 vty_out (vty, "%3d ", rinfo->tag);
paul718e3742002-12-13 20:15:29 +00003470 rip_vty_out_uptime (vty, rinfo);
3471 }
3472 else if (rinfo->metric == RIP_METRIC_INFINITY)
3473 {
3474 vty_out (vty, "self ");
hasso16705132003-05-25 14:49:19 +00003475 vty_out (vty, "%3d ", rinfo->tag);
paul718e3742002-12-13 20:15:29 +00003476 rip_vty_out_uptime (vty, rinfo);
3477 }
3478 else
hasso16705132003-05-25 14:49:19 +00003479 {
vincentfbf5d032005-09-29 11:25:50 +00003480 if (rinfo->external_metric)
3481 {
3482 len = vty_out (vty, "self (%s:%d)",
ajsf52d13c2005-10-01 17:38:06 +00003483 zebra_route_string(rinfo->type),
vincentfbf5d032005-09-29 11:25:50 +00003484 rinfo->external_metric);
3485 len = 16 - len;
3486 if (len > 0)
3487 vty_out (vty, "%*s", len, " ");
3488 }
3489 else
3490 vty_out (vty, "self ");
hasso16705132003-05-25 14:49:19 +00003491 vty_out (vty, "%3d", rinfo->tag);
3492 }
paul718e3742002-12-13 20:15:29 +00003493
3494 vty_out (vty, "%s", VTY_NEWLINE);
3495 }
3496 return CMD_SUCCESS;
3497}
3498
3499/* Return next event time. */
pauldc63bfd2005-10-25 23:31:05 +00003500static int
paul718e3742002-12-13 20:15:29 +00003501rip_next_thread_timer (struct thread *thread)
3502{
3503 struct timeval timer_now;
3504
3505 gettimeofday (&timer_now, NULL);
3506
3507 return thread->u.sands.tv_sec - timer_now.tv_sec;
3508}
3509
hasso16705132003-05-25 14:49:19 +00003510/* Vincent: formerly, it was show_ip_protocols_rip: "show ip protocols" */
3511DEFUN (show_ip_rip_status,
3512 show_ip_rip_status_cmd,
3513 "show ip rip status",
paul718e3742002-12-13 20:15:29 +00003514 SHOW_STR
3515 IP_STR
hasso16705132003-05-25 14:49:19 +00003516 "Show RIP routes\n"
paul718e3742002-12-13 20:15:29 +00003517 "IP routing protocol process parameters and statistics\n")
3518{
hasso52dc7ee2004-09-23 19:18:23 +00003519 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00003520 struct interface *ifp;
3521 struct rip_interface *ri;
3522 extern struct message ri_version_msg[];
hasso8a676be2004-10-08 06:36:38 +00003523 const char *send_version;
3524 const char *receive_version;
paul718e3742002-12-13 20:15:29 +00003525
3526 if (! rip)
3527 return CMD_SUCCESS;
3528
3529 vty_out (vty, "Routing Protocol is \"rip\"%s", VTY_NEWLINE);
3530 vty_out (vty, " Sending updates every %ld seconds with +/-50%%,",
3531 rip->update_time);
3532 vty_out (vty, " next due in %d seconds%s",
3533 rip_next_thread_timer (rip->t_update),
3534 VTY_NEWLINE);
3535 vty_out (vty, " Timeout after %ld seconds,", rip->timeout_time);
3536 vty_out (vty, " garbage collect after %ld seconds%s", rip->garbage_time,
3537 VTY_NEWLINE);
3538
3539 /* Filtering status show. */
3540 config_show_distribute (vty);
3541
3542 /* Default metric information. */
3543 vty_out (vty, " Default redistribution metric is %d%s",
3544 rip->default_metric, VTY_NEWLINE);
3545
3546 /* Redistribute information. */
3547 vty_out (vty, " Redistributing:");
3548 config_write_rip_redistribute (vty, 0);
3549 vty_out (vty, "%s", VTY_NEWLINE);
3550
paulf38a4712003-06-07 01:10:00 +00003551 vty_out (vty, " Default version control: send version %s,",
3552 lookup(ri_version_msg,rip->version_send));
3553 if (rip->version_recv == RI_RIP_VERSION_1_AND_2)
3554 vty_out (vty, " receive any version %s", VTY_NEWLINE);
3555 else
3556 vty_out (vty, " receive version %s %s",
3557 lookup(ri_version_msg,rip->version_recv), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00003558
3559 vty_out (vty, " Interface Send Recv Key-chain%s", VTY_NEWLINE);
3560
paul1eb8ef22005-04-07 07:30:20 +00003561 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +00003562 {
paul718e3742002-12-13 20:15:29 +00003563 ri = ifp->info;
3564
3565 if (ri->enable_network || ri->enable_interface)
3566 {
3567 if (ri->ri_send == RI_RIP_UNSPEC)
paulf38a4712003-06-07 01:10:00 +00003568 send_version = lookup (ri_version_msg, rip->version_send);
paul718e3742002-12-13 20:15:29 +00003569 else
3570 send_version = lookup (ri_version_msg, ri->ri_send);
3571
3572 if (ri->ri_receive == RI_RIP_UNSPEC)
paulf38a4712003-06-07 01:10:00 +00003573 receive_version = lookup (ri_version_msg, rip->version_recv);
paul718e3742002-12-13 20:15:29 +00003574 else
3575 receive_version = lookup (ri_version_msg, ri->ri_receive);
3576
3577 vty_out (vty, " %-17s%-3s %-3s %s%s", ifp->name,
3578 send_version,
3579 receive_version,
3580 ri->key_chain ? ri->key_chain : "",
3581 VTY_NEWLINE);
3582 }
3583 }
3584
3585 vty_out (vty, " Routing for Networks:%s", VTY_NEWLINE);
3586 config_write_rip_network (vty, 0);
3587
paul4aaff3f2003-06-07 01:04:45 +00003588 {
3589 int found_passive = 0;
paul1eb8ef22005-04-07 07:30:20 +00003590 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul4aaff3f2003-06-07 01:04:45 +00003591 {
paul4aaff3f2003-06-07 01:04:45 +00003592 ri = ifp->info;
3593
3594 if ((ri->enable_network || ri->enable_interface) && ri->passive)
3595 {
3596 if (!found_passive)
3597 {
3598 vty_out (vty, " Passive Interface(s):%s", VTY_NEWLINE);
3599 found_passive = 1;
3600 }
3601 vty_out (vty, " %s%s", ifp->name, VTY_NEWLINE);
3602 }
3603 }
3604 }
3605
paul718e3742002-12-13 20:15:29 +00003606 vty_out (vty, " Routing Information Sources:%s", VTY_NEWLINE);
3607 vty_out (vty, " Gateway BadPackets BadRoutes Distance Last Update%s", VTY_NEWLINE);
3608 rip_peer_display (vty);
3609
3610 rip_distance_show (vty);
3611
3612 return CMD_SUCCESS;
3613}
3614
3615/* RIP configuration write function. */
pauldc63bfd2005-10-25 23:31:05 +00003616static int
paul718e3742002-12-13 20:15:29 +00003617config_write_rip (struct vty *vty)
3618{
3619 int write = 0;
3620 struct route_node *rn;
3621 struct rip_distance *rdistance;
3622
3623 if (rip)
3624 {
3625 /* Router RIP statement. */
3626 vty_out (vty, "router rip%s", VTY_NEWLINE);
3627 write++;
3628
3629 /* RIP version statement. Default is RIP version 2. */
paulf38a4712003-06-07 01:10:00 +00003630 if (rip->version_send != RI_RIP_VERSION_2
3631 || rip->version_recv != RI_RIP_VERSION_1_AND_2)
3632 vty_out (vty, " version %d%s", rip->version_send,
paul718e3742002-12-13 20:15:29 +00003633 VTY_NEWLINE);
3634
3635 /* RIP timer configuration. */
3636 if (rip->update_time != RIP_UPDATE_TIMER_DEFAULT
3637 || rip->timeout_time != RIP_TIMEOUT_TIMER_DEFAULT
3638 || rip->garbage_time != RIP_GARBAGE_TIMER_DEFAULT)
3639 vty_out (vty, " timers basic %lu %lu %lu%s",
3640 rip->update_time,
3641 rip->timeout_time,
3642 rip->garbage_time,
3643 VTY_NEWLINE);
3644
3645 /* Default information configuration. */
3646 if (rip->default_information)
3647 {
3648 if (rip->default_information_route_map)
3649 vty_out (vty, " default-information originate route-map %s%s",
3650 rip->default_information_route_map, VTY_NEWLINE);
3651 else
3652 vty_out (vty, " default-information originate%s",
3653 VTY_NEWLINE);
3654 }
3655
3656 /* Redistribute configuration. */
3657 config_write_rip_redistribute (vty, 1);
3658
3659 /* RIP offset-list configuration. */
3660 config_write_rip_offset_list (vty);
3661
3662 /* RIP enabled network and interface configuration. */
3663 config_write_rip_network (vty, 1);
3664
3665 /* RIP default metric configuration */
3666 if (rip->default_metric != RIP_DEFAULT_METRIC_DEFAULT)
3667 vty_out (vty, " default-metric %d%s",
3668 rip->default_metric, VTY_NEWLINE);
3669
3670 /* Distribute configuration. */
3671 write += config_write_distribute (vty);
3672
hasso16705132003-05-25 14:49:19 +00003673 /* Interface routemap configuration */
3674 write += config_write_if_rmap (vty);
3675
paul718e3742002-12-13 20:15:29 +00003676 /* Distance configuration. */
3677 if (rip->distance)
3678 vty_out (vty, " distance %d%s", rip->distance, VTY_NEWLINE);
3679
3680 /* RIP source IP prefix distance configuration. */
3681 for (rn = route_top (rip_distance_table); rn; rn = route_next (rn))
3682 if ((rdistance = rn->info) != NULL)
3683 vty_out (vty, " distance %d %s/%d %s%s", rdistance->distance,
3684 inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
3685 rdistance->access_list ? rdistance->access_list : "",
3686 VTY_NEWLINE);
3687
3688 /* RIP static route configuration. */
3689 for (rn = route_top (rip->route); rn; rn = route_next (rn))
3690 if (rn->info)
3691 vty_out (vty, " route %s/%d%s",
3692 inet_ntoa (rn->p.u.prefix4),
3693 rn->p.prefixlen,
3694 VTY_NEWLINE);
3695
3696 }
3697 return write;
3698}
3699
3700/* RIP node structure. */
3701struct cmd_node rip_node =
3702{
3703 RIP_NODE,
3704 "%s(config-router)# ",
3705 1
3706};
3707
3708/* Distribute-list update functions. */
pauldc63bfd2005-10-25 23:31:05 +00003709static void
paul718e3742002-12-13 20:15:29 +00003710rip_distribute_update (struct distribute *dist)
3711{
3712 struct interface *ifp;
3713 struct rip_interface *ri;
3714 struct access_list *alist;
3715 struct prefix_list *plist;
3716
3717 if (! dist->ifname)
3718 return;
3719
3720 ifp = if_lookup_by_name (dist->ifname);
3721 if (ifp == NULL)
3722 return;
3723
3724 ri = ifp->info;
3725
3726 if (dist->list[DISTRIBUTE_IN])
3727 {
3728 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_IN]);
3729 if (alist)
3730 ri->list[RIP_FILTER_IN] = alist;
3731 else
3732 ri->list[RIP_FILTER_IN] = NULL;
3733 }
3734 else
3735 ri->list[RIP_FILTER_IN] = NULL;
3736
3737 if (dist->list[DISTRIBUTE_OUT])
3738 {
3739 alist = access_list_lookup (AFI_IP, dist->list[DISTRIBUTE_OUT]);
3740 if (alist)
3741 ri->list[RIP_FILTER_OUT] = alist;
3742 else
3743 ri->list[RIP_FILTER_OUT] = NULL;
3744 }
3745 else
3746 ri->list[RIP_FILTER_OUT] = NULL;
3747
3748 if (dist->prefix[DISTRIBUTE_IN])
3749 {
3750 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_IN]);
3751 if (plist)
3752 ri->prefix[RIP_FILTER_IN] = plist;
3753 else
3754 ri->prefix[RIP_FILTER_IN] = NULL;
3755 }
3756 else
3757 ri->prefix[RIP_FILTER_IN] = NULL;
3758
3759 if (dist->prefix[DISTRIBUTE_OUT])
3760 {
3761 plist = prefix_list_lookup (AFI_IP, dist->prefix[DISTRIBUTE_OUT]);
3762 if (plist)
3763 ri->prefix[RIP_FILTER_OUT] = plist;
3764 else
3765 ri->prefix[RIP_FILTER_OUT] = NULL;
3766 }
3767 else
3768 ri->prefix[RIP_FILTER_OUT] = NULL;
3769}
3770
3771void
3772rip_distribute_update_interface (struct interface *ifp)
3773{
3774 struct distribute *dist;
3775
3776 dist = distribute_lookup (ifp->name);
3777 if (dist)
3778 rip_distribute_update (dist);
3779}
3780
3781/* Update all interface's distribute list. */
paul02ff83c2004-06-11 11:27:03 +00003782/* ARGSUSED */
pauldc63bfd2005-10-25 23:31:05 +00003783static void
paul02ff83c2004-06-11 11:27:03 +00003784rip_distribute_update_all (struct prefix_list *notused)
paul718e3742002-12-13 20:15:29 +00003785{
3786 struct interface *ifp;
paul1eb8ef22005-04-07 07:30:20 +00003787 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00003788
paul1eb8ef22005-04-07 07:30:20 +00003789 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
3790 rip_distribute_update_interface (ifp);
paul718e3742002-12-13 20:15:29 +00003791}
paul11dde9c2004-05-31 14:00:00 +00003792/* ARGSUSED */
pauldc63bfd2005-10-25 23:31:05 +00003793static void
paul11dde9c2004-05-31 14:00:00 +00003794rip_distribute_update_all_wrapper(struct access_list *notused)
3795{
paul02ff83c2004-06-11 11:27:03 +00003796 rip_distribute_update_all(NULL);
paul11dde9c2004-05-31 14:00:00 +00003797}
paul718e3742002-12-13 20:15:29 +00003798
3799/* Delete all added rip route. */
3800void
paul216565a2005-10-25 23:35:28 +00003801rip_clean (void)
paul718e3742002-12-13 20:15:29 +00003802{
3803 int i;
3804 struct route_node *rp;
3805 struct rip_info *rinfo;
3806
3807 if (rip)
3808 {
3809 /* Clear RIP routes */
3810 for (rp = route_top (rip->table); rp; rp = route_next (rp))
3811 if ((rinfo = rp->info) != NULL)
3812 {
3813 if (rinfo->type == ZEBRA_ROUTE_RIP &&
3814 rinfo->sub_type == RIP_ROUTE_RTE)
3815 rip_zebra_ipv4_delete ((struct prefix_ipv4 *)&rp->p,
3816 &rinfo->nexthop, rinfo->metric);
3817
3818 RIP_TIMER_OFF (rinfo->t_timeout);
3819 RIP_TIMER_OFF (rinfo->t_garbage_collect);
3820
3821 rp->info = NULL;
3822 route_unlock_node (rp);
3823
3824 rip_info_free (rinfo);
3825 }
3826
3827 /* Cancel RIP related timers. */
3828 RIP_TIMER_OFF (rip->t_update);
3829 RIP_TIMER_OFF (rip->t_triggered_update);
3830 RIP_TIMER_OFF (rip->t_triggered_interval);
3831
3832 /* Cancel read thread. */
3833 if (rip->t_read)
3834 {
3835 thread_cancel (rip->t_read);
3836 rip->t_read = NULL;
3837 }
3838
3839 /* Close RIP socket. */
3840 if (rip->sock >= 0)
3841 {
3842 close (rip->sock);
3843 rip->sock = -1;
3844 }
3845
3846 /* Static RIP route configuration. */
3847 for (rp = route_top (rip->route); rp; rp = route_next (rp))
3848 if (rp->info)
3849 {
3850 rp->info = NULL;
3851 route_unlock_node (rp);
3852 }
3853
3854 /* RIP neighbor configuration. */
3855 for (rp = route_top (rip->neighbor); rp; rp = route_next (rp))
3856 if (rp->info)
3857 {
3858 rp->info = NULL;
3859 route_unlock_node (rp);
3860 }
3861
3862 /* Redistribute related clear. */
3863 if (rip->default_information_route_map)
3864 free (rip->default_information_route_map);
3865
3866 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3867 if (rip->route_map[i].name)
3868 free (rip->route_map[i].name);
3869
3870 XFREE (MTYPE_ROUTE_TABLE, rip->table);
3871 XFREE (MTYPE_ROUTE_TABLE, rip->route);
3872 XFREE (MTYPE_ROUTE_TABLE, rip->neighbor);
3873
3874 XFREE (MTYPE_RIP, rip);
3875 rip = NULL;
3876 }
3877
3878 rip_clean_network ();
paul4aaff3f2003-06-07 01:04:45 +00003879 rip_passive_nondefault_clean ();
paul718e3742002-12-13 20:15:29 +00003880 rip_offset_clean ();
3881 rip_interface_clean ();
3882 rip_distance_reset ();
3883 rip_redistribute_clean ();
3884}
3885
3886/* Reset all values to the default settings. */
3887void
paul216565a2005-10-25 23:35:28 +00003888rip_reset (void)
paul718e3742002-12-13 20:15:29 +00003889{
3890 /* Reset global counters. */
3891 rip_global_route_changes = 0;
3892 rip_global_queries = 0;
3893
3894 /* Call ripd related reset functions. */
3895 rip_debug_reset ();
3896 rip_route_map_reset ();
3897
3898 /* Call library reset functions. */
3899 vty_reset ();
3900 access_list_reset ();
3901 prefix_list_reset ();
3902
3903 distribute_list_reset ();
3904
3905 rip_interface_reset ();
3906 rip_distance_reset ();
3907
3908 rip_zclient_reset ();
3909}
3910
pauldc63bfd2005-10-25 23:31:05 +00003911static void
hasso16705132003-05-25 14:49:19 +00003912rip_if_rmap_update (struct if_rmap *if_rmap)
3913{
3914 struct interface *ifp;
3915 struct rip_interface *ri;
3916 struct route_map *rmap;
3917
3918 ifp = if_lookup_by_name (if_rmap->ifname);
3919 if (ifp == NULL)
3920 return;
3921
3922 ri = ifp->info;
3923
3924 if (if_rmap->routemap[IF_RMAP_IN])
3925 {
3926 rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_IN]);
3927 if (rmap)
3928 ri->routemap[IF_RMAP_IN] = rmap;
3929 else
3930 ri->routemap[IF_RMAP_IN] = NULL;
3931 }
3932 else
3933 ri->routemap[RIP_FILTER_IN] = NULL;
3934
3935 if (if_rmap->routemap[IF_RMAP_OUT])
3936 {
3937 rmap = route_map_lookup_by_name (if_rmap->routemap[IF_RMAP_OUT]);
3938 if (rmap)
3939 ri->routemap[IF_RMAP_OUT] = rmap;
3940 else
3941 ri->routemap[IF_RMAP_OUT] = NULL;
3942 }
3943 else
3944 ri->routemap[RIP_FILTER_OUT] = NULL;
3945}
3946
3947void
3948rip_if_rmap_update_interface (struct interface *ifp)
3949{
3950 struct if_rmap *if_rmap;
3951
3952 if_rmap = if_rmap_lookup (ifp->name);
3953 if (if_rmap)
3954 rip_if_rmap_update (if_rmap);
3955}
3956
pauldc63bfd2005-10-25 23:31:05 +00003957static void
hasso16705132003-05-25 14:49:19 +00003958rip_routemap_update_redistribute (void)
3959{
3960 int i;
3961
3962 if (rip)
3963 {
3964 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
3965 {
3966 if (rip->route_map[i].name)
3967 rip->route_map[i].map =
3968 route_map_lookup_by_name (rip->route_map[i].name);
3969 }
3970 }
3971}
3972
paul11dde9c2004-05-31 14:00:00 +00003973/* ARGSUSED */
pauldc63bfd2005-10-25 23:31:05 +00003974static void
hasso98b718a2004-10-11 12:57:57 +00003975rip_routemap_update (const char *notused)
hasso16705132003-05-25 14:49:19 +00003976{
3977 struct interface *ifp;
paul1eb8ef22005-04-07 07:30:20 +00003978 struct listnode *node, *nnode;
hasso16705132003-05-25 14:49:19 +00003979
paul1eb8ef22005-04-07 07:30:20 +00003980 for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
3981 rip_if_rmap_update_interface (ifp);
hasso16705132003-05-25 14:49:19 +00003982
3983 rip_routemap_update_redistribute ();
3984}
3985
paul718e3742002-12-13 20:15:29 +00003986/* Allocate new rip structure and set default value. */
3987void
pauldc63bfd2005-10-25 23:31:05 +00003988rip_init (void)
paul718e3742002-12-13 20:15:29 +00003989{
3990 /* Randomize for triggered update random(). */
3991 srand (time (NULL));
3992
3993 /* Install top nodes. */
3994 install_node (&rip_node, config_write_rip);
3995
3996 /* Install rip commands. */
3997 install_element (VIEW_NODE, &show_ip_rip_cmd);
hasso16705132003-05-25 14:49:19 +00003998 install_element (VIEW_NODE, &show_ip_rip_status_cmd);
paul718e3742002-12-13 20:15:29 +00003999 install_element (ENABLE_NODE, &show_ip_rip_cmd);
hasso16705132003-05-25 14:49:19 +00004000 install_element (ENABLE_NODE, &show_ip_rip_status_cmd);
paul718e3742002-12-13 20:15:29 +00004001 install_element (CONFIG_NODE, &router_rip_cmd);
4002 install_element (CONFIG_NODE, &no_router_rip_cmd);
4003
4004 install_default (RIP_NODE);
4005 install_element (RIP_NODE, &rip_version_cmd);
4006 install_element (RIP_NODE, &no_rip_version_cmd);
4007 install_element (RIP_NODE, &no_rip_version_val_cmd);
4008 install_element (RIP_NODE, &rip_default_metric_cmd);
4009 install_element (RIP_NODE, &no_rip_default_metric_cmd);
4010 install_element (RIP_NODE, &no_rip_default_metric_val_cmd);
4011 install_element (RIP_NODE, &rip_timers_cmd);
4012 install_element (RIP_NODE, &no_rip_timers_cmd);
hasso16705132003-05-25 14:49:19 +00004013 install_element (RIP_NODE, &no_rip_timers_val_cmd);
paul718e3742002-12-13 20:15:29 +00004014 install_element (RIP_NODE, &rip_route_cmd);
4015 install_element (RIP_NODE, &no_rip_route_cmd);
4016 install_element (RIP_NODE, &rip_distance_cmd);
4017 install_element (RIP_NODE, &no_rip_distance_cmd);
4018 install_element (RIP_NODE, &rip_distance_source_cmd);
4019 install_element (RIP_NODE, &no_rip_distance_source_cmd);
4020 install_element (RIP_NODE, &rip_distance_source_access_list_cmd);
4021 install_element (RIP_NODE, &no_rip_distance_source_access_list_cmd);
4022
4023 /* Debug related init. */
4024 rip_debug_init ();
4025
paul718e3742002-12-13 20:15:29 +00004026 /* SNMP init. */
4027#ifdef HAVE_SNMP
4028 rip_snmp_init ();
4029#endif /* HAVE_SNMP */
4030
4031 /* Access list install. */
4032 access_list_init ();
paul11dde9c2004-05-31 14:00:00 +00004033 access_list_add_hook (rip_distribute_update_all_wrapper);
4034 access_list_delete_hook (rip_distribute_update_all_wrapper);
paul718e3742002-12-13 20:15:29 +00004035
4036 /* Prefix list initialize.*/
4037 prefix_list_init ();
4038 prefix_list_add_hook (rip_distribute_update_all);
4039 prefix_list_delete_hook (rip_distribute_update_all);
4040
4041 /* Distribute list install. */
4042 distribute_list_init (RIP_NODE);
4043 distribute_list_add_hook (rip_distribute_update);
4044 distribute_list_delete_hook (rip_distribute_update);
4045
hasso16705132003-05-25 14:49:19 +00004046 /* Route-map */
4047 rip_route_map_init ();
4048 rip_offset_init ();
4049
4050 route_map_add_hook (rip_routemap_update);
4051 route_map_delete_hook (rip_routemap_update);
4052
4053 if_rmap_init (RIP_NODE);
4054 if_rmap_hook_add (rip_if_rmap_update);
4055 if_rmap_hook_delete (rip_if_rmap_update);
4056
paul718e3742002-12-13 20:15:29 +00004057 /* Distance control. */
4058 rip_distance_table = route_table_init ();
4059}