]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "command.h" | |
26 | #include "network.h" | |
27 | #include "prefix.h" | |
28 | #include "routemap.h" | |
29 | #include "table.h" | |
30 | #include "stream.h" | |
31 | #include "memory.h" | |
32 | #include "zclient.h" | |
33 | #include "filter.h" | |
dd669bb0 | 34 | #include "plist.h" |
718e3744 | 35 | #include "log.h" |
2376c3f2 | 36 | #include "lib/bfd.h" |
5b30316e | 37 | #include "nexthop.h" |
718e3744 | 38 | |
39 | #include "ospfd/ospfd.h" | |
40 | #include "ospfd/ospf_interface.h" | |
41 | #include "ospfd/ospf_ism.h" | |
42 | #include "ospfd/ospf_asbr.h" | |
43 | #include "ospfd/ospf_asbr.h" | |
44 | #include "ospfd/ospf_abr.h" | |
45 | #include "ospfd/ospf_lsa.h" | |
46 | #include "ospfd/ospf_dump.h" | |
47 | #include "ospfd/ospf_route.h" | |
d5a5c8f0 DS |
48 | #include "ospfd/ospf_lsdb.h" |
49 | #include "ospfd/ospf_neighbor.h" | |
50 | #include "ospfd/ospf_nsm.h" | |
718e3744 | 51 | #include "ospfd/ospf_zebra.h" |
16f1b9ee | 52 | #include "ospfd/ospf_te.h" |
718e3744 | 53 | |
dfac5d39 CS |
54 | DEFINE_MTYPE_STATIC(OSPFD, OSPF_EXTERNAL, "OSPF External route table") |
55 | DEFINE_MTYPE_STATIC(OSPFD, OSPF_REDISTRIBUTE, "OSPF Redistriute") | |
b5a8894d | 56 | DEFINE_MTYPE_STATIC(OSPFD, OSPF_DIST_ARGS, "OSPF Distribute arguments") |
dfac5d39 | 57 | |
718e3744 | 58 | /* Zebra structure to hold current status. */ |
59 | struct zclient *zclient = NULL; | |
60 | ||
61 | /* For registering threads. */ | |
62 | extern struct thread_master *master; | |
18a6dce6 | 63 | |
64 | /* Router-id update message from zebra. */ | |
121f9dee | 65 | static int ospf_router_id_update_zebra(ZAPI_CALLBACK_ARGS) |
18a6dce6 | 66 | { |
b5a8894d | 67 | struct ospf *ospf = NULL; |
d62a17ae | 68 | struct prefix router_id; |
69 | zebra_router_id_update_read(zclient->ibuf, &router_id); | |
70 | ||
71 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) { | |
72 | char buf[PREFIX2STR_BUFFER]; | |
73 | prefix2str(&router_id, buf, sizeof(buf)); | |
996c9314 LB |
74 | zlog_debug("Zebra rcvd: router id update %s vrf %s id %u", buf, |
75 | ospf_vrf_id_to_name(vrf_id), vrf_id); | |
d62a17ae | 76 | } |
77 | ||
b5a8894d | 78 | ospf = ospf_lookup_by_vrf_id(vrf_id); |
d62a17ae | 79 | |
6021c6c0 CS |
80 | if (ospf != NULL) { |
81 | ospf->router_id_zebra = router_id.u.prefix4; | |
d62a17ae | 82 | ospf_router_id_update(ospf); |
6021c6c0 | 83 | } else { |
b5a8894d CS |
84 | if (IS_DEBUG_OSPF_EVENT) { |
85 | char buf[PREFIX2STR_BUFFER]; | |
86 | ||
87 | prefix2str(&router_id, buf, sizeof(buf)); | |
996c9314 LB |
88 | zlog_debug( |
89 | "%s: ospf instance not found for vrf %s id %u router_id %s", | |
15569c58 DA |
90 | __func__, ospf_vrf_id_to_name(vrf_id), vrf_id, |
91 | buf); | |
b5a8894d CS |
92 | } |
93 | } | |
d62a17ae | 94 | return 0; |
18a6dce6 | 95 | } |
718e3744 | 96 | |
121f9dee | 97 | static int ospf_interface_address_add(ZAPI_CALLBACK_ARGS) |
718e3744 | 98 | { |
d62a17ae | 99 | struct connected *c; |
b5a8894d CS |
100 | struct ospf *ospf = NULL; |
101 | ||
718e3744 | 102 | |
121f9dee | 103 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
718e3744 | 104 | |
d62a17ae | 105 | if (c == NULL) |
106 | return 0; | |
718e3744 | 107 | |
d62a17ae | 108 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) { |
109 | char buf[PREFIX2STR_BUFFER]; | |
110 | prefix2str(c->address, buf, sizeof(buf)); | |
b5a8894d CS |
111 | zlog_debug("Zebra: interface %s address add %s vrf %s id %u", |
112 | c->ifp->name, buf, ospf_vrf_id_to_name(vrf_id), | |
113 | vrf_id); | |
d62a17ae | 114 | } |
7f643ebf | 115 | |
b5a8894d | 116 | ospf = ospf_lookup_by_vrf_id(vrf_id); |
43b8d1d8 CS |
117 | if (!ospf) |
118 | return 0; | |
b5a8894d CS |
119 | |
120 | ospf_if_update(ospf, c->ifp); | |
718e3744 | 121 | |
ef7bd2a3 | 122 | ospf_if_interface(c->ifp); |
718e3744 | 123 | |
d62a17ae | 124 | return 0; |
718e3744 | 125 | } |
126 | ||
121f9dee | 127 | static int ospf_interface_address_delete(ZAPI_CALLBACK_ARGS) |
718e3744 | 128 | { |
d62a17ae | 129 | struct connected *c; |
130 | struct interface *ifp; | |
131 | struct ospf_interface *oi; | |
132 | struct route_node *rn; | |
133 | struct prefix p; | |
134 | ||
121f9dee | 135 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
d62a17ae | 136 | |
137 | if (c == NULL) | |
138 | return 0; | |
139 | ||
140 | if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) { | |
141 | char buf[PREFIX2STR_BUFFER]; | |
142 | prefix2str(c->address, buf, sizeof(buf)); | |
143 | zlog_debug("Zebra: interface %s address delete %s", | |
144 | c->ifp->name, buf); | |
145 | } | |
7f643ebf | 146 | |
d62a17ae | 147 | ifp = c->ifp; |
148 | p = *c->address; | |
149 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
718e3744 | 150 | |
d62a17ae | 151 | rn = route_node_lookup(IF_OIFS(ifp), &p); |
152 | if (!rn) { | |
721c0857 | 153 | connected_free(&c); |
d62a17ae | 154 | return 0; |
155 | } | |
718e3744 | 156 | |
d62a17ae | 157 | assert(rn->info); |
158 | oi = rn->info; | |
159 | route_unlock_node(rn); | |
cf795c5d | 160 | |
d62a17ae | 161 | /* Call interface hook functions to clean up */ |
162 | ospf_if_free(oi); | |
cf795c5d | 163 | |
ef7bd2a3 | 164 | ospf_if_interface(c->ifp); |
718e3744 | 165 | |
721c0857 | 166 | connected_free(&c); |
718e3744 | 167 | |
d62a17ae | 168 | return 0; |
718e3744 | 169 | } |
72357f2b | 170 | |
121f9dee | 171 | static int ospf_interface_link_params(ZAPI_CALLBACK_ARGS) |
16f1b9ee | 172 | { |
d62a17ae | 173 | struct interface *ifp; |
16f1b9ee | 174 | |
edc12762 | 175 | ifp = zebra_interface_link_params_read(zclient->ibuf, vrf_id); |
16f1b9ee | 176 | |
d62a17ae | 177 | if (ifp == NULL) |
178 | return 0; | |
16f1b9ee | 179 | |
d62a17ae | 180 | /* Update TE TLV */ |
181 | ospf_mpls_te_update_if(ifp); | |
16f1b9ee | 182 | |
d62a17ae | 183 | return 0; |
16f1b9ee OD |
184 | } |
185 | ||
b5a8894d | 186 | /* VRF update for an interface. */ |
121f9dee | 187 | static int ospf_interface_vrf_update(ZAPI_CALLBACK_ARGS) |
b5a8894d CS |
188 | { |
189 | struct interface *ifp = NULL; | |
190 | vrf_id_t new_vrf_id; | |
16f1b9ee | 191 | |
b5a8894d | 192 | ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id, |
996c9314 | 193 | &new_vrf_id); |
b5a8894d CS |
194 | if (!ifp) |
195 | return 0; | |
196 | ||
197 | if (IS_DEBUG_OSPF_EVENT) | |
996c9314 LB |
198 | zlog_debug( |
199 | "%s: Rx Interface %s VRF change vrf_id %u New vrf %s id %u", | |
15569c58 | 200 | __func__, ifp->name, vrf_id, |
996c9314 | 201 | ospf_vrf_id_to_name(new_vrf_id), new_vrf_id); |
b5a8894d CS |
202 | |
203 | /*if_update(ifp, ifp->name, strlen(ifp->name), new_vrf_id);*/ | |
a36898e7 | 204 | if_update_to_new_vrf(ifp, new_vrf_id); |
b5a8894d | 205 | |
996c9314 | 206 | return 0; |
b5a8894d CS |
207 | } |
208 | ||
209 | void ospf_zebra_add(struct ospf *ospf, struct prefix_ipv4 *p, | |
996c9314 | 210 | struct ospf_route * or) |
718e3744 | 211 | { |
5fef910e RW |
212 | struct zapi_route api; |
213 | struct zapi_nexthop *api_nh; | |
d7c0a89a | 214 | uint8_t distance; |
d62a17ae | 215 | struct ospf_path *path; |
216 | struct listnode *node; | |
5fef910e | 217 | int count = 0; |
d62a17ae | 218 | |
5fef910e | 219 | memset(&api, 0, sizeof(api)); |
b5a8894d | 220 | api.vrf_id = ospf->vrf_id; |
5fef910e RW |
221 | api.type = ZEBRA_ROUTE_OSPF; |
222 | api.instance = ospf->instance; | |
223 | api.safi = SAFI_UNICAST; | |
d00061ea | 224 | |
5fef910e RW |
225 | memcpy(&api.prefix, p, sizeof(*p)); |
226 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
d00061ea | 227 | |
5fef910e RW |
228 | /* Metric value. */ |
229 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); | |
230 | if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL) | |
231 | api.metric = or->cost + or->u.ext.type2_cost; | |
232 | else if (or->path_type == OSPF_PATH_TYPE2_EXTERNAL) | |
233 | api.metric = or->u.ext.type2_cost; | |
234 | else | |
235 | api.metric = or->cost; | |
d00061ea RW |
236 | |
237 | /* Check if path type is ASE */ | |
238 | if (((or->path_type == OSPF_PATH_TYPE1_EXTERNAL) | |
239 | || (or->path_type == OSPF_PATH_TYPE2_EXTERNAL)) | |
5fef910e RW |
240 | && (or->u.ext.tag > 0) && (or->u.ext.tag <= ROUTE_TAG_MAX)) { |
241 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
242 | api.tag = or->u.ext.tag; | |
243 | } | |
d00061ea | 244 | |
5fef910e | 245 | /* Distance value. */ |
b5a8894d | 246 | distance = ospf_distance_apply(ospf, p, or); |
5fef910e RW |
247 | if (distance) { |
248 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
249 | api.distance = distance; | |
250 | } | |
d00061ea RW |
251 | |
252 | /* Nexthop, ifindex, distance and metric information. */ | |
253 | for (ALL_LIST_ELEMENTS_RO(or->paths, node, path)) { | |
a74e593b RW |
254 | if (count >= MULTIPATH_NUM) |
255 | break; | |
5fef910e | 256 | api_nh = &api.nexthops[count]; |
525c1839 | 257 | #ifdef HAVE_NETLINK |
d00061ea RW |
258 | if (path->unnumbered || (path->nexthop.s_addr != INADDR_ANY |
259 | && path->ifindex != 0)) { | |
525c1839 | 260 | #else /* HAVE_NETLINK */ |
d00061ea | 261 | if (path->nexthop.s_addr != INADDR_ANY && path->ifindex != 0) { |
5fef910e RW |
262 | #endif /* HAVE_NETLINK */ |
263 | api_nh->gate.ipv4 = path->nexthop; | |
264 | api_nh->ifindex = path->ifindex; | |
265 | api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
d00061ea | 266 | } else if (path->nexthop.s_addr != INADDR_ANY) { |
5fef910e RW |
267 | api_nh->gate.ipv4 = path->nexthop; |
268 | api_nh->type = NEXTHOP_TYPE_IPV4; | |
d00061ea | 269 | } else { |
5fef910e RW |
270 | api_nh->ifindex = path->ifindex; |
271 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
d00061ea | 272 | } |
4a7371e9 | 273 | api_nh->vrf_id = ospf->vrf_id; |
5fef910e | 274 | count++; |
72357f2b | 275 | |
d00061ea | 276 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
96065dc3 CS |
277 | char buf[2][INET_ADDRSTRLEN]; |
278 | struct interface *ifp; | |
279 | ||
280 | ifp = if_lookup_by_index(path->ifindex, ospf->vrf_id); | |
4259ea81 | 281 | |
d00061ea | 282 | zlog_debug( |
96065dc3 | 283 | "Zebra: Route add %s nexthop %s, ifindex=%d %s", |
4259ea81 MS |
284 | prefix2str(p, buf[0], sizeof(buf[0])), |
285 | inet_ntop(AF_INET, &path->nexthop, | |
286 | buf[1], sizeof(buf[1])), | |
96065dc3 | 287 | path->ifindex, ifp ? ifp->name : " "); |
d62a17ae | 288 | } |
d00061ea | 289 | } |
a74e593b | 290 | api.nexthop_num = count; |
d62a17ae | 291 | |
5fef910e | 292 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); |
718e3744 | 293 | } |
294 | ||
b5a8894d | 295 | void ospf_zebra_delete(struct ospf *ospf, struct prefix_ipv4 *p, |
996c9314 | 296 | struct ospf_route * or) |
718e3744 | 297 | { |
5fef910e | 298 | struct zapi_route api; |
d62a17ae | 299 | |
5fef910e | 300 | memset(&api, 0, sizeof(api)); |
b5a8894d | 301 | api.vrf_id = ospf->vrf_id; |
5fef910e RW |
302 | api.type = ZEBRA_ROUTE_OSPF; |
303 | api.instance = ospf->instance; | |
304 | api.safi = SAFI_UNICAST; | |
305 | memcpy(&api.prefix, p, sizeof(*p)); | |
d62a17ae | 306 | |
5fef910e | 307 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
4259ea81 MS |
308 | char buf[PREFIX2STR_BUFFER]; |
309 | zlog_debug("Zebra: Route delete %s", | |
310 | prefix2str(p, buf, sizeof(buf))); | |
ba281d3d | 311 | } |
d00061ea | 312 | |
5fef910e | 313 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); |
718e3744 | 314 | } |
315 | ||
b5a8894d | 316 | void ospf_zebra_add_discard(struct ospf *ospf, struct prefix_ipv4 *p) |
718e3744 | 317 | { |
5fef910e | 318 | struct zapi_route api; |
d62a17ae | 319 | |
5fef910e | 320 | memset(&api, 0, sizeof(api)); |
b5a8894d | 321 | api.vrf_id = ospf->vrf_id; |
d00061ea RW |
322 | api.type = ZEBRA_ROUTE_OSPF; |
323 | api.instance = ospf->instance; | |
d00061ea | 324 | api.safi = SAFI_UNICAST; |
5fef910e | 325 | memcpy(&api.prefix, p, sizeof(*p)); |
09a484dd | 326 | zapi_route_set_blackhole(&api, BLACKHOLE_NULL); |
d00061ea | 327 | |
5fef910e | 328 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); |
d00061ea | 329 | |
4259ea81 MS |
330 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
331 | char buf[PREFIX2STR_BUFFER]; | |
332 | zlog_debug("Zebra: Route add discard %s", | |
333 | prefix2str(p, buf, sizeof(buf))); | |
334 | } | |
718e3744 | 335 | } |
336 | ||
b5a8894d | 337 | void ospf_zebra_delete_discard(struct ospf *ospf, struct prefix_ipv4 *p) |
718e3744 | 338 | { |
5fef910e | 339 | struct zapi_route api; |
d62a17ae | 340 | |
5fef910e | 341 | memset(&api, 0, sizeof(api)); |
b5a8894d | 342 | api.vrf_id = ospf->vrf_id; |
d00061ea RW |
343 | api.type = ZEBRA_ROUTE_OSPF; |
344 | api.instance = ospf->instance; | |
d00061ea | 345 | api.safi = SAFI_UNICAST; |
5fef910e | 346 | memcpy(&api.prefix, p, sizeof(*p)); |
09a484dd | 347 | zapi_route_set_blackhole(&api, BLACKHOLE_NULL); |
d00061ea | 348 | |
5fef910e | 349 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); |
d00061ea | 350 | |
4259ea81 MS |
351 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
352 | char buf[PREFIX2STR_BUFFER]; | |
353 | zlog_debug("Zebra: Route delete discard %s", | |
354 | prefix2str(p, buf, sizeof(buf))); | |
355 | } | |
718e3744 | 356 | } |
357 | ||
d7c0a89a QY |
358 | struct ospf_external *ospf_external_lookup(struct ospf *ospf, uint8_t type, |
359 | unsigned short instance) | |
7c8ff89e | 360 | { |
d62a17ae | 361 | struct list *ext_list; |
362 | struct listnode *node; | |
363 | struct ospf_external *ext; | |
7c8ff89e | 364 | |
de1ac5fd | 365 | ext_list = ospf->external[type]; |
d62a17ae | 366 | if (!ext_list) |
367 | return (NULL); | |
7c8ff89e | 368 | |
d62a17ae | 369 | for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) |
370 | if (ext->instance == instance) | |
371 | return ext; | |
7c8ff89e | 372 | |
d62a17ae | 373 | return NULL; |
7c8ff89e DS |
374 | } |
375 | ||
d7c0a89a QY |
376 | struct ospf_external *ospf_external_add(struct ospf *ospf, uint8_t type, |
377 | unsigned short instance) | |
7c8ff89e | 378 | { |
d62a17ae | 379 | struct list *ext_list; |
380 | struct ospf_external *ext; | |
7c8ff89e | 381 | |
de1ac5fd | 382 | ext = ospf_external_lookup(ospf, type, instance); |
d62a17ae | 383 | if (ext) |
384 | return ext; | |
7c8ff89e | 385 | |
de1ac5fd CS |
386 | if (!ospf->external[type]) |
387 | ospf->external[type] = list_new(); | |
7c8ff89e | 388 | |
de1ac5fd | 389 | ext_list = ospf->external[type]; |
9f5dc319 | 390 | ext = XCALLOC(MTYPE_OSPF_EXTERNAL, sizeof(struct ospf_external)); |
d62a17ae | 391 | ext->instance = instance; |
392 | EXTERNAL_INFO(ext) = route_table_init(); | |
7c8ff89e | 393 | |
d62a17ae | 394 | listnode_add(ext_list, ext); |
7c8ff89e | 395 | |
d62a17ae | 396 | return ext; |
7c8ff89e DS |
397 | } |
398 | ||
d7c0a89a | 399 | void ospf_external_del(struct ospf *ospf, uint8_t type, unsigned short instance) |
7c8ff89e | 400 | { |
d62a17ae | 401 | struct ospf_external *ext; |
402 | ||
de1ac5fd | 403 | ext = ospf_external_lookup(ospf, type, instance); |
d62a17ae | 404 | |
405 | if (ext) { | |
406 | if (EXTERNAL_INFO(ext)) | |
407 | route_table_finish(EXTERNAL_INFO(ext)); | |
408 | ||
de1ac5fd CS |
409 | listnode_delete(ospf->external[type], ext); |
410 | ||
411 | if (!ospf->external[type]->count) | |
6a154c88 | 412 | list_delete(&ospf->external[type]); |
de1ac5fd | 413 | |
7f586094 | 414 | XFREE(MTYPE_OSPF_EXTERNAL, ext); |
d62a17ae | 415 | } |
7c8ff89e DS |
416 | } |
417 | ||
d7c0a89a QY |
418 | struct ospf_redist *ospf_redist_lookup(struct ospf *ospf, uint8_t type, |
419 | unsigned short instance) | |
7c8ff89e | 420 | { |
d62a17ae | 421 | struct list *red_list; |
422 | struct listnode *node; | |
423 | struct ospf_redist *red; | |
7c8ff89e | 424 | |
d62a17ae | 425 | red_list = ospf->redist[type]; |
426 | if (!red_list) | |
427 | return (NULL); | |
7c8ff89e | 428 | |
d62a17ae | 429 | for (ALL_LIST_ELEMENTS_RO(red_list, node, red)) |
430 | if (red->instance == instance) | |
431 | return red; | |
7c8ff89e | 432 | |
d62a17ae | 433 | return NULL; |
7c8ff89e DS |
434 | } |
435 | ||
d7c0a89a QY |
436 | struct ospf_redist *ospf_redist_add(struct ospf *ospf, uint8_t type, |
437 | unsigned short instance) | |
7c8ff89e | 438 | { |
d62a17ae | 439 | struct list *red_list; |
440 | struct ospf_redist *red; | |
7c8ff89e | 441 | |
d62a17ae | 442 | red = ospf_redist_lookup(ospf, type, instance); |
443 | if (red) | |
444 | return red; | |
7c8ff89e | 445 | |
d62a17ae | 446 | if (!ospf->redist[type]) |
447 | ospf->redist[type] = list_new(); | |
7c8ff89e | 448 | |
d62a17ae | 449 | red_list = ospf->redist[type]; |
9f5dc319 | 450 | red = XCALLOC(MTYPE_OSPF_REDISTRIBUTE, sizeof(struct ospf_redist)); |
d62a17ae | 451 | red->instance = instance; |
452 | red->dmetric.type = -1; | |
453 | red->dmetric.value = -1; | |
1fb93326 | 454 | ROUTEMAP_NAME(red) = NULL; |
455 | ROUTEMAP(red) = NULL; | |
7c8ff89e | 456 | |
d62a17ae | 457 | listnode_add(red_list, red); |
7c8ff89e | 458 | |
d62a17ae | 459 | return red; |
7c8ff89e DS |
460 | } |
461 | ||
d7c0a89a | 462 | void ospf_redist_del(struct ospf *ospf, uint8_t type, unsigned short instance) |
7c8ff89e | 463 | { |
d62a17ae | 464 | struct ospf_redist *red; |
465 | ||
466 | red = ospf_redist_lookup(ospf, type, instance); | |
467 | ||
468 | if (red) { | |
469 | listnode_delete(ospf->redist[type], red); | |
470 | if (!ospf->redist[type]->count) { | |
6a154c88 | 471 | list_delete(&ospf->redist[type]); |
d62a17ae | 472 | } |
97a69b31 | 473 | ospf_routemap_unset(red); |
7f586094 | 474 | XFREE(MTYPE_OSPF_REDISTRIBUTE, red); |
d62a17ae | 475 | } |
7c8ff89e DS |
476 | } |
477 | ||
478 | ||
d7c0a89a QY |
479 | int ospf_is_type_redistributed(struct ospf *ospf, int type, |
480 | unsigned short instance) | |
718e3744 | 481 | { |
d62a17ae | 482 | return (DEFAULT_ROUTE_TYPE(type) |
49db7a7b | 483 | ? vrf_bitmap_check(zclient->default_information[AFI_IP], |
b5a8894d | 484 | ospf->vrf_id) |
d62a17ae | 485 | : ((instance |
486 | && redist_check_instance( | |
487 | &zclient->mi_redist[AFI_IP][type], | |
488 | instance)) | |
489 | || (!instance | |
490 | && vrf_bitmap_check( | |
491 | zclient->redist[AFI_IP][type], | |
b5a8894d | 492 | ospf->vrf_id)))); |
718e3744 | 493 | } |
494 | ||
d7c0a89a | 495 | int ospf_redistribute_set(struct ospf *ospf, int type, unsigned short instance, |
d62a17ae | 496 | int mtype, int mvalue) |
718e3744 | 497 | { |
d62a17ae | 498 | int force = 0; |
499 | struct ospf_redist *red; | |
500 | ||
501 | red = ospf_redist_lookup(ospf, type, instance); | |
162dbe41 | 502 | |
503 | if (red == NULL) { | |
504 | zlog_err( | |
505 | "Redistribute[%s][%d]: Lookup failed Type[%d] , Metric[%d]", | |
506 | ospf_redist_string(type), instance, | |
507 | metric_type(ospf, type, instance), | |
508 | metric_value(ospf, type, instance)); | |
509 | return CMD_WARNING_CONFIG_FAILED; | |
510 | } | |
511 | ||
b5a8894d | 512 | if (ospf_is_type_redistributed(ospf, type, instance)) { |
d62a17ae | 513 | if (mtype != red->dmetric.type) { |
514 | red->dmetric.type = mtype; | |
515 | force = LSA_REFRESH_FORCE; | |
516 | } | |
517 | if (mvalue != red->dmetric.value) { | |
518 | red->dmetric.value = mvalue; | |
519 | force = LSA_REFRESH_FORCE; | |
520 | } | |
521 | ||
522 | ospf_external_lsa_refresh_type(ospf, type, instance, force); | |
523 | ||
524 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) | |
525 | zlog_debug( | |
526 | "Redistribute[%s][%d]: Refresh Type[%d], Metric[%d]", | |
527 | ospf_redist_string(type), instance, | |
528 | metric_type(ospf, type, instance), | |
529 | metric_value(ospf, type, instance)); | |
530 | ||
531 | return CMD_SUCCESS; | |
532 | } | |
533 | ||
534 | red->dmetric.type = mtype; | |
535 | red->dmetric.value = mvalue; | |
536 | ||
de1ac5fd | 537 | ospf_external_add(ospf, type, instance); |
d62a17ae | 538 | |
539 | zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, | |
b5a8894d | 540 | instance, ospf->vrf_id); |
d62a17ae | 541 | |
542 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) | |
996c9314 LB |
543 | zlog_debug( |
544 | "Redistribute[%s][%d] vrf id %u: Start Type[%d], Metric[%d]", | |
545 | ospf_redist_string(type), instance, ospf->vrf_id, | |
546 | metric_type(ospf, type, instance), | |
547 | metric_value(ospf, type, instance)); | |
d62a17ae | 548 | |
549 | ospf_asbr_status_update(ospf, ++ospf->redistribute); | |
550 | ||
551 | return CMD_SUCCESS; | |
718e3744 | 552 | } |
553 | ||
d7c0a89a QY |
554 | int ospf_redistribute_unset(struct ospf *ospf, int type, |
555 | unsigned short instance) | |
718e3744 | 556 | { |
d62a17ae | 557 | if (type == zclient->redist_default && instance == zclient->instance) |
558 | return CMD_SUCCESS; | |
718e3744 | 559 | |
b5a8894d | 560 | if (!ospf_is_type_redistributed(ospf, type, instance)) |
d62a17ae | 561 | return CMD_SUCCESS; |
718e3744 | 562 | |
d62a17ae | 563 | zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP, type, |
b5a8894d | 564 | instance, ospf->vrf_id); |
cf795c5d | 565 | |
d62a17ae | 566 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) |
b5a8894d CS |
567 | zlog_debug("Redistribute[%s][%d] vrf id %u: Stop", |
568 | ospf_redist_string(type), instance, ospf->vrf_id); | |
718e3744 | 569 | |
d62a17ae | 570 | /* Remove the routes from OSPF table. */ |
571 | ospf_redistribute_withdraw(ospf, type, instance); | |
7c8ff89e | 572 | |
de1ac5fd | 573 | ospf_external_del(ospf, type, instance); |
718e3744 | 574 | |
d62a17ae | 575 | ospf_asbr_status_update(ospf, --ospf->redistribute); |
718e3744 | 576 | |
d62a17ae | 577 | return CMD_SUCCESS; |
718e3744 | 578 | } |
579 | ||
d62a17ae | 580 | int ospf_redistribute_default_set(struct ospf *ospf, int originate, int mtype, |
581 | int mvalue) | |
718e3744 | 582 | { |
1fb93326 | 583 | struct prefix_ipv4 p; |
584 | struct in_addr nexthop; | |
585 | int cur_originate = ospf->default_originate; | |
d5eac1e0 | 586 | const char *type_str = NULL; |
1fb93326 | 587 | |
975a328e | 588 | nexthop.s_addr = INADDR_ANY; |
1fb93326 | 589 | p.family = AF_INET; |
975a328e | 590 | p.prefix.s_addr = INADDR_ANY; |
1fb93326 | 591 | p.prefixlen = 0; |
592 | ||
d62a17ae | 593 | ospf->default_originate = originate; |
020709f9 | 594 | |
d5eac1e0 | 595 | if (cur_originate == originate) { |
1fb93326 | 596 | /* Refresh the lsa since metric might different */ |
d62a17ae | 597 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) |
598 | zlog_debug( | |
599 | "Redistribute[%s]: Refresh Type[%d], Metric[%d]", | |
600 | ospf_redist_string(DEFAULT_ROUTE), | |
601 | metric_type(ospf, DEFAULT_ROUTE, 0), | |
602 | metric_value(ospf, DEFAULT_ROUTE, 0)); | |
718e3744 | 603 | |
1fb93326 | 604 | ospf_external_lsa_refresh_default(ospf); |
d5eac1e0 | 605 | return CMD_SUCCESS; |
1fb93326 | 606 | } |
718e3744 | 607 | |
d5eac1e0 DL |
608 | switch (cur_originate) { |
609 | case DEFAULT_ORIGINATE_NONE: | |
610 | break; | |
611 | case DEFAULT_ORIGINATE_ZEBRA: | |
1fb93326 | 612 | zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, |
49db7a7b | 613 | zclient, AFI_IP, ospf->vrf_id); |
d5eac1e0 DL |
614 | ospf->redistribute--; |
615 | break; | |
616 | case DEFAULT_ORIGINATE_ALWAYS: | |
617 | ospf_external_info_delete(ospf, DEFAULT_ROUTE, 0, p); | |
618 | ospf_external_del(ospf, DEFAULT_ROUTE, 0); | |
619 | ospf->redistribute--; | |
620 | break; | |
1fb93326 | 621 | } |
718e3744 | 622 | |
d5eac1e0 DL |
623 | switch (originate) { |
624 | case DEFAULT_ORIGINATE_NONE: | |
625 | type_str = "none"; | |
626 | break; | |
627 | case DEFAULT_ORIGINATE_ZEBRA: | |
628 | type_str = "normal"; | |
629 | ospf->redistribute++; | |
630 | zclient_redistribute_default(ZEBRA_REDISTRIBUTE_DEFAULT_ADD, | |
631 | zclient, AFI_IP, ospf->vrf_id); | |
632 | break; | |
633 | case DEFAULT_ORIGINATE_ALWAYS: | |
634 | type_str = "always"; | |
635 | ospf->redistribute++; | |
636 | ospf_external_add(ospf, DEFAULT_ROUTE, 0); | |
637 | ospf_external_info_add(ospf, DEFAULT_ROUTE, 0, p, 0, nexthop, | |
638 | 0); | |
639 | break; | |
640 | } | |
718e3744 | 641 | |
d62a17ae | 642 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) |
d5eac1e0 DL |
643 | zlog_debug("Redistribute[DEFAULT]: %s Type[%d], Metric[%d]", |
644 | type_str, | |
645 | metric_type(ospf, DEFAULT_ROUTE, 0), | |
646 | metric_value(ospf, DEFAULT_ROUTE, 0)); | |
718e3744 | 647 | |
d5eac1e0 DL |
648 | ospf_external_lsa_refresh_default(ospf); |
649 | ospf_asbr_status_update(ospf, ospf->redistribute); | |
d62a17ae | 650 | return CMD_SUCCESS; |
718e3744 | 651 | } |
652 | ||
d62a17ae | 653 | static int ospf_external_lsa_originate_check(struct ospf *ospf, |
654 | struct external_info *ei) | |
718e3744 | 655 | { |
d62a17ae | 656 | /* If prefix is multicast, then do not originate LSA. */ |
657 | if (IN_MULTICAST(htonl(ei->p.prefix.s_addr))) { | |
658 | zlog_info( | |
659 | "LSA[Type5:%s]: Not originate AS-external-LSA, " | |
660 | "Prefix belongs multicast", | |
661 | inet_ntoa(ei->p.prefix)); | |
662 | return 0; | |
663 | } | |
664 | ||
665 | /* Take care of default-originate. */ | |
666 | if (is_prefix_default(&ei->p)) | |
667 | if (ospf->default_originate == DEFAULT_ORIGINATE_NONE) { | |
668 | zlog_info( | |
669 | "LSA[Type5:0.0.0.0]: Not originate AS-external-LSA " | |
670 | "for default"); | |
671 | return 0; | |
672 | } | |
673 | ||
674 | return 1; | |
718e3744 | 675 | } |
676 | ||
677 | /* If connected prefix is OSPF enable interface, then do not announce. */ | |
d62a17ae | 678 | int ospf_distribute_check_connected(struct ospf *ospf, struct external_info *ei) |
718e3744 | 679 | { |
d62a17ae | 680 | struct listnode *node; |
681 | struct ospf_interface *oi; | |
718e3744 | 682 | |
718e3744 | 683 | |
d62a17ae | 684 | for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) |
685 | if (prefix_match(oi->address, (struct prefix *)&ei->p)) | |
686 | return 0; | |
687 | return 1; | |
718e3744 | 688 | } |
689 | ||
690 | /* return 1 if external LSA must be originated, 0 otherwise */ | |
d62a17ae | 691 | int ospf_redistribute_check(struct ospf *ospf, struct external_info *ei, |
692 | int *changed) | |
718e3744 | 693 | { |
d62a17ae | 694 | struct route_map_set_values save_values; |
695 | struct prefix_ipv4 *p = &ei->p; | |
696 | struct ospf_redist *red; | |
d7c0a89a QY |
697 | uint8_t type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type; |
698 | unsigned short instance = is_prefix_default(&ei->p) ? 0 : ei->instance; | |
d62a17ae | 699 | |
700 | if (changed) | |
701 | *changed = 0; | |
702 | ||
703 | if (!ospf_external_lsa_originate_check(ospf, ei)) | |
704 | return 0; | |
705 | ||
706 | /* Take care connected route. */ | |
707 | if (type == ZEBRA_ROUTE_CONNECT | |
708 | && !ospf_distribute_check_connected(ospf, ei)) | |
709 | return 0; | |
710 | ||
711 | if (!DEFAULT_ROUTE_TYPE(type) && DISTRIBUTE_NAME(ospf, type)) | |
712 | /* distirbute-list exists, but access-list may not? */ | |
713 | if (DISTRIBUTE_LIST(ospf, type)) | |
714 | if (access_list_apply(DISTRIBUTE_LIST(ospf, type), p) | |
715 | == FILTER_DENY) { | |
4259ea81 MS |
716 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
717 | char buf[PREFIX2STR_BUFFER]; | |
d62a17ae | 718 | zlog_debug( |
4259ea81 | 719 | "Redistribute[%s]: %s filtered by distribute-list.", |
d62a17ae | 720 | ospf_redist_string(type), |
4259ea81 MS |
721 | prefix2str(p, buf, sizeof(buf))); |
722 | } | |
d62a17ae | 723 | return 0; |
724 | } | |
725 | ||
726 | save_values = ei->route_map_set; | |
727 | ospf_reset_route_map_set_values(&ei->route_map_set); | |
728 | ||
729 | /* apply route-map if needed */ | |
730 | red = ospf_redist_lookup(ospf, type, instance); | |
731 | if (red && ROUTEMAP_NAME(red)) { | |
b68885f9 | 732 | route_map_result_t ret; |
d62a17ae | 733 | |
734 | ret = route_map_apply(ROUTEMAP(red), (struct prefix *)p, | |
735 | RMAP_OSPF, ei); | |
736 | ||
737 | if (ret == RMAP_DENYMATCH) { | |
738 | ei->route_map_set = save_values; | |
4259ea81 MS |
739 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
740 | char buf[PREFIX2STR_BUFFER]; | |
d62a17ae | 741 | zlog_debug( |
4259ea81 | 742 | "Redistribute[%s]: %s filtered by route-map.", |
d62a17ae | 743 | ospf_redist_string(type), |
4259ea81 MS |
744 | prefix2str(p, buf, sizeof(buf))); |
745 | } | |
d62a17ae | 746 | return 0; |
747 | } | |
748 | ||
749 | /* check if 'route-map set' changed something */ | |
750 | if (changed) | |
751 | *changed = !ospf_route_map_set_compare( | |
752 | &ei->route_map_set, &save_values); | |
753 | } | |
754 | ||
755 | return 1; | |
718e3744 | 756 | } |
757 | ||
758 | /* OSPF route-map set for redistribution */ | |
d62a17ae | 759 | void ospf_routemap_set(struct ospf_redist *red, const char *name) |
718e3744 | 760 | { |
93d836e6 | 761 | if (ROUTEMAP_NAME(red)) { |
762 | route_map_counter_decrement(ROUTEMAP(red)); | |
d62a17ae | 763 | free(ROUTEMAP_NAME(red)); |
93d836e6 | 764 | } |
718e3744 | 765 | |
d62a17ae | 766 | ROUTEMAP_NAME(red) = strdup(name); |
767 | ROUTEMAP(red) = route_map_lookup_by_name(name); | |
93d836e6 | 768 | route_map_counter_increment(ROUTEMAP(red)); |
718e3744 | 769 | } |
770 | ||
d62a17ae | 771 | void ospf_routemap_unset(struct ospf_redist *red) |
718e3744 | 772 | { |
93d836e6 | 773 | if (ROUTEMAP_NAME(red)) { |
774 | route_map_counter_decrement(ROUTEMAP(red)); | |
d62a17ae | 775 | free(ROUTEMAP_NAME(red)); |
93d836e6 | 776 | } |
718e3744 | 777 | |
d62a17ae | 778 | ROUTEMAP_NAME(red) = NULL; |
779 | ROUTEMAP(red) = NULL; | |
718e3744 | 780 | } |
781 | ||
782 | /* Zebra route add and delete treatment. */ | |
121f9dee | 783 | static int ospf_zebra_read_route(ZAPI_CALLBACK_ARGS) |
718e3744 | 784 | { |
74489921 RW |
785 | struct zapi_route api; |
786 | struct prefix_ipv4 p; | |
d62a17ae | 787 | unsigned long ifindex; |
788 | struct in_addr nexthop; | |
d62a17ae | 789 | struct external_info *ei; |
790 | struct ospf *ospf; | |
791 | int i; | |
1fb93326 | 792 | uint8_t rt_type; |
d62a17ae | 793 | |
b5a8894d | 794 | ospf = ospf_lookup_by_vrf_id(vrf_id); |
74489921 RW |
795 | if (ospf == NULL) |
796 | return 0; | |
d62a17ae | 797 | |
74489921 RW |
798 | if (zapi_route_decode(zclient->ibuf, &api) < 0) |
799 | return -1; | |
d62a17ae | 800 | |
74489921 RW |
801 | ifindex = api.nexthops[0].ifindex; |
802 | nexthop = api.nexthops[0].gate.ipv4; | |
1fb93326 | 803 | rt_type = api.type; |
d62a17ae | 804 | |
74489921 | 805 | memcpy(&p, &api.prefix, sizeof(p)); |
d62a17ae | 806 | if (IPV4_NET127(ntohl(p.prefix.s_addr))) |
807 | return 0; | |
808 | ||
1fb93326 | 809 | /* Re-destributed route is default route. |
810 | * Here, route type is used as 'ZEBRA_ROUTE_KERNEL' for | |
811 | * updating ex-info. But in resetting (no default-info | |
812 | * originate)ZEBRA_ROUTE_MAX is used to delete the ex-info. | |
813 | * Resolved this inconsistency by maintaining same route type. | |
814 | */ | |
815 | if (is_prefix_default(&p)) | |
816 | rt_type = DEFAULT_ROUTE; | |
817 | ||
5c780bc9 PZ |
818 | if (IS_DEBUG_OSPF(zebra, ZEBRA_REDISTRIBUTE)) { |
819 | char buf_prefix[PREFIX_STRLEN]; | |
820 | prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix)); | |
821 | ||
ae4080c5 MS |
822 | zlog_debug("%s: cmd %s from client %s: vrf_id %d, p %s", |
823 | __func__, zserv_command_string(cmd), | |
5c780bc9 PZ |
824 | zebra_route_string(api.type), vrf_id, buf_prefix); |
825 | } | |
826 | ||
121f9dee | 827 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) { |
d62a17ae | 828 | /* XXX|HACK|TODO|FIXME: |
09a484dd DL |
829 | * Maybe we should ignore reject/blackhole routes? Testing |
830 | * shows that there is no problems though and this is only way | |
831 | * to "summarize" routes in ASBR at the moment. Maybe we need | |
832 | * just a better generalised solution for these types? | |
d62a17ae | 833 | */ |
834 | ||
74489921 | 835 | /* Protocol tag overwrites all other tag value sent by zebra */ |
1fb93326 | 836 | if (ospf->dtag[rt_type] > 0) |
837 | api.tag = ospf->dtag[rt_type]; | |
d62a17ae | 838 | |
839 | /* | |
840 | * Given zebra sends update for a prefix via ADD message, it | |
841 | * should | |
842 | * be considered as an implicit DEL for that prefix with other | |
843 | * source | |
844 | * types. | |
845 | */ | |
1fb93326 | 846 | for (i = 0; i <= ZEBRA_ROUTE_MAX; i++) |
847 | if (i != rt_type) | |
996c9314 LB |
848 | ospf_external_info_delete(ospf, i, api.instance, |
849 | p); | |
d62a17ae | 850 | |
1fb93326 | 851 | ei = ospf_external_info_add(ospf, rt_type, api.instance, p, |
de1ac5fd | 852 | ifindex, nexthop, api.tag); |
d62a17ae | 853 | if (ei == NULL) { |
854 | /* Nothing has changed, so nothing to do; return */ | |
855 | return 0; | |
856 | } | |
975a328e | 857 | if (ospf->router_id.s_addr != INADDR_ANY) { |
d62a17ae | 858 | if (ei) { |
859 | if (is_prefix_default(&p)) | |
860 | ospf_external_lsa_refresh_default(ospf); | |
861 | else { | |
862 | struct ospf_lsa *current; | |
863 | ||
864 | current = ospf_external_info_find_lsa( | |
865 | ospf, &ei->p); | |
866 | if (!current) | |
867 | ospf_external_lsa_originate( | |
868 | ospf, ei); | |
869 | else { | |
870 | if (IS_DEBUG_OSPF( | |
871 | zebra, | |
872 | ZEBRA_REDISTRIBUTE)) | |
873 | zlog_debug( | |
74489921 | 874 | "ospf_zebra_read_route() : %s refreshing LSA", |
d62a17ae | 875 | inet_ntoa( |
876 | p.prefix)); | |
877 | ospf_external_lsa_refresh( | |
878 | ospf, current, ei, | |
879 | LSA_REFRESH_FORCE); | |
880 | } | |
881 | } | |
882 | } | |
883 | } | |
121f9dee | 884 | } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */ |
5048fe14 | 885 | { |
1fb93326 | 886 | ospf_external_info_delete(ospf, rt_type, api.instance, p); |
d62a17ae | 887 | if (is_prefix_default(&p)) |
888 | ospf_external_lsa_refresh_default(ospf); | |
889 | else | |
1fb93326 | 890 | ospf_external_lsa_flush(ospf, rt_type, &p, |
d62a17ae | 891 | ifindex /*, nexthop */); |
5048fe14 | 892 | } |
d62a17ae | 893 | |
894 | return 0; | |
718e3744 | 895 | } |
6b0655a2 | 896 | |
cf795c5d | 897 | |
d62a17ae | 898 | int ospf_distribute_list_out_set(struct ospf *ospf, int type, const char *name) |
718e3744 | 899 | { |
d62a17ae | 900 | /* Lookup access-list for distribute-list. */ |
901 | DISTRIBUTE_LIST(ospf, type) = access_list_lookup(AFI_IP, name); | |
718e3744 | 902 | |
d62a17ae | 903 | /* Clear previous distribute-name. */ |
904 | if (DISTRIBUTE_NAME(ospf, type)) | |
905 | free(DISTRIBUTE_NAME(ospf, type)); | |
718e3744 | 906 | |
d62a17ae | 907 | /* Set distribute-name. */ |
908 | DISTRIBUTE_NAME(ospf, type) = strdup(name); | |
718e3744 | 909 | |
d62a17ae | 910 | /* If access-list have been set, schedule update timer. */ |
911 | if (DISTRIBUTE_LIST(ospf, type)) | |
912 | ospf_distribute_list_update(ospf, type, 0); | |
718e3744 | 913 | |
d62a17ae | 914 | return CMD_SUCCESS; |
718e3744 | 915 | } |
916 | ||
d62a17ae | 917 | int ospf_distribute_list_out_unset(struct ospf *ospf, int type, |
918 | const char *name) | |
718e3744 | 919 | { |
d62a17ae | 920 | /* Schedule update timer. */ |
921 | if (DISTRIBUTE_LIST(ospf, type)) | |
922 | ospf_distribute_list_update(ospf, type, 0); | |
718e3744 | 923 | |
d62a17ae | 924 | /* Unset distribute-list. */ |
925 | DISTRIBUTE_LIST(ospf, type) = NULL; | |
718e3744 | 926 | |
d62a17ae | 927 | /* Clear distribute-name. */ |
928 | if (DISTRIBUTE_NAME(ospf, type)) | |
929 | free(DISTRIBUTE_NAME(ospf, type)); | |
cf795c5d | 930 | |
d62a17ae | 931 | DISTRIBUTE_NAME(ospf, type) = NULL; |
718e3744 | 932 | |
d62a17ae | 933 | return CMD_SUCCESS; |
718e3744 | 934 | } |
935 | ||
936 | /* distribute-list update timer. */ | |
d62a17ae | 937 | static int ospf_distribute_list_update_timer(struct thread *thread) |
718e3744 | 938 | { |
d62a17ae | 939 | struct route_node *rn; |
940 | struct external_info *ei; | |
941 | struct route_table *rt; | |
942 | struct ospf_lsa *lsa; | |
b5a8894d CS |
943 | int type, default_refresh = 0, arg_type; |
944 | struct ospf *ospf = NULL; | |
996c9314 | 945 | void **arg = THREAD_ARG(thread); |
b5a8894d CS |
946 | |
947 | ospf = (struct ospf *)arg[0]; | |
948 | arg_type = (int)(intptr_t)arg[1]; | |
d62a17ae | 949 | |
d62a17ae | 950 | if (ospf == NULL) |
951 | return 0; | |
952 | ||
953 | ospf->t_distribute_update = NULL; | |
954 | ||
955 | zlog_info("Zebra[Redistribute]: distribute-list update timer fired!"); | |
956 | ||
b5a8894d | 957 | if (IS_DEBUG_OSPF_EVENT) { |
996c9314 LB |
958 | zlog_debug( |
959 | "%s: ospf distribute-list update arg_type %d vrf %s id %d", | |
5e81f5dd DS |
960 | __func__, arg_type, ospf_vrf_id_to_name(ospf->vrf_id), |
961 | ospf->vrf_id); | |
b5a8894d CS |
962 | } |
963 | ||
d62a17ae | 964 | /* foreach all external info. */ |
965 | for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) { | |
966 | struct list *ext_list; | |
967 | struct listnode *node; | |
968 | struct ospf_external *ext; | |
969 | ||
de1ac5fd | 970 | ext_list = ospf->external[type]; |
d62a17ae | 971 | if (!ext_list) |
972 | continue; | |
973 | ||
974 | for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) { | |
975 | rt = ext->external_info; | |
976 | if (!rt) | |
977 | continue; | |
978 | for (rn = route_top(rt); rn; rn = route_next(rn)) | |
979 | if ((ei = rn->info) != NULL) { | |
980 | if (is_prefix_default(&ei->p)) | |
981 | default_refresh = 1; | |
982 | else if ( | |
983 | (lsa = ospf_external_info_find_lsa( | |
984 | ospf, &ei->p))) | |
985 | ospf_external_lsa_refresh( | |
986 | ospf, lsa, ei, | |
987 | LSA_REFRESH_IF_CHANGED); | |
988 | else | |
989 | ospf_external_lsa_originate( | |
990 | ospf, ei); | |
991 | } | |
992 | } | |
993 | } | |
994 | if (default_refresh) | |
995 | ospf_external_lsa_refresh_default(ospf); | |
b5a8894d CS |
996 | |
997 | XFREE(MTYPE_OSPF_DIST_ARGS, arg); | |
d62a17ae | 998 | return 0; |
718e3744 | 999 | } |
1000 | ||
718e3744 | 1001 | /* Update distribute-list and set timer to apply access-list. */ |
d7c0a89a QY |
1002 | void ospf_distribute_list_update(struct ospf *ospf, int type, |
1003 | unsigned short instance) | |
718e3744 | 1004 | { |
d62a17ae | 1005 | struct ospf_external *ext; |
996c9314 | 1006 | void **args = XCALLOC(MTYPE_OSPF_DIST_ARGS, sizeof(void *) * 2); |
d62a17ae | 1007 | |
b5a8894d | 1008 | args[0] = ospf; |
996c9314 | 1009 | args[1] = (void *)((ptrdiff_t)type); |
b5a8894d | 1010 | |
d62a17ae | 1011 | /* External info does not exist. */ |
de1ac5fd | 1012 | ext = ospf_external_lookup(ospf, type, instance); |
6e3e2c6d | 1013 | if (!ext || !EXTERNAL_INFO(ext)) { |
a68730c6 | 1014 | XFREE(MTYPE_OSPF_DIST_ARGS, args); |
d62a17ae | 1015 | return; |
a68730c6 | 1016 | } |
d62a17ae | 1017 | |
1018 | /* If exists previously invoked thread, then let it continue. */ | |
a68730c6 DS |
1019 | if (ospf->t_distribute_update) { |
1020 | XFREE(MTYPE_OSPF_DIST_ARGS, args); | |
d62a17ae | 1021 | return; |
a68730c6 | 1022 | } |
d62a17ae | 1023 | |
1024 | /* Set timer. */ | |
1025 | ospf->t_distribute_update = NULL; | |
1026 | thread_add_timer_msec(master, ospf_distribute_list_update_timer, | |
b5a8894d | 1027 | (void **)args, ospf->min_ls_interval, |
d62a17ae | 1028 | &ospf->t_distribute_update); |
718e3744 | 1029 | } |
1030 | ||
1031 | /* If access-list is updated, apply some check. */ | |
d62a17ae | 1032 | static void ospf_filter_update(struct access_list *access) |
718e3744 | 1033 | { |
d62a17ae | 1034 | struct ospf *ospf; |
1035 | int type; | |
1036 | int abr_inv = 0; | |
1037 | struct ospf_area *area; | |
b5a8894d | 1038 | struct listnode *node, *n1; |
d62a17ae | 1039 | |
1040 | /* If OSPF instance does not exist, return right now. */ | |
b5a8894d | 1041 | if (listcount(om->ospf) == 0) |
d62a17ae | 1042 | return; |
1043 | ||
b5a8894d | 1044 | /* Iterate all ospf [VRF] instances */ |
43b8d1d8 | 1045 | for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) { |
b5a8894d CS |
1046 | /* Update distribute-list, and apply filter. */ |
1047 | for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) { | |
1048 | struct list *red_list; | |
b5a8894d CS |
1049 | struct ospf_redist *red; |
1050 | ||
1051 | red_list = ospf->redist[type]; | |
1052 | if (red_list) | |
996c9314 LB |
1053 | for (ALL_LIST_ELEMENTS_RO(red_list, node, |
1054 | red)) { | |
b5a8894d | 1055 | if (ROUTEMAP(red)) { |
996c9314 LB |
1056 | /* if route-map is not NULL it |
1057 | * may be | |
b5a8894d CS |
1058 | * using this access list */ |
1059 | ospf_distribute_list_update( | |
996c9314 LB |
1060 | ospf, type, |
1061 | red->instance); | |
b5a8894d | 1062 | } |
d62a17ae | 1063 | } |
d62a17ae | 1064 | |
b5a8894d CS |
1065 | /* There is place for route-map for default-information |
1066 | * (ZEBRA_ROUTE_MAX), | |
1067 | * but no distribute list. */ | |
1068 | if (type == ZEBRA_ROUTE_MAX) | |
1069 | break; | |
1070 | ||
1071 | if (DISTRIBUTE_NAME(ospf, type)) { | |
1072 | /* Keep old access-list for distribute-list. */ | |
996c9314 LB |
1073 | struct access_list *old = |
1074 | DISTRIBUTE_LIST(ospf, type); | |
b5a8894d CS |
1075 | |
1076 | /* Update access-list for distribute-list. */ | |
996c9314 LB |
1077 | DISTRIBUTE_LIST(ospf, type) = |
1078 | access_list_lookup( | |
1079 | AFI_IP, | |
1080 | DISTRIBUTE_NAME(ospf, type)); | |
b5a8894d CS |
1081 | |
1082 | /* No update for this distribute type. */ | |
996c9314 LB |
1083 | if (old == NULL |
1084 | && DISTRIBUTE_LIST(ospf, type) == NULL) | |
b5a8894d CS |
1085 | continue; |
1086 | ||
1087 | /* Schedule distribute-list update timer. */ | |
1088 | if (DISTRIBUTE_LIST(ospf, type) == NULL | |
996c9314 LB |
1089 | || strcmp(DISTRIBUTE_NAME(ospf, type), |
1090 | access->name) | |
1091 | == 0) | |
1092 | ospf_distribute_list_update(ospf, type, | |
1093 | 0); | |
b5a8894d | 1094 | } |
d62a17ae | 1095 | } |
d62a17ae | 1096 | |
b5a8894d CS |
1097 | /* Update Area access-list. */ |
1098 | for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) { | |
1099 | if (EXPORT_NAME(area)) { | |
1100 | EXPORT_LIST(area) = NULL; | |
1101 | abr_inv++; | |
1102 | } | |
d62a17ae | 1103 | |
b5a8894d CS |
1104 | if (IMPORT_NAME(area)) { |
1105 | IMPORT_LIST(area) = NULL; | |
1106 | abr_inv++; | |
1107 | } | |
d62a17ae | 1108 | } |
d62a17ae | 1109 | |
b5a8894d CS |
1110 | /* Schedule ABR tasks -- this will be changed -- takada. */ |
1111 | if (IS_OSPF_ABR(ospf) && abr_inv) | |
1112 | ospf_schedule_abr_task(ospf); | |
1113 | } | |
718e3744 | 1114 | } |
dd669bb0 | 1115 | |
1116 | /* If prefix-list is updated, do some updates. */ | |
d62a17ae | 1117 | void ospf_prefix_list_update(struct prefix_list *plist) |
dd669bb0 | 1118 | { |
b5a8894d | 1119 | struct ospf *ospf = NULL; |
d62a17ae | 1120 | int type; |
1121 | int abr_inv = 0; | |
1122 | struct ospf_area *area; | |
b5a8894d | 1123 | struct listnode *node, *n1; |
d62a17ae | 1124 | |
1125 | /* If OSPF instatnce does not exist, return right now. */ | |
b5a8894d | 1126 | if (listcount(om->ospf) == 0) |
d62a17ae | 1127 | return; |
1128 | ||
b5a8894d CS |
1129 | /* Iterate all ospf [VRF] instances */ |
1130 | for (ALL_LIST_ELEMENTS_RO(om->ospf, n1, ospf)) { | |
1131 | ||
1132 | /* Update all route-maps which are used | |
1133 | * as redistribution filters. | |
1134 | * They might use prefix-list. | |
1135 | */ | |
1136 | for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) { | |
1137 | struct list *red_list; | |
b5a8894d CS |
1138 | struct ospf_redist *red; |
1139 | ||
1140 | red_list = ospf->redist[type]; | |
1141 | if (red_list) { | |
996c9314 LB |
1142 | for (ALL_LIST_ELEMENTS_RO(red_list, node, |
1143 | red)) { | |
b5a8894d CS |
1144 | if (ROUTEMAP(red)) { |
1145 | /* if route-map is not NULL | |
1146 | * it may be using | |
1147 | * this prefix list */ | |
1148 | ospf_distribute_list_update( | |
1149 | ospf, type, | |
1150 | red->instance); | |
1151 | } | |
d62a17ae | 1152 | } |
1153 | } | |
b5a8894d | 1154 | } |
d62a17ae | 1155 | |
b5a8894d CS |
1156 | /* Update area filter-lists. */ |
1157 | for (ALL_LIST_ELEMENTS_RO(ospf->areas, node, area)) { | |
1158 | /* Update filter-list in. */ | |
1159 | if (PREFIX_NAME_IN(area)) | |
1160 | if (strcmp(PREFIX_NAME_IN(area), | |
996c9314 LB |
1161 | prefix_list_name(plist)) |
1162 | == 0) { | |
b5a8894d CS |
1163 | PREFIX_LIST_IN(area) = |
1164 | prefix_list_lookup( | |
996c9314 LB |
1165 | AFI_IP, |
1166 | PREFIX_NAME_IN(area)); | |
b5a8894d CS |
1167 | abr_inv++; |
1168 | } | |
d62a17ae | 1169 | |
b5a8894d CS |
1170 | /* Update filter-list out. */ |
1171 | if (PREFIX_NAME_OUT(area)) | |
1172 | if (strcmp(PREFIX_NAME_OUT(area), | |
996c9314 LB |
1173 | prefix_list_name(plist)) |
1174 | == 0) { | |
b5a8894d CS |
1175 | PREFIX_LIST_IN(area) = |
1176 | prefix_list_lookup( | |
996c9314 LB |
1177 | AFI_IP, |
1178 | PREFIX_NAME_OUT(area)); | |
b5a8894d CS |
1179 | abr_inv++; |
1180 | } | |
1181 | } | |
d62a17ae | 1182 | |
b5a8894d CS |
1183 | /* Schedule ABR task. */ |
1184 | if (IS_OSPF_ABR(ospf) && abr_inv) | |
1185 | ospf_schedule_abr_task(ospf); | |
1186 | } | |
dd669bb0 | 1187 | } |
cf795c5d | 1188 | |
d62a17ae | 1189 | static struct ospf_distance *ospf_distance_new(void) |
718e3744 | 1190 | { |
d62a17ae | 1191 | return XCALLOC(MTYPE_OSPF_DISTANCE, sizeof(struct ospf_distance)); |
718e3744 | 1192 | } |
1193 | ||
d62a17ae | 1194 | static void ospf_distance_free(struct ospf_distance *odistance) |
718e3744 | 1195 | { |
d62a17ae | 1196 | XFREE(MTYPE_OSPF_DISTANCE, odistance); |
718e3744 | 1197 | } |
1198 | ||
d62a17ae | 1199 | int ospf_distance_set(struct vty *vty, struct ospf *ospf, |
1200 | const char *distance_str, const char *ip_str, | |
1201 | const char *access_list_str) | |
718e3744 | 1202 | { |
d62a17ae | 1203 | int ret; |
1204 | struct prefix_ipv4 p; | |
d7c0a89a | 1205 | uint8_t distance; |
d62a17ae | 1206 | struct route_node *rn; |
1207 | struct ospf_distance *odistance; | |
1208 | ||
1209 | ret = str2prefix_ipv4(ip_str, &p); | |
1210 | if (ret == 0) { | |
1211 | vty_out(vty, "Malformed prefix\n"); | |
1212 | return CMD_WARNING_CONFIG_FAILED; | |
1213 | } | |
1214 | ||
1215 | distance = atoi(distance_str); | |
1216 | ||
1217 | /* Get OSPF distance node. */ | |
1218 | rn = route_node_get(ospf->distance_table, (struct prefix *)&p); | |
1219 | if (rn->info) { | |
1220 | odistance = rn->info; | |
1221 | route_unlock_node(rn); | |
1222 | } else { | |
1223 | odistance = ospf_distance_new(); | |
1224 | rn->info = odistance; | |
1225 | } | |
1226 | ||
1227 | /* Set distance value. */ | |
1228 | odistance->distance = distance; | |
1229 | ||
1230 | /* Reset access-list configuration. */ | |
1231 | if (odistance->access_list) { | |
1232 | free(odistance->access_list); | |
1233 | odistance->access_list = NULL; | |
1234 | } | |
1235 | if (access_list_str) | |
1236 | odistance->access_list = strdup(access_list_str); | |
1237 | ||
1238 | return CMD_SUCCESS; | |
718e3744 | 1239 | } |
1240 | ||
d62a17ae | 1241 | int ospf_distance_unset(struct vty *vty, struct ospf *ospf, |
1242 | const char *distance_str, const char *ip_str, | |
1243 | char const *access_list_str) | |
718e3744 | 1244 | { |
d62a17ae | 1245 | int ret; |
1246 | struct prefix_ipv4 p; | |
1247 | struct route_node *rn; | |
1248 | struct ospf_distance *odistance; | |
1249 | ||
1250 | ret = str2prefix_ipv4(ip_str, &p); | |
1251 | if (ret == 0) { | |
1252 | vty_out(vty, "Malformed prefix\n"); | |
1253 | return CMD_WARNING_CONFIG_FAILED; | |
1254 | } | |
1255 | ||
1256 | rn = route_node_lookup(ospf->distance_table, (struct prefix *)&p); | |
1257 | if (!rn) { | |
1258 | vty_out(vty, "Can't find specified prefix\n"); | |
1259 | return CMD_WARNING_CONFIG_FAILED; | |
1260 | } | |
1261 | ||
1262 | odistance = rn->info; | |
1263 | ||
1264 | if (odistance->access_list) | |
1265 | free(odistance->access_list); | |
1266 | ospf_distance_free(odistance); | |
1267 | ||
1268 | rn->info = NULL; | |
1269 | route_unlock_node(rn); | |
1270 | route_unlock_node(rn); | |
1271 | ||
1272 | return CMD_SUCCESS; | |
718e3744 | 1273 | } |
1274 | ||
d62a17ae | 1275 | void ospf_distance_reset(struct ospf *ospf) |
718e3744 | 1276 | { |
d62a17ae | 1277 | struct route_node *rn; |
1278 | struct ospf_distance *odistance; | |
1279 | ||
1280 | for (rn = route_top(ospf->distance_table); rn; rn = route_next(rn)) | |
1281 | if ((odistance = rn->info) != NULL) { | |
1282 | if (odistance->access_list) | |
1283 | free(odistance->access_list); | |
1284 | ospf_distance_free(odistance); | |
1285 | rn->info = NULL; | |
1286 | route_unlock_node(rn); | |
1287 | } | |
718e3744 | 1288 | } |
1289 | ||
d7c0a89a QY |
1290 | uint8_t ospf_distance_apply(struct ospf *ospf, struct prefix_ipv4 *p, |
1291 | struct ospf_route * or) | |
718e3744 | 1292 | { |
718e3744 | 1293 | |
d62a17ae | 1294 | if (ospf == NULL) |
1295 | return 0; | |
718e3744 | 1296 | |
d62a17ae | 1297 | if (ospf->distance_intra) |
1298 | if (or->path_type == OSPF_PATH_INTRA_AREA) | |
1299 | return ospf->distance_intra; | |
718e3744 | 1300 | |
d62a17ae | 1301 | if (ospf->distance_inter) |
1302 | if (or->path_type == OSPF_PATH_INTER_AREA) | |
1303 | return ospf->distance_inter; | |
718e3744 | 1304 | |
d62a17ae | 1305 | if (ospf->distance_external) |
1306 | if (or->path_type == OSPF_PATH_TYPE1_EXTERNAL || | |
1307 | or->path_type == OSPF_PATH_TYPE2_EXTERNAL) | |
1308 | return ospf->distance_external; | |
cf795c5d | 1309 | |
d62a17ae | 1310 | if (ospf->distance_all) |
1311 | return ospf->distance_all; | |
718e3744 | 1312 | |
d62a17ae | 1313 | return 0; |
718e3744 | 1314 | } |
1315 | ||
b5a8894d CS |
1316 | void ospf_zebra_vrf_register(struct ospf *ospf) |
1317 | { | |
1318 | if (!zclient || zclient->sock < 0 || !ospf) | |
1319 | return; | |
1320 | ||
6021c6c0 | 1321 | if (ospf->vrf_id != VRF_UNKNOWN) { |
b5a8894d | 1322 | if (IS_DEBUG_OSPF_EVENT) |
15569c58 | 1323 | zlog_debug("%s: Register VRF %s id %u", __func__, |
b5a8894d CS |
1324 | ospf_vrf_id_to_name(ospf->vrf_id), |
1325 | ospf->vrf_id); | |
1326 | zclient_send_reg_requests(zclient, ospf->vrf_id); | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | void ospf_zebra_vrf_deregister(struct ospf *ospf) | |
1331 | { | |
1332 | if (!zclient || zclient->sock < 0 || !ospf) | |
1333 | return; | |
1334 | ||
1335 | if (ospf->vrf_id != VRF_DEFAULT && ospf->vrf_id != VRF_UNKNOWN) { | |
1336 | if (IS_DEBUG_OSPF_EVENT) | |
6021c6c0 | 1337 | zlog_debug("%s: De-Register VRF %s id %u to Zebra.", |
15569c58 | 1338 | __func__, ospf_vrf_id_to_name(ospf->vrf_id), |
b5a8894d CS |
1339 | ospf->vrf_id); |
1340 | /* Deregister for router-id, interfaces, | |
1341 | * redistributed routes. */ | |
1342 | zclient_send_dereg_requests(zclient, ospf->vrf_id); | |
1343 | } | |
1344 | } | |
d62a17ae | 1345 | static void ospf_zebra_connected(struct zclient *zclient) |
7076bb2f | 1346 | { |
d62a17ae | 1347 | /* Send the client registration */ |
0945d5ed | 1348 | bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT); |
2376c3f2 | 1349 | |
d62a17ae | 1350 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
7076bb2f FL |
1351 | } |
1352 | ||
d7c0a89a | 1353 | void ospf_zebra_init(struct thread_master *master, unsigned short instance) |
718e3744 | 1354 | { |
d62a17ae | 1355 | /* Allocate zebra structure. */ |
26f63a1e | 1356 | zclient = zclient_new(master, &zclient_options_default); |
342213ea | 1357 | zclient_init(zclient, ZEBRA_ROUTE_OSPF, instance, &ospfd_privs); |
d62a17ae | 1358 | zclient->zebra_connected = ospf_zebra_connected; |
1359 | zclient->router_id_update = ospf_router_id_update_zebra; | |
d62a17ae | 1360 | zclient->interface_address_add = ospf_interface_address_add; |
1361 | zclient->interface_address_delete = ospf_interface_address_delete; | |
1362 | zclient->interface_link_params = ospf_interface_link_params; | |
b5a8894d | 1363 | zclient->interface_vrf_update = ospf_interface_vrf_update; |
d62a17ae | 1364 | |
74489921 RW |
1365 | zclient->redistribute_route_add = ospf_zebra_read_route; |
1366 | zclient->redistribute_route_del = ospf_zebra_read_route; | |
d62a17ae | 1367 | |
1368 | access_list_add_hook(ospf_filter_update); | |
1369 | access_list_delete_hook(ospf_filter_update); | |
1370 | prefix_list_add_hook(ospf_prefix_list_update); | |
1371 | prefix_list_delete_hook(ospf_prefix_list_update); | |
718e3744 | 1372 | } |