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