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