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