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