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