paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Zebra connect library for OSPFd |
| 3 | * Copyright (C) 1997, 98, 99, 2000 Kunihiro Ishiguro, Toshiaki Takada |
| 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 |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | * Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "thread.h" |
| 26 | #include "command.h" |
| 27 | #include "network.h" |
| 28 | #include "prefix.h" |
| 29 | #include "routemap.h" |
| 30 | #include "table.h" |
| 31 | #include "stream.h" |
| 32 | #include "memory.h" |
| 33 | #include "zclient.h" |
| 34 | #include "filter.h" |
| 35 | #include "log.h" |
| 36 | |
| 37 | #include "ospfd/ospfd.h" |
| 38 | #include "ospfd/ospf_interface.h" |
| 39 | #include "ospfd/ospf_ism.h" |
| 40 | #include "ospfd/ospf_asbr.h" |
| 41 | #include "ospfd/ospf_asbr.h" |
| 42 | #include "ospfd/ospf_abr.h" |
| 43 | #include "ospfd/ospf_lsa.h" |
| 44 | #include "ospfd/ospf_dump.h" |
| 45 | #include "ospfd/ospf_route.h" |
| 46 | #include "ospfd/ospf_zebra.h" |
| 47 | #ifdef HAVE_SNMP |
| 48 | #include "ospfd/ospf_snmp.h" |
| 49 | #endif /* HAVE_SNMP */ |
| 50 | |
| 51 | /* Zebra structure to hold current status. */ |
| 52 | struct zclient *zclient = NULL; |
| 53 | |
| 54 | /* For registering threads. */ |
| 55 | extern struct thread_master *master; |
| 56 | |
| 57 | /* Inteface addition message from zebra. */ |
| 58 | int |
| 59 | ospf_interface_add (int command, struct zclient *zclient, zebra_size_t length) |
| 60 | { |
| 61 | struct interface *ifp; |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 62 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | |
| 64 | ifp = zebra_interface_add_read (zclient->ibuf); |
| 65 | |
| 66 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
| 67 | zlog_info ("Zebra: interface add %s index %d flags %ld metric %d mtu %d", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 68 | ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 70 | assert (ifp->info); |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 71 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | if (!OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type)) |
| 73 | { |
| 74 | SET_IF_PARAM (IF_DEF_PARAMS (ifp), type); |
| 75 | IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 76 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | if (if_is_broadcast (ifp)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 78 | IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_BROADCAST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | else if (if_is_pointopoint (ifp)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 80 | IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_POINTOPOINT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | else if (if_is_loopback (ifp)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 82 | IF_DEF_PARAMS (ifp)->type = OSPF_IFTYPE_LOOPBACK; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | } |
| 84 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 85 | ospf = ospf_lookup (); |
| 86 | if (ospf != NULL) |
| 87 | ospf_if_update (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef HAVE_SNMP |
| 90 | ospf_snmp_if_update (ifp); |
| 91 | #endif /* HAVE_SNMP */ |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int |
| 97 | ospf_interface_delete (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 98 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | { |
| 100 | struct interface *ifp; |
| 101 | struct stream *s; |
| 102 | struct route_node *rn; |
| 103 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 104 | s = zclient->ibuf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | /* zebra_interface_state_read() updates interface structure in iflist */ |
| 106 | ifp = zebra_interface_state_read (s); |
| 107 | |
| 108 | if (ifp == NULL) |
| 109 | return 0; |
| 110 | |
| 111 | if (if_is_up (ifp)) |
| 112 | zlog_warn ("Zebra: got delete of %s, but interface is still up", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 113 | ifp->name); |
| 114 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 116 | zlog_info |
| 117 | ("Zebra: interface delete %s index %d flags %ld metric %d mtu %d", |
| 118 | ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | |
| 120 | #ifdef HAVE_SNMP |
| 121 | ospf_snmp_if_delete (ifp); |
| 122 | #endif /* HAVE_SNMP */ |
| 123 | |
| 124 | for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn)) |
| 125 | if (rn->info) |
| 126 | ospf_if_free ((struct ospf_interface *) rn->info); |
| 127 | |
| 128 | for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn)) |
| 129 | if (rn->info) |
| 130 | ospf_del_if_params (rn->info); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 131 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | if_delete (ifp); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | struct interface * |
| 138 | zebra_interface_if_lookup (struct stream *s) |
| 139 | { |
| 140 | struct interface *ifp; |
| 141 | u_char ifname_tmp[INTERFACE_NAMSIZ]; |
| 142 | |
| 143 | /* Read interface name. */ |
| 144 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 145 | |
| 146 | /* Lookup this by interface index. */ |
| 147 | ifp = if_lookup_by_name (ifname_tmp); |
| 148 | |
| 149 | /* If such interface does not exist, indicate an error */ |
| 150 | if (!ifp) |
| 151 | return NULL; |
| 152 | |
| 153 | return ifp; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) |
| 158 | { |
| 159 | /* Read interface's index. */ |
| 160 | ifp->ifindex = stream_getl (s); |
| 161 | |
| 162 | /* Read interface's value. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 163 | ifp->status = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | ifp->flags = stream_getl (s); |
| 165 | ifp->metric = stream_getl (s); |
| 166 | ifp->mtu = stream_getl (s); |
| 167 | ifp->bandwidth = stream_getl (s); |
| 168 | } |
| 169 | |
| 170 | int |
| 171 | ospf_interface_state_up (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 172 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | { |
| 174 | struct interface *ifp; |
| 175 | struct interface if_tmp; |
| 176 | struct ospf_interface *oi; |
| 177 | struct route_node *rn; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 178 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | ifp = zebra_interface_if_lookup (zclient->ibuf); |
| 180 | |
| 181 | if (ifp == NULL) |
| 182 | return 0; |
| 183 | |
| 184 | /* Interface is already up. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 185 | if (if_is_operative (ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | { |
| 187 | /* Temporarily keep ifp values. */ |
| 188 | memcpy (&if_tmp, ifp, sizeof (struct interface)); |
| 189 | |
| 190 | zebra_interface_if_set_value (zclient->ibuf, ifp); |
| 191 | |
| 192 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 193 | zlog_info ("Zebra: Interface[%s] state update.", ifp->name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | |
| 195 | if (if_tmp.bandwidth != ifp->bandwidth) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 196 | { |
| 197 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
| 198 | zlog_info ("Zebra: Interface[%s] bandwidth change %d -> %d.", |
| 199 | ifp->name, if_tmp.bandwidth, ifp->bandwidth); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 201 | ospf_if_recalculate_output_cost (ifp); |
| 202 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | return 0; |
| 204 | } |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 205 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | zebra_interface_if_set_value (zclient->ibuf, ifp); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 207 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 208 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
| 209 | zlog_info ("Zebra: Interface[%s] state change to up.", ifp->name); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 210 | |
| 211 | for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 213 | if ((oi = rn->info) == NULL) |
| 214 | continue; |
| 215 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | ospf_if_up (oi); |
| 217 | } |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 218 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | ospf_interface_state_down (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 224 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | { |
| 226 | struct interface *ifp; |
| 227 | struct ospf_interface *oi; |
| 228 | struct route_node *node; |
| 229 | |
| 230 | ifp = zebra_interface_state_read (zclient->ibuf); |
| 231 | |
| 232 | if (ifp == NULL) |
| 233 | return 0; |
| 234 | |
| 235 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
| 236 | zlog_info ("Zebra: Interface[%s] state change to down.", ifp->name); |
| 237 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 238 | for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 240 | if ((oi = node->info) == NULL) |
| 241 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 242 | ospf_if_down (oi); |
| 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | ospf_interface_address_add (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 250 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 252 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | struct connected *c; |
| 254 | |
| 255 | c = zebra_interface_address_add_read (zclient->ibuf); |
| 256 | |
| 257 | if (c == NULL) |
| 258 | return 0; |
| 259 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 260 | ospf = ospf_lookup (); |
| 261 | if (ospf != NULL) |
| 262 | ospf_if_update (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 263 | |
| 264 | #ifdef HAVE_SNMP |
| 265 | ospf_snmp_if_update (c->ifp); |
| 266 | #endif /* HAVE_SNMP */ |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | ospf_interface_address_delete (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 273 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 274 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 275 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 276 | struct connected *c; |
| 277 | struct interface *ifp; |
| 278 | struct ospf_interface *oi; |
| 279 | struct route_node *rn; |
| 280 | struct prefix p; |
| 281 | |
| 282 | c = zebra_interface_address_delete_read (zclient->ibuf); |
| 283 | |
| 284 | if (c == NULL) |
| 285 | return 0; |
| 286 | |
| 287 | ifp = c->ifp; |
| 288 | p = *c->address; |
| 289 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 290 | |
| 291 | rn = route_node_lookup (IF_OIFS (ifp), &p); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 292 | if (!rn) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | |
| 295 | assert (rn->info); |
| 296 | oi = rn->info; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 297 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 298 | /* Call interface hook functions to clean up */ |
| 299 | ospf_if_free (oi); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 300 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | #ifdef HAVE_SNMP |
| 302 | ospf_snmp_if_update (c->ifp); |
| 303 | #endif /* HAVE_SNMP */ |
| 304 | |
| 305 | connected_free (c); |
| 306 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 307 | ospf = ospf_lookup (); |
| 308 | if (ospf != NULL) |
| 309 | ospf_if_update (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 313 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | void |
| 315 | ospf_zebra_add (struct prefix_ipv4 *p, struct ospf_route *or) |
| 316 | { |
| 317 | u_char message; |
| 318 | u_char distance; |
| 319 | u_char flags; |
| 320 | int psize; |
| 321 | struct stream *s; |
| 322 | struct ospf_path *path; |
| 323 | listnode node; |
| 324 | |
| 325 | if (zclient->redist[ZEBRA_ROUTE_OSPF]) |
| 326 | { |
| 327 | message = 0; |
| 328 | flags = 0; |
| 329 | |
| 330 | /* OSPF pass nexthop and metric */ |
| 331 | SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP); |
| 332 | SET_FLAG (message, ZAPI_MESSAGE_METRIC); |
| 333 | |
| 334 | /* Distance value. */ |
| 335 | distance = ospf_distance_apply (p, or); |
| 336 | if (distance) |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 337 | SET_FLAG (message, ZAPI_MESSAGE_DISTANCE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | |
| 339 | /* Make packet. */ |
| 340 | s = zclient->obuf; |
| 341 | stream_reset (s); |
| 342 | |
| 343 | /* Length place holder. */ |
| 344 | stream_putw (s, 0); |
| 345 | |
| 346 | /* Put command, type, flags, message. */ |
| 347 | stream_putc (s, ZEBRA_IPV4_ROUTE_ADD); |
| 348 | stream_putc (s, ZEBRA_ROUTE_OSPF); |
| 349 | stream_putc (s, flags); |
| 350 | stream_putc (s, message); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 351 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 352 | /* Put prefix information. */ |
| 353 | psize = PSIZE (p->prefixlen); |
| 354 | stream_putc (s, p->prefixlen); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 355 | stream_write (s, (u_char *) & p->prefix, psize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 356 | |
| 357 | /* Nexthop count. */ |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 358 | stream_putc (s, or->paths->count); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 359 | |
| 360 | /* Nexthop, ifindex, distance and metric information. */ |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 361 | for (node = listhead (or->paths); node; nextnode (node)) |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 362 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 363 | path = getdata (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 364 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 365 | if (path->nexthop.s_addr != INADDR_ANY) |
| 366 | { |
| 367 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); |
| 368 | stream_put_in_addr (s, &path->nexthop); |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 373 | if (path->oi) |
| 374 | stream_putl (s, path->oi->ifp->ifindex); |
| 375 | else |
| 376 | stream_putl (s, 0); |
| 377 | } |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 378 | |
| 379 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 380 | { |
| 381 | zlog_info ("Zebra: Route add %s/%d nexthop %s", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 382 | inet_ntoa (p->prefix), |
| 383 | p->prefixlen, inet_ntoa (path->nexthop)); |
| 384 | } |
| 385 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | |
| 387 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 388 | stream_putc (s, distance); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 389 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 390 | { |
| 391 | if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL) |
| 392 | stream_putl (s, or->cost + or->u.ext.type2_cost); |
| 393 | else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL) |
| 394 | stream_putl (s, or->u.ext.type2_cost); |
| 395 | else |
| 396 | stream_putl (s, or->cost); |
| 397 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | |
| 399 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 400 | |
| 401 | writen (zclient->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | ospf_zebra_delete (struct prefix_ipv4 *p, struct ospf_route *or) |
| 407 | { |
| 408 | struct zapi_ipv4 api; |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 409 | struct ospf_path *path; |
| 410 | struct in_addr *nexthop; |
| 411 | listnode node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | |
| 413 | if (zclient->redist[ZEBRA_ROUTE_OSPF]) |
| 414 | { |
| 415 | api.type = ZEBRA_ROUTE_OSPF; |
| 416 | api.flags = 0; |
| 417 | api.message = 0; |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 418 | api.ifindex_num = 0; |
| 419 | api.nexthop_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 420 | |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 421 | for (node = listhead (or->paths); node; nextnode (node)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 422 | { |
| 423 | path = getdata (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 424 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 425 | if (path->nexthop.s_addr != INADDR_ANY) |
| 426 | { |
paul | d8e1d6b | 2003-08-10 04:04:41 +0000 | [diff] [blame^] | 427 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 428 | api.nexthop_num = 1; |
| 429 | nexthop = &path->nexthop; |
| 430 | api.nexthop = &nexthop; |
| 431 | } |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 432 | |
| 433 | zapi_ipv4_delete (zclient, p, &api); |
| 434 | |
| 435 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE) && api.nexthop_num) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 436 | { |
| 437 | zlog_info ("Zebra: Route delete %s/%d nexthop %s", |
| 438 | inet_ntoa (p->prefix), |
| 439 | p->prefixlen, inet_ntoa (**api.nexthop)); |
| 440 | } |
| 441 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | void |
| 446 | ospf_zebra_add_discard (struct prefix_ipv4 *p) |
| 447 | { |
| 448 | struct zapi_ipv4 api; |
| 449 | |
| 450 | if (zclient->redist[ZEBRA_ROUTE_OSPF]) |
| 451 | { |
| 452 | api.type = ZEBRA_ROUTE_OSPF; |
| 453 | api.flags = ZEBRA_FLAG_BLACKHOLE; |
| 454 | api.message = 0; |
| 455 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 456 | api.nexthop_num = 0; |
| 457 | api.ifindex_num = 0; |
| 458 | |
| 459 | zapi_ipv4_add (zclient, p, &api); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void |
| 464 | ospf_zebra_delete_discard (struct prefix_ipv4 *p) |
| 465 | { |
| 466 | struct zapi_ipv4 api; |
| 467 | |
| 468 | if (zclient->redist[ZEBRA_ROUTE_OSPF]) |
| 469 | { |
| 470 | api.type = ZEBRA_ROUTE_OSPF; |
| 471 | api.flags = ZEBRA_FLAG_BLACKHOLE; |
| 472 | api.message = 0; |
| 473 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
| 474 | api.nexthop_num = 0; |
| 475 | api.ifindex_num = 0; |
| 476 | |
| 477 | zapi_ipv4_delete (zclient, p, &api); |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 478 | |
| 479 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 480 | zlog_info ("Zebra: Route delete discard %s/%d", |
| 481 | inet_ntoa (p->prefix), p->prefixlen); |
paul | 72357f2 | 2003-06-19 02:11:23 +0000 | [diff] [blame] | 482 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | int |
| 487 | ospf_is_type_redistributed (int type) |
| 488 | { |
| 489 | return (DEFAULT_ROUTE_TYPE (type)) ? |
| 490 | zclient->default_information : zclient->redist[type]; |
| 491 | } |
| 492 | |
| 493 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 494 | ospf_redistribute_set (struct ospf *ospf, int type, int mtype, int mvalue) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | { |
| 496 | int force = 0; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 497 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | if (ospf_is_type_redistributed (type)) |
| 499 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 500 | if (mtype != ospf->dmetric[type].type) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 501 | { |
| 502 | ospf->dmetric[type].type = mtype; |
| 503 | force = LSA_REFRESH_FORCE; |
| 504 | } |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 505 | if (mvalue != ospf->dmetric[type].value) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 506 | { |
| 507 | ospf->dmetric[type].value = mvalue; |
| 508 | force = LSA_REFRESH_FORCE; |
| 509 | } |
| 510 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 511 | ospf_external_lsa_refresh_type (ospf, type, force); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 512 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 513 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 514 | zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]", |
| 515 | LOOKUP (ospf_redistributed_proto, type), |
| 516 | metric_type (ospf, type), metric_value (ospf, type)); |
| 517 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | return CMD_SUCCESS; |
| 519 | } |
| 520 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 521 | ospf->dmetric[type].type = mtype; |
| 522 | ospf->dmetric[type].value = mvalue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | |
| 524 | zclient_redistribute_set (zclient, type); |
| 525 | |
| 526 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 527 | zlog_info ("Redistribute[%s]: Start Type[%d], Metric[%d]", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 528 | LOOKUP (ospf_redistributed_proto, type), |
| 529 | metric_type (ospf, type), metric_value (ospf, type)); |
| 530 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 531 | ospf_asbr_status_update (ospf, ++ospf->redistribute); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | |
| 533 | return CMD_SUCCESS; |
| 534 | } |
| 535 | |
| 536 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 537 | ospf_redistribute_unset (struct ospf *ospf, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | { |
| 539 | if (type == zclient->redist_default) |
| 540 | return CMD_SUCCESS; |
| 541 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 542 | if (!ospf_is_type_redistributed (type)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | return CMD_SUCCESS; |
| 544 | |
| 545 | zclient_redistribute_unset (zclient, type); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 546 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 548 | zlog_info ("Redistribute[%s]: Stop", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 549 | LOOKUP (ospf_redistributed_proto, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 550 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 551 | ospf->dmetric[type].type = -1; |
| 552 | ospf->dmetric[type].value = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | |
| 554 | /* Remove the routes from OSPF table. */ |
| 555 | ospf_redistribute_withdraw (type); |
| 556 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 557 | ospf_asbr_status_update (ospf, --ospf->redistribute); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 558 | |
| 559 | return CMD_SUCCESS; |
| 560 | } |
| 561 | |
| 562 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 563 | ospf_redistribute_default_set (struct ospf *ospf, int originate, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 564 | int mtype, int mvalue) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 565 | { |
| 566 | int force = 0; |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 567 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | if (ospf_is_type_redistributed (DEFAULT_ROUTE)) |
| 569 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 570 | if (mtype != ospf->dmetric[DEFAULT_ROUTE].type) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 571 | { |
| 572 | ospf->dmetric[DEFAULT_ROUTE].type = mtype; |
| 573 | force = 1; |
| 574 | } |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 575 | if (mvalue != ospf->dmetric[DEFAULT_ROUTE].value) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 576 | { |
| 577 | force = 1; |
| 578 | ospf->dmetric[DEFAULT_ROUTE].value = mvalue; |
| 579 | } |
| 580 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 581 | ospf_external_lsa_refresh_default (ospf); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 582 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 584 | zlog_info ("Redistribute[%s]: Refresh Type[%d], Metric[%d]", |
| 585 | LOOKUP (ospf_redistributed_proto, DEFAULT_ROUTE), |
| 586 | metric_type (ospf, DEFAULT_ROUTE), |
| 587 | metric_value (ospf, DEFAULT_ROUTE)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | return CMD_SUCCESS; |
| 589 | } |
| 590 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 591 | ospf->default_originate = originate; |
| 592 | ospf->dmetric[DEFAULT_ROUTE].type = mtype; |
| 593 | ospf->dmetric[DEFAULT_ROUTE].value = mvalue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 594 | |
| 595 | zclient_redistribute_default_set (zclient); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 596 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 597 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 598 | zlog_info ("Redistribute[DEFAULT]: Start Type[%d], Metric[%d]", |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 599 | metric_type (ospf, DEFAULT_ROUTE), |
| 600 | metric_value (ospf, DEFAULT_ROUTE)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 601 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 602 | if (ospf->router_id.s_addr == 0) |
| 603 | ospf->external_origin |= (1 << DEFAULT_ROUTE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | else |
| 605 | thread_add_timer (master, ospf_default_originate_timer, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 606 | &ospf->default_originate, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 607 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 608 | ospf_asbr_status_update (ospf, ++ospf->redistribute); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | |
| 610 | return CMD_SUCCESS; |
| 611 | } |
| 612 | |
| 613 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 614 | ospf_redistribute_default_unset (struct ospf *ospf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | { |
| 616 | if (!ospf_is_type_redistributed (DEFAULT_ROUTE)) |
| 617 | return CMD_SUCCESS; |
| 618 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 619 | ospf->default_originate = DEFAULT_ORIGINATE_NONE; |
| 620 | ospf->dmetric[DEFAULT_ROUTE].type = -1; |
| 621 | ospf->dmetric[DEFAULT_ROUTE].value = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | |
| 623 | zclient_redistribute_default_unset (zclient); |
| 624 | |
| 625 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 626 | zlog_info ("Redistribute[DEFAULT]: Stop"); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 627 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 628 | ospf_asbr_status_update (ospf, --ospf->redistribute); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 629 | |
| 630 | return CMD_SUCCESS; |
| 631 | } |
| 632 | |
| 633 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 634 | ospf_external_lsa_originate_check (struct ospf *ospf, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 635 | struct external_info *ei) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 636 | { |
| 637 | /* If prefix is multicast, then do not originate LSA. */ |
| 638 | if (IN_MULTICAST (htonl (ei->p.prefix.s_addr))) |
| 639 | { |
| 640 | zlog_info ("LSA[Type5:%s]: Not originate AS-external-LSA, " |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 641 | "Prefix belongs multicast", inet_ntoa (ei->p.prefix)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* Take care of default-originate. */ |
| 646 | if (is_prefix_default (&ei->p)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 647 | if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 649 | zlog_info ("LSA[Type5:0.0.0.0]: Not originate AS-exntenal-LSA " |
| 650 | "for default"); |
| 651 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* If connected prefix is OSPF enable interface, then do not announce. */ |
| 658 | int |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 659 | ospf_distribute_check_connected (struct ospf *ospf, struct external_info *ei) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 660 | { |
| 661 | struct route_node *rn; |
| 662 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 663 | for (rn = route_top (ospf->networks); rn; rn = route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 664 | if (rn->info != NULL) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 665 | if (prefix_match (&rn->p, (struct prefix *) &ei->p)) |
| 666 | { |
| 667 | route_unlock_node (rn); |
| 668 | return 0; |
| 669 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | |
| 671 | return 1; |
| 672 | } |
| 673 | |
| 674 | /* return 1 if external LSA must be originated, 0 otherwise */ |
| 675 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 676 | ospf_redistribute_check (struct ospf *ospf, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 677 | struct external_info *ei, int *changed) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 678 | { |
| 679 | struct route_map_set_values save_values; |
| 680 | struct prefix_ipv4 *p = &ei->p; |
| 681 | u_char type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 682 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | if (changed) |
| 684 | *changed = 0; |
| 685 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 686 | if (!ospf_external_lsa_originate_check (ospf, ei)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 687 | return 0; |
| 688 | |
| 689 | /* Take care connected route. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 690 | if (type == ZEBRA_ROUTE_CONNECT && |
| 691 | !ospf_distribute_check_connected (ospf, ei)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 692 | return 0; |
| 693 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 694 | if (!DEFAULT_ROUTE_TYPE (type) && DISTRIBUTE_NAME (ospf, type)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | /* distirbute-list exists, but access-list may not? */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 696 | if (DISTRIBUTE_LIST (ospf, type)) |
| 697 | if (access_list_apply (DISTRIBUTE_LIST (ospf, type), p) == FILTER_DENY) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 698 | { |
| 699 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 700 | zlog_info ("Redistribute[%s]: %s/%d filtered by ditribute-list.", |
| 701 | LOOKUP (ospf_redistributed_proto, type), |
| 702 | inet_ntoa (p->prefix), p->prefixlen); |
| 703 | return 0; |
| 704 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | |
| 706 | save_values = ei->route_map_set; |
| 707 | ospf_reset_route_map_set_values (&ei->route_map_set); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 708 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | /* apply route-map if needed */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 710 | if (ROUTEMAP_NAME (ospf, type)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | { |
| 712 | int ret; |
| 713 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 714 | ret = route_map_apply (ROUTEMAP (ospf, type), (struct prefix *) p, |
| 715 | RMAP_OSPF, ei); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 716 | |
| 717 | if (ret == RMAP_DENYMATCH) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 718 | { |
| 719 | ei->route_map_set = save_values; |
| 720 | if (IS_DEBUG_OSPF (zebra, ZEBRA_REDISTRIBUTE)) |
| 721 | zlog_info ("Redistribute[%s]: %s/%d filtered by route-map.", |
| 722 | LOOKUP (ospf_redistributed_proto, type), |
| 723 | inet_ntoa (p->prefix), p->prefixlen); |
| 724 | return 0; |
| 725 | } |
| 726 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 727 | /* check if 'route-map set' changed something */ |
| 728 | if (changed) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 729 | *changed = !ospf_route_map_set_compare (&ei->route_map_set, |
| 730 | &save_values); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | /* OSPF route-map set for redistribution */ |
| 737 | void |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 738 | ospf_routemap_set (struct ospf *ospf, int type, char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 740 | if (ROUTEMAP_NAME (ospf, type)) |
| 741 | free (ROUTEMAP_NAME (ospf, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 743 | ROUTEMAP_NAME (ospf, type) = strdup (name); |
| 744 | ROUTEMAP (ospf, type) = route_map_lookup_by_name (name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | void |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 748 | ospf_routemap_unset (struct ospf *ospf, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 749 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 750 | if (ROUTEMAP_NAME (ospf, type)) |
| 751 | free (ROUTEMAP_NAME (ospf, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 752 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 753 | ROUTEMAP_NAME (ospf, type) = NULL; |
| 754 | ROUTEMAP (ospf, type) = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* Zebra route add and delete treatment. */ |
| 758 | int |
| 759 | ospf_zebra_read_ipv4 (int command, struct zclient *zclient, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 760 | zebra_size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 761 | { |
| 762 | struct stream *s; |
| 763 | struct zapi_ipv4 api; |
| 764 | unsigned long ifindex; |
| 765 | struct in_addr nexthop; |
| 766 | struct prefix_ipv4 p; |
| 767 | struct external_info *ei; |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 768 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | |
| 770 | s = zclient->ibuf; |
| 771 | ifindex = 0; |
| 772 | nexthop.s_addr = 0; |
| 773 | |
| 774 | /* Type, flags, message. */ |
| 775 | api.type = stream_getc (s); |
| 776 | api.flags = stream_getc (s); |
| 777 | api.message = stream_getc (s); |
| 778 | |
| 779 | /* IPv4 prefix. */ |
| 780 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 781 | p.family = AF_INET; |
| 782 | p.prefixlen = stream_getc (s); |
| 783 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 784 | |
| 785 | /* Nexthop, ifindex, distance, metric. */ |
| 786 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 787 | { |
| 788 | api.nexthop_num = stream_getc (s); |
| 789 | nexthop.s_addr = stream_get_ipv4 (s); |
| 790 | } |
| 791 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 792 | { |
| 793 | api.ifindex_num = stream_getc (s); |
| 794 | ifindex = stream_getl (s); |
| 795 | } |
| 796 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 797 | api.distance = stream_getc (s); |
| 798 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 799 | api.metric = stream_getl (s); |
| 800 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 801 | ospf = ospf_lookup (); |
| 802 | if (ospf == NULL) |
| 803 | return 0; |
| 804 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | if (command == ZEBRA_IPV4_ROUTE_ADD) |
| 806 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 807 | /* XXX|HACK|TODO|FIXME: |
| 808 | * ignore reject/blackhole routes |
| 809 | * need a better generalised solution for these types |
| 810 | * really. |
| 811 | */ |
| 812 | if ( CHECK_FLAG (api.flags, ZEBRA_FLAG_BLACKHOLE) |
| 813 | || CHECK_FLAG (api.flags, ZEBRA_FLAG_REJECT)) |
| 814 | return 0; |
| 815 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | ei = ospf_external_info_add (api.type, p, ifindex, nexthop); |
| 817 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 818 | if (ospf->router_id.s_addr == 0) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 819 | /* Set flags to generate AS-external-LSA originate event |
| 820 | for each redistributed protocols later. */ |
| 821 | ospf->external_origin |= (1 << api.type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 822 | else |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 823 | { |
| 824 | if (ei) |
| 825 | { |
| 826 | if (is_prefix_default (&p)) |
| 827 | ospf_external_lsa_refresh_default (ospf); |
| 828 | else |
| 829 | { |
| 830 | struct ospf_lsa *current; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 831 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 832 | current = ospf_external_info_find_lsa (ospf, &ei->p); |
| 833 | if (!current) |
| 834 | ospf_external_lsa_originate (ospf, ei); |
| 835 | else if (IS_LSA_MAXAGE (current)) |
| 836 | ospf_external_lsa_refresh (ospf, current, |
| 837 | ei, LSA_REFRESH_FORCE); |
| 838 | else |
| 839 | zlog_warn ("ospf_zebra_read_ipv4() : %s already exists", |
| 840 | inet_ntoa (p.prefix)); |
| 841 | } |
| 842 | } |
| 843 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | } |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 845 | else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 846 | { |
| 847 | ospf_external_info_delete (api.type, p); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 848 | if (is_prefix_default (&p)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 849 | ospf_external_lsa_refresh_default (ospf); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 850 | else |
| 851 | ospf_external_lsa_flush (ospf, api.type, &p, ifindex, nexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 856 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 857 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 858 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 859 | ospf_distribute_list_out_set (struct ospf *ospf, int type, char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 860 | { |
| 861 | /* Lookup access-list for distribute-list. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 862 | DISTRIBUTE_LIST (ospf, type) = access_list_lookup (AFI_IP, name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 863 | |
| 864 | /* Clear previous distribute-name. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 865 | if (DISTRIBUTE_NAME (ospf, type)) |
| 866 | free (DISTRIBUTE_NAME (ospf, type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 867 | |
| 868 | /* Set distribute-name. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 869 | DISTRIBUTE_NAME (ospf, type) = strdup (name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 870 | |
| 871 | /* If access-list have been set, schedule update timer. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 872 | if (DISTRIBUTE_LIST (ospf, type)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 873 | ospf_distribute_list_update (ospf, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 874 | |
| 875 | return CMD_SUCCESS; |
| 876 | } |
| 877 | |
| 878 | int |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 879 | ospf_distribute_list_out_unset (struct ospf *ospf, int type, char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | { |
| 881 | /* Schedule update timer. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 882 | if (DISTRIBUTE_LIST (ospf, type)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 883 | ospf_distribute_list_update (ospf, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 884 | |
| 885 | /* Unset distribute-list. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 886 | DISTRIBUTE_LIST (ospf, type) = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 887 | |
| 888 | /* Clear distribute-name. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 889 | if (DISTRIBUTE_NAME (ospf, type)) |
| 890 | free (DISTRIBUTE_NAME (ospf, type)); |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 891 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 892 | DISTRIBUTE_NAME (ospf, type) = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 893 | |
| 894 | return CMD_SUCCESS; |
| 895 | } |
| 896 | |
| 897 | /* distribute-list update timer. */ |
| 898 | int |
| 899 | ospf_distribute_list_update_timer (struct thread *thread) |
| 900 | { |
| 901 | struct route_node *rn; |
| 902 | struct external_info *ei; |
| 903 | struct route_table *rt; |
| 904 | struct ospf_lsa *lsa; |
| 905 | u_char type; |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 906 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 907 | |
| 908 | type = (int) THREAD_ARG (thread); |
| 909 | rt = EXTERNAL_INFO (type); |
| 910 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 911 | ospf = ospf_lookup (); |
| 912 | if (ospf == NULL) |
| 913 | return 0; |
| 914 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 915 | ospf->t_distribute_update = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 916 | |
| 917 | zlog_info ("Zebra[Redistribute]: distribute-list update timer fired!"); |
| 918 | |
| 919 | /* foreach all external info. */ |
| 920 | if (rt) |
| 921 | for (rn = route_top (rt); rn; rn = route_next (rn)) |
| 922 | if ((ei = rn->info) != NULL) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 923 | { |
| 924 | if (is_prefix_default (&ei->p)) |
| 925 | ospf_external_lsa_refresh_default (ospf); |
| 926 | else if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p))) |
| 927 | ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_IF_CHANGED); |
| 928 | else |
| 929 | ospf_external_lsa_originate (ospf, ei); |
| 930 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | #define OSPF_DISTRIBUTE_UPDATE_DELAY 5 |
| 935 | |
| 936 | /* Update distribute-list and set timer to apply access-list. */ |
| 937 | void |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 938 | ospf_distribute_list_update (struct ospf *ospf, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 939 | { |
| 940 | struct route_table *rt; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 941 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 942 | /* External info does not exist. */ |
| 943 | if (!(rt = EXTERNAL_INFO (type))) |
| 944 | return; |
| 945 | |
| 946 | /* If exists previously invoked thread, then cancel it. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 947 | if (ospf->t_distribute_update) |
| 948 | OSPF_TIMER_OFF (ospf->t_distribute_update); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | |
| 950 | /* Set timer. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 951 | ospf->t_distribute_update = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 952 | thread_add_timer (master, ospf_distribute_list_update_timer, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 953 | (void *) type, OSPF_DISTRIBUTE_UPDATE_DELAY); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* If access-list is updated, apply some check. */ |
| 957 | void |
| 958 | ospf_filter_update (struct access_list *access) |
| 959 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 960 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 961 | int type; |
| 962 | int abr_inv = 0; |
| 963 | struct ospf_area *area; |
| 964 | listnode node; |
| 965 | |
| 966 | /* If OSPF instatnce does not exist, return right now. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 967 | ospf = ospf_lookup (); |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 968 | if (ospf == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 969 | return; |
| 970 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | /* Update distribute-list, and apply filter. */ |
| 972 | for (type = 0; type < ZEBRA_ROUTE_MAX; type++) |
| 973 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 974 | if (ROUTEMAP (ospf, type) != NULL) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 975 | { |
| 976 | /* if route-map is not NULL it may be using this access list */ |
| 977 | ospf_distribute_list_update (ospf, type); |
| 978 | continue; |
| 979 | } |
| 980 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 982 | if (DISTRIBUTE_NAME (ospf, type)) |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 983 | { |
| 984 | /* Keep old access-list for distribute-list. */ |
| 985 | struct access_list *old = DISTRIBUTE_LIST (ospf, type); |
| 986 | |
| 987 | /* Update access-list for distribute-list. */ |
| 988 | DISTRIBUTE_LIST (ospf, type) = |
| 989 | access_list_lookup (AFI_IP, DISTRIBUTE_NAME (ospf, type)); |
| 990 | |
| 991 | /* No update for this distribute type. */ |
| 992 | if (old == NULL && DISTRIBUTE_LIST (ospf, type) == NULL) |
| 993 | continue; |
| 994 | |
| 995 | /* Schedule distribute-list update timer. */ |
| 996 | if (DISTRIBUTE_LIST (ospf, type) == NULL || |
| 997 | strcmp (DISTRIBUTE_NAME (ospf, type), access->name) == 0) |
| 998 | ospf_distribute_list_update (ospf, type); |
| 999 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* Update Area access-list. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1003 | for (node = listhead (ospf->areas); node; nextnode (node)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | if ((area = getdata (node)) != NULL) |
| 1005 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1006 | if (EXPORT_NAME (area)) |
| 1007 | { |
| 1008 | EXPORT_LIST (area) = NULL; |
| 1009 | abr_inv++; |
| 1010 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1011 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1012 | if (IMPORT_NAME (area)) |
| 1013 | { |
| 1014 | IMPORT_LIST (area) = NULL; |
| 1015 | abr_inv++; |
| 1016 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /* Schedule ABR tasks -- this will be changed -- takada. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1020 | if (IS_OSPF_ABR (ospf) && abr_inv) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1021 | ospf_schedule_abr_task (ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1022 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1023 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1024 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | struct ospf_distance * |
| 1026 | ospf_distance_new () |
| 1027 | { |
| 1028 | struct ospf_distance *new; |
| 1029 | new = XMALLOC (MTYPE_OSPF_DISTANCE, sizeof (struct ospf_distance)); |
| 1030 | memset (new, 0, sizeof (struct ospf_distance)); |
| 1031 | return new; |
| 1032 | } |
| 1033 | |
| 1034 | void |
| 1035 | ospf_distance_free (struct ospf_distance *odistance) |
| 1036 | { |
| 1037 | XFREE (MTYPE_OSPF_DISTANCE, odistance); |
| 1038 | } |
| 1039 | |
| 1040 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1041 | ospf_distance_set (struct vty *vty, struct ospf *ospf, char *distance_str, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1042 | char *ip_str, char *access_list_str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | { |
| 1044 | int ret; |
| 1045 | struct prefix_ipv4 p; |
| 1046 | u_char distance; |
| 1047 | struct route_node *rn; |
| 1048 | struct ospf_distance *odistance; |
| 1049 | |
| 1050 | ret = str2prefix_ipv4 (ip_str, &p); |
| 1051 | if (ret == 0) |
| 1052 | { |
| 1053 | vty_out (vty, "Malformed prefix%s", VTY_NEWLINE); |
| 1054 | return CMD_WARNING; |
| 1055 | } |
| 1056 | |
| 1057 | distance = atoi (distance_str); |
| 1058 | |
| 1059 | /* Get OSPF distance node. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1060 | rn = route_node_get (ospf->distance_table, (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1061 | if (rn->info) |
| 1062 | { |
| 1063 | odistance = rn->info; |
| 1064 | route_unlock_node (rn); |
| 1065 | } |
| 1066 | else |
| 1067 | { |
| 1068 | odistance = ospf_distance_new (); |
| 1069 | rn->info = odistance; |
| 1070 | } |
| 1071 | |
| 1072 | /* Set distance value. */ |
| 1073 | odistance->distance = distance; |
| 1074 | |
| 1075 | /* Reset access-list configuration. */ |
| 1076 | if (odistance->access_list) |
| 1077 | { |
| 1078 | free (odistance->access_list); |
| 1079 | odistance->access_list = NULL; |
| 1080 | } |
| 1081 | if (access_list_str) |
| 1082 | odistance->access_list = strdup (access_list_str); |
| 1083 | |
| 1084 | return CMD_SUCCESS; |
| 1085 | } |
| 1086 | |
| 1087 | int |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1088 | ospf_distance_unset (struct vty *vty, struct ospf *ospf, char *distance_str, |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1089 | char *ip_str, char *access_list_str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1090 | { |
| 1091 | int ret; |
| 1092 | struct prefix_ipv4 p; |
| 1093 | u_char distance; |
| 1094 | struct route_node *rn; |
| 1095 | struct ospf_distance *odistance; |
| 1096 | |
| 1097 | ret = str2prefix_ipv4 (ip_str, &p); |
| 1098 | if (ret == 0) |
| 1099 | { |
| 1100 | vty_out (vty, "Malformed prefix%s", VTY_NEWLINE); |
| 1101 | return CMD_WARNING; |
| 1102 | } |
| 1103 | |
| 1104 | distance = atoi (distance_str); |
| 1105 | |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1106 | rn = route_node_lookup (ospf->distance_table, (struct prefix *) &p); |
| 1107 | if (!rn) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1108 | { |
| 1109 | vty_out (vty, "Can't find specified prefix%s", VTY_NEWLINE); |
| 1110 | return CMD_WARNING; |
| 1111 | } |
| 1112 | |
| 1113 | odistance = rn->info; |
| 1114 | |
| 1115 | if (odistance->access_list) |
| 1116 | free (odistance->access_list); |
| 1117 | ospf_distance_free (odistance); |
| 1118 | |
| 1119 | rn->info = NULL; |
| 1120 | route_unlock_node (rn); |
| 1121 | route_unlock_node (rn); |
| 1122 | |
| 1123 | return CMD_SUCCESS; |
| 1124 | } |
| 1125 | |
| 1126 | void |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1127 | ospf_distance_reset (struct ospf *ospf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1128 | { |
| 1129 | struct route_node *rn; |
| 1130 | struct ospf_distance *odistance; |
| 1131 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1132 | for (rn = route_top (ospf->distance_table); rn; rn = route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | if ((odistance = rn->info) != NULL) |
| 1134 | { |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1135 | if (odistance->access_list) |
| 1136 | free (odistance->access_list); |
| 1137 | ospf_distance_free (odistance); |
| 1138 | rn->info = NULL; |
| 1139 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | u_char |
| 1144 | ospf_distance_apply (struct prefix_ipv4 *p, struct ospf_route *or) |
| 1145 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1146 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1148 | ospf = ospf_lookup (); |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1149 | if (ospf == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | return 0; |
| 1151 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1152 | if (ospf->distance_intra) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1153 | if (or->path_type == OSPF_PATH_INTRA_AREA) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1154 | return ospf->distance_intra; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1155 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1156 | if (ospf->distance_inter) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1157 | if (or->path_type == OSPF_PATH_INTER_AREA) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1158 | return ospf->distance_inter; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1160 | if (ospf->distance_external) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1161 | if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1162 | || or->path_type == OSPF_PATH_TYPE2_EXTERNAL) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1163 | return ospf->distance_external; |
paul | cf795c5 | 2003-06-19 02:13:25 +0000 | [diff] [blame] | 1164 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1165 | if (ospf->distance_all) |
| 1166 | return ospf->distance_all; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | void |
| 1172 | ospf_zebra_init () |
| 1173 | { |
| 1174 | /* Allocate zebra structure. */ |
| 1175 | zclient = zclient_new (); |
| 1176 | zclient_init (zclient, ZEBRA_ROUTE_OSPF); |
| 1177 | zclient->interface_add = ospf_interface_add; |
| 1178 | zclient->interface_delete = ospf_interface_delete; |
| 1179 | zclient->interface_up = ospf_interface_state_up; |
| 1180 | zclient->interface_down = ospf_interface_state_down; |
| 1181 | zclient->interface_address_add = ospf_interface_address_add; |
| 1182 | zclient->interface_address_delete = ospf_interface_address_delete; |
| 1183 | zclient->ipv4_route_add = ospf_zebra_read_ipv4; |
| 1184 | zclient->ipv4_route_delete = ospf_zebra_read_ipv4; |
| 1185 | |
| 1186 | access_list_add_hook (ospf_filter_update); |
| 1187 | access_list_delete_hook (ospf_filter_update); |
| 1188 | } |