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