]>
Commit | Line | Data |
---|---|---|
1 | /* Redistribution Handler | |
2 | * Copyright (C) 1998 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "vector.h" | |
24 | #include "vty.h" | |
25 | #include "command.h" | |
26 | #include "prefix.h" | |
27 | #include "table.h" | |
28 | #include "stream.h" | |
29 | #include "zclient.h" | |
30 | #include "linklist.h" | |
31 | #include "log.h" | |
32 | #include "vrf.h" | |
33 | #include "srcdest_table.h" | |
34 | ||
35 | #include "zebra/rib.h" | |
36 | #include "zebra/zebra_router.h" | |
37 | #include "zebra/zebra_ns.h" | |
38 | #include "zebra/zebra_vrf.h" | |
39 | #include "zebra/zebra_routemap.h" | |
40 | #include "zebra/redistribute.h" | |
41 | #include "zebra/debug.h" | |
42 | #include "zebra/router-id.h" | |
43 | #include "zebra/zapi_msg.h" | |
44 | #include "zebra/zebra_memory.h" | |
45 | #include "zebra/zebra_vxlan.h" | |
46 | #include "zebra/zebra_errors.h" | |
47 | ||
48 | #define ZEBRA_PTM_SUPPORT | |
49 | ||
50 | /* array holding redistribute info about table redistribution */ | |
51 | /* bit AFI is set if that AFI is redistributing routes from this table */ | |
52 | static int zebra_import_table_used[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX]; | |
53 | static uint32_t zebra_import_table_distance[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX]; | |
54 | ||
55 | int is_zebra_import_table_enabled(afi_t afi, uint32_t table_id) | |
56 | { | |
57 | /* | |
58 | * Make sure that what we are called with actualy makes sense | |
59 | */ | |
60 | if (afi == AFI_MAX) | |
61 | return 0; | |
62 | ||
63 | if (is_zebra_valid_kernel_table(table_id) && | |
64 | table_id < ZEBRA_KERNEL_TABLE_MAX) | |
65 | return zebra_import_table_used[afi][table_id]; | |
66 | return 0; | |
67 | } | |
68 | ||
69 | static void zebra_redistribute_default(struct zserv *client, vrf_id_t vrf_id) | |
70 | { | |
71 | int afi; | |
72 | struct prefix p; | |
73 | struct route_table *table; | |
74 | struct route_node *rn; | |
75 | struct route_entry *newre; | |
76 | ||
77 | for (afi = AFI_IP; afi <= AFI_IP6; afi++) { | |
78 | /* Lookup table. */ | |
79 | table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id); | |
80 | if (!table) | |
81 | continue; | |
82 | ||
83 | /* Lookup default route. */ | |
84 | memset(&p, 0, sizeof(p)); | |
85 | p.family = afi2family(afi); | |
86 | rn = route_node_lookup(table, &p); | |
87 | if (!rn) | |
88 | continue; | |
89 | ||
90 | RNODE_FOREACH_RE (rn, newre) { | |
91 | if (CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED) | |
92 | && newre->distance != DISTANCE_INFINITY) | |
93 | zsend_redistribute_route( | |
94 | ZEBRA_REDISTRIBUTE_ROUTE_ADD, client, | |
95 | &rn->p, NULL, newre); | |
96 | } | |
97 | ||
98 | route_unlock_node(rn); | |
99 | } | |
100 | } | |
101 | ||
102 | /* Redistribute routes. */ | |
103 | static void zebra_redistribute(struct zserv *client, int type, | |
104 | unsigned short instance, vrf_id_t vrf_id, | |
105 | int afi) | |
106 | { | |
107 | struct route_entry *newre; | |
108 | struct route_table *table; | |
109 | struct route_node *rn; | |
110 | ||
111 | table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id); | |
112 | if (!table) | |
113 | return; | |
114 | ||
115 | for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) | |
116 | RNODE_FOREACH_RE (rn, newre) { | |
117 | const struct prefix *dst_p, *src_p; | |
118 | char buf[PREFIX_STRLEN]; | |
119 | ||
120 | srcdest_rnode_prefixes(rn, &dst_p, &src_p); | |
121 | ||
122 | if (IS_ZEBRA_DEBUG_EVENT) | |
123 | zlog_debug( | |
124 | "%s: client %s %s(%u) checking: selected=%d, type=%d, distance=%d, metric=%d zebra_check_addr=%d", | |
125 | __func__, | |
126 | zebra_route_string(client->proto), | |
127 | prefix2str(dst_p, buf, sizeof(buf)), | |
128 | vrf_id, CHECK_FLAG(newre->flags, | |
129 | ZEBRA_FLAG_SELECTED), | |
130 | newre->type, newre->distance, | |
131 | newre->metric, zebra_check_addr(dst_p)); | |
132 | ||
133 | if (!CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED)) | |
134 | continue; | |
135 | if ((type != ZEBRA_ROUTE_ALL | |
136 | && (newre->type != type | |
137 | || newre->instance != instance))) | |
138 | continue; | |
139 | if (newre->distance == DISTANCE_INFINITY) | |
140 | continue; | |
141 | if (!zebra_check_addr(dst_p)) | |
142 | continue; | |
143 | ||
144 | zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD, | |
145 | client, dst_p, src_p, newre); | |
146 | } | |
147 | } | |
148 | ||
149 | /* Either advertise a route for redistribution to registered clients or */ | |
150 | /* withdraw redistribution if add cannot be done for client */ | |
151 | void redistribute_update(const struct prefix *p, const struct prefix *src_p, | |
152 | struct route_entry *re, struct route_entry *prev_re) | |
153 | { | |
154 | struct listnode *node, *nnode; | |
155 | struct zserv *client; | |
156 | int send_redistribute; | |
157 | int afi; | |
158 | char buf[PREFIX_STRLEN]; | |
159 | ||
160 | if (IS_ZEBRA_DEBUG_RIB) { | |
161 | zlog_debug( | |
162 | "%u:%s: Redist update re %p (%s), old %p (%s)", | |
163 | re->vrf_id, prefix2str(p, buf, sizeof(buf)), | |
164 | re, zebra_route_string(re->type), prev_re, | |
165 | prev_re ? zebra_route_string(prev_re->type) : "None"); | |
166 | } | |
167 | ||
168 | afi = family2afi(p->family); | |
169 | if (!afi) { | |
170 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
171 | "%s: Unknown AFI/SAFI prefix received\n", | |
172 | __FUNCTION__); | |
173 | return; | |
174 | } | |
175 | ||
176 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
177 | send_redistribute = 0; | |
178 | ||
179 | if (is_default_prefix(p) | |
180 | && vrf_bitmap_check(client->redist_default[afi], | |
181 | re->vrf_id)) | |
182 | send_redistribute = 1; | |
183 | else if (vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL], | |
184 | re->vrf_id)) | |
185 | send_redistribute = 1; | |
186 | else if (re->instance | |
187 | && redist_check_instance( | |
188 | &client->mi_redist[afi][re->type], | |
189 | re->instance)) | |
190 | send_redistribute = 1; | |
191 | else if (vrf_bitmap_check(client->redist[afi][re->type], | |
192 | re->vrf_id)) | |
193 | send_redistribute = 1; | |
194 | ||
195 | if (send_redistribute) { | |
196 | if (IS_ZEBRA_DEBUG_EVENT) { | |
197 | zlog_debug( | |
198 | "%s: client %s %s(%u), type=%d, distance=%d, metric=%d", | |
199 | __func__, | |
200 | zebra_route_string(client->proto), | |
201 | prefix2str(p, buf, sizeof(buf)), | |
202 | re->vrf_id, re->type, | |
203 | re->distance, re->metric); | |
204 | } | |
205 | zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD, | |
206 | client, p, src_p, re); | |
207 | } else if (prev_re | |
208 | && ((re->instance | |
209 | && redist_check_instance( | |
210 | &client->mi_redist[afi] | |
211 | [prev_re->type], | |
212 | re->instance)) | |
213 | || vrf_bitmap_check( | |
214 | client->redist[afi][prev_re->type], | |
215 | re->vrf_id))) { | |
216 | zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL, | |
217 | client, p, src_p, prev_re); | |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | void redistribute_delete(const struct prefix *p, const struct prefix *src_p, | |
223 | struct route_entry *re) | |
224 | { | |
225 | struct listnode *node, *nnode; | |
226 | struct zserv *client; | |
227 | char buf[INET6_ADDRSTRLEN]; | |
228 | int afi; | |
229 | ||
230 | if (IS_ZEBRA_DEBUG_RIB) { | |
231 | inet_ntop(p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN); | |
232 | zlog_debug("%u:%s/%d: Redist delete re %p (%s)", | |
233 | re->vrf_id, buf, p->prefixlen, re, | |
234 | zebra_route_string(re->type)); | |
235 | } | |
236 | ||
237 | /* Add DISTANCE_INFINITY check. */ | |
238 | if (re->distance == DISTANCE_INFINITY) | |
239 | return; | |
240 | ||
241 | afi = family2afi(p->family); | |
242 | if (!afi) { | |
243 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
244 | "%s: Unknown AFI/SAFI prefix received\n", | |
245 | __FUNCTION__); | |
246 | return; | |
247 | } | |
248 | ||
249 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
250 | if ((is_default_prefix(p) | |
251 | && vrf_bitmap_check(client->redist_default[afi], | |
252 | re->vrf_id)) | |
253 | || vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL], | |
254 | re->vrf_id) | |
255 | || (re->instance | |
256 | && redist_check_instance( | |
257 | &client->mi_redist[afi][re->type], | |
258 | re->instance)) | |
259 | || vrf_bitmap_check(client->redist[afi][re->type], | |
260 | re->vrf_id)) { | |
261 | zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL, | |
262 | client, p, src_p, re); | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | void zebra_redistribute_add(ZAPI_HANDLER_ARGS) | |
268 | { | |
269 | afi_t afi = 0; | |
270 | int type = 0; | |
271 | unsigned short instance; | |
272 | ||
273 | STREAM_GETC(msg, afi); | |
274 | STREAM_GETC(msg, type); | |
275 | STREAM_GETW(msg, instance); | |
276 | ||
277 | if (IS_ZEBRA_DEBUG_EVENT) | |
278 | zlog_debug( | |
279 | "%s: client proto %s afi=%d, wants %s, vrf %u, instance=%d", | |
280 | __func__, zebra_route_string(client->proto), afi, | |
281 | zebra_route_string(type), zvrf_id(zvrf), instance); | |
282 | ||
283 | if (afi == 0 || afi >= AFI_MAX) { | |
284 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
285 | "%s: Specified afi %d does not exist", | |
286 | __PRETTY_FUNCTION__, afi); | |
287 | return; | |
288 | } | |
289 | ||
290 | if (type == 0 || type >= ZEBRA_ROUTE_MAX) { | |
291 | zlog_debug("%s: Specified Route Type %d does not exist", | |
292 | __PRETTY_FUNCTION__, type); | |
293 | return; | |
294 | } | |
295 | ||
296 | if (instance) { | |
297 | if (!redist_check_instance(&client->mi_redist[afi][type], | |
298 | instance)) { | |
299 | redist_add_instance(&client->mi_redist[afi][type], | |
300 | instance); | |
301 | zebra_redistribute(client, type, instance, | |
302 | zvrf_id(zvrf), afi); | |
303 | } | |
304 | } else { | |
305 | if (!vrf_bitmap_check(client->redist[afi][type], | |
306 | zvrf_id(zvrf))) { | |
307 | if (IS_ZEBRA_DEBUG_EVENT) | |
308 | zlog_debug("%s: setting vrf %u redist bitmap", | |
309 | __func__, zvrf_id(zvrf)); | |
310 | vrf_bitmap_set(client->redist[afi][type], | |
311 | zvrf_id(zvrf)); | |
312 | zebra_redistribute(client, type, 0, zvrf_id(zvrf), afi); | |
313 | } | |
314 | } | |
315 | ||
316 | stream_failure: | |
317 | return; | |
318 | } | |
319 | ||
320 | void zebra_redistribute_delete(ZAPI_HANDLER_ARGS) | |
321 | { | |
322 | afi_t afi = 0; | |
323 | int type = 0; | |
324 | unsigned short instance; | |
325 | ||
326 | STREAM_GETC(msg, afi); | |
327 | STREAM_GETC(msg, type); | |
328 | STREAM_GETW(msg, instance); | |
329 | ||
330 | if (afi == 0 || afi >= AFI_MAX) { | |
331 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
332 | "%s: Specified afi %d does not exist", | |
333 | __PRETTY_FUNCTION__, afi); | |
334 | return; | |
335 | } | |
336 | ||
337 | if (type == 0 || type >= ZEBRA_ROUTE_MAX) { | |
338 | zlog_debug("%s: Specified Route Type %d does not exist", | |
339 | __PRETTY_FUNCTION__, type); | |
340 | return; | |
341 | } | |
342 | ||
343 | /* | |
344 | * NOTE: no need to withdraw the previously advertised routes. The | |
345 | * clients | |
346 | * themselves should keep track of the received routes from zebra and | |
347 | * withdraw them when necessary. | |
348 | */ | |
349 | if (instance) | |
350 | redist_del_instance(&client->mi_redist[afi][type], instance); | |
351 | else | |
352 | vrf_bitmap_unset(client->redist[afi][type], zvrf_id(zvrf)); | |
353 | ||
354 | stream_failure: | |
355 | return; | |
356 | } | |
357 | ||
358 | void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS) | |
359 | { | |
360 | afi_t afi = 0; | |
361 | ||
362 | STREAM_GETC(msg, afi); | |
363 | ||
364 | if (afi == 0 || afi >= AFI_MAX) { | |
365 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
366 | "%s: Specified afi %u does not exist", | |
367 | __PRETTY_FUNCTION__, afi); | |
368 | return; | |
369 | } | |
370 | ||
371 | vrf_bitmap_set(client->redist_default[afi], zvrf_id(zvrf)); | |
372 | zebra_redistribute_default(client, zvrf_id(zvrf)); | |
373 | ||
374 | stream_failure: | |
375 | return; | |
376 | } | |
377 | ||
378 | void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS) | |
379 | { | |
380 | afi_t afi = 0; | |
381 | ||
382 | STREAM_GETC(msg, afi); | |
383 | ||
384 | if (afi == 0 || afi >= AFI_MAX) { | |
385 | flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF, | |
386 | "%s: Specified afi %u does not exist", | |
387 | __PRETTY_FUNCTION__, afi); | |
388 | return; | |
389 | } | |
390 | ||
391 | vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf)); | |
392 | ||
393 | stream_failure: | |
394 | return; | |
395 | } | |
396 | ||
397 | /* Interface up information. */ | |
398 | void zebra_interface_up_update(struct interface *ifp) | |
399 | { | |
400 | struct listnode *node, *nnode; | |
401 | struct zserv *client; | |
402 | ||
403 | if (IS_ZEBRA_DEBUG_EVENT) | |
404 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s(%u)", | |
405 | ifp->name, ifp->vrf_id); | |
406 | ||
407 | if (ifp->ptm_status || !ifp->ptm_enable) { | |
408 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, | |
409 | client)) { | |
410 | zsend_interface_update(ZEBRA_INTERFACE_UP, | |
411 | client, ifp); | |
412 | zsend_interface_link_params(client, ifp); | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
417 | /* Interface down information. */ | |
418 | void zebra_interface_down_update(struct interface *ifp) | |
419 | { | |
420 | struct listnode *node, *nnode; | |
421 | struct zserv *client; | |
422 | ||
423 | if (IS_ZEBRA_DEBUG_EVENT) | |
424 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s(%u)", | |
425 | ifp->name, ifp->vrf_id); | |
426 | ||
427 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
428 | zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp); | |
429 | } | |
430 | } | |
431 | ||
432 | /* Interface information update. */ | |
433 | void zebra_interface_add_update(struct interface *ifp) | |
434 | { | |
435 | struct listnode *node, *nnode; | |
436 | struct zserv *client; | |
437 | ||
438 | if (IS_ZEBRA_DEBUG_EVENT) | |
439 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s(%u)", ifp->name, | |
440 | ifp->vrf_id); | |
441 | ||
442 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
443 | client->ifadd_cnt++; | |
444 | zsend_interface_add(client, ifp); | |
445 | zsend_interface_link_params(client, ifp); | |
446 | } | |
447 | } | |
448 | ||
449 | void zebra_interface_delete_update(struct interface *ifp) | |
450 | { | |
451 | struct listnode *node, *nnode; | |
452 | struct zserv *client; | |
453 | ||
454 | if (IS_ZEBRA_DEBUG_EVENT) | |
455 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s(%u)", | |
456 | ifp->name, ifp->vrf_id); | |
457 | ||
458 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
459 | client->ifdel_cnt++; | |
460 | zsend_interface_delete(client, ifp); | |
461 | } | |
462 | } | |
463 | ||
464 | /* Interface address addition. */ | |
465 | void zebra_interface_address_add_update(struct interface *ifp, | |
466 | struct connected *ifc) | |
467 | { | |
468 | struct listnode *node, *nnode; | |
469 | struct zserv *client; | |
470 | struct prefix *p; | |
471 | ||
472 | if (IS_ZEBRA_DEBUG_EVENT) { | |
473 | char buf[PREFIX_STRLEN]; | |
474 | ||
475 | p = ifc->address; | |
476 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s(%u)", | |
477 | prefix2str(p, buf, sizeof(buf)), ifp->name, | |
478 | ifp->vrf_id); | |
479 | } | |
480 | ||
481 | if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) | |
482 | flog_warn( | |
483 | EC_ZEBRA_ADVERTISING_UNUSABLE_ADDR, | |
484 | "WARNING: advertising address to clients that is not yet usable."); | |
485 | ||
486 | zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 1); | |
487 | ||
488 | router_id_add_address(ifc); | |
489 | ||
490 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) | |
491 | if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) { | |
492 | client->connected_rt_add_cnt++; | |
493 | zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, | |
494 | client, ifp, ifc); | |
495 | } | |
496 | } | |
497 | ||
498 | /* Interface address deletion. */ | |
499 | void zebra_interface_address_delete_update(struct interface *ifp, | |
500 | struct connected *ifc) | |
501 | { | |
502 | struct listnode *node, *nnode; | |
503 | struct zserv *client; | |
504 | struct prefix *p; | |
505 | ||
506 | if (IS_ZEBRA_DEBUG_EVENT) { | |
507 | char buf[PREFIX_STRLEN]; | |
508 | ||
509 | p = ifc->address; | |
510 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s(%u)", | |
511 | prefix2str(p, buf, sizeof(buf)), | |
512 | ifp->name, ifp->vrf_id); | |
513 | } | |
514 | ||
515 | zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0); | |
516 | ||
517 | router_id_del_address(ifc); | |
518 | ||
519 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) | |
520 | if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) { | |
521 | client->connected_rt_del_cnt++; | |
522 | zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE, | |
523 | client, ifp, ifc); | |
524 | } | |
525 | } | |
526 | ||
527 | /* Interface VRF change. May need to delete from clients not interested in | |
528 | * the new VRF. Note that this function is invoked *prior* to the VRF change. | |
529 | */ | |
530 | void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id) | |
531 | { | |
532 | struct listnode *node, *nnode; | |
533 | struct zserv *client; | |
534 | ||
535 | if (IS_ZEBRA_DEBUG_EVENT) | |
536 | zlog_debug( | |
537 | "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u", | |
538 | ifp->name, ifp->vrf_id, new_vrf_id); | |
539 | ||
540 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
541 | /* Need to delete if the client is not interested in the new | |
542 | * VRF. */ | |
543 | zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp); | |
544 | client->ifdel_cnt++; | |
545 | zsend_interface_delete(client, ifp); | |
546 | zsend_interface_vrf_update(client, ifp, new_vrf_id); | |
547 | } | |
548 | } | |
549 | ||
550 | /* Interface VRF change. This function is invoked *post* VRF change and sends an | |
551 | * add to clients who are interested in the new VRF but not in the old VRF. | |
552 | */ | |
553 | void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id) | |
554 | { | |
555 | struct listnode *node, *nnode; | |
556 | struct zserv *client; | |
557 | ||
558 | if (IS_ZEBRA_DEBUG_EVENT) | |
559 | zlog_debug( | |
560 | "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u", | |
561 | ifp->name, old_vrf_id, ifp->vrf_id); | |
562 | ||
563 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) { | |
564 | /* Need to add if the client is interested in the new VRF. */ | |
565 | client->ifadd_cnt++; | |
566 | zsend_interface_add(client, ifp); | |
567 | zsend_interface_addresses(client, ifp); | |
568 | } | |
569 | } | |
570 | ||
571 | int zebra_add_import_table_entry(struct route_node *rn, struct route_entry *re, | |
572 | const char *rmap_name) | |
573 | { | |
574 | struct route_entry *newre; | |
575 | struct route_entry *same; | |
576 | struct prefix p; | |
577 | route_map_result_t ret = RMAP_MATCH; | |
578 | afi_t afi; | |
579 | ||
580 | afi = family2afi(rn->p.family); | |
581 | if (rmap_name) | |
582 | ret = zebra_import_table_route_map_check( | |
583 | afi, re->type, re->instance, &rn->p, re->ng.nexthop, | |
584 | re->vrf_id, re->tag, rmap_name); | |
585 | ||
586 | if (ret != RMAP_MATCH) { | |
587 | UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED); | |
588 | zebra_del_import_table_entry(rn, re); | |
589 | return 0; | |
590 | } | |
591 | ||
592 | prefix_copy(&p, &rn->p); | |
593 | ||
594 | RNODE_FOREACH_RE (rn, same) { | |
595 | if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) | |
596 | continue; | |
597 | ||
598 | if (same->type == re->type && same->instance == re->instance | |
599 | && same->table == re->table | |
600 | && same->type != ZEBRA_ROUTE_CONNECT) | |
601 | break; | |
602 | } | |
603 | ||
604 | if (same) { | |
605 | UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED); | |
606 | zebra_del_import_table_entry(rn, same); | |
607 | } | |
608 | ||
609 | newre = XCALLOC(MTYPE_RE, sizeof(struct route_entry)); | |
610 | newre->type = ZEBRA_ROUTE_TABLE; | |
611 | newre->distance = zebra_import_table_distance[afi][re->table]; | |
612 | newre->flags = re->flags; | |
613 | newre->metric = re->metric; | |
614 | newre->mtu = re->mtu; | |
615 | newre->table = 0; | |
616 | newre->nexthop_num = 0; | |
617 | newre->uptime = monotime(NULL); | |
618 | newre->instance = re->table; | |
619 | route_entry_copy_nexthops(newre, re->ng.nexthop); | |
620 | ||
621 | rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre); | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
626 | int zebra_del_import_table_entry(struct route_node *rn, struct route_entry *re) | |
627 | { | |
628 | struct prefix p; | |
629 | afi_t afi; | |
630 | ||
631 | afi = family2afi(rn->p.family); | |
632 | prefix_copy(&p, &rn->p); | |
633 | ||
634 | rib_delete(afi, SAFI_UNICAST, re->vrf_id, ZEBRA_ROUTE_TABLE, re->table, | |
635 | re->flags, &p, NULL, re->ng.nexthop, 0, re->metric, | |
636 | re->distance, false); | |
637 | ||
638 | return 0; | |
639 | } | |
640 | ||
641 | /* Assuming no one calls this with the main routing table */ | |
642 | int zebra_import_table(afi_t afi, uint32_t table_id, uint32_t distance, | |
643 | const char *rmap_name, int add) | |
644 | { | |
645 | struct route_table *table; | |
646 | struct route_entry *re; | |
647 | struct route_node *rn; | |
648 | ||
649 | if (!is_zebra_valid_kernel_table(table_id) | |
650 | || (table_id == RT_TABLE_MAIN)) | |
651 | return (-1); | |
652 | ||
653 | if (afi >= AFI_MAX) | |
654 | return (-1); | |
655 | ||
656 | table = zebra_vrf_table_with_table_id(afi, SAFI_UNICAST, | |
657 | VRF_DEFAULT, table_id); | |
658 | if (table == NULL) { | |
659 | return 0; | |
660 | } else if (IS_ZEBRA_DEBUG_RIB) { | |
661 | zlog_debug("%s routes from table %d", | |
662 | add ? "Importing" : "Unimporting", table_id); | |
663 | } | |
664 | ||
665 | if (add) { | |
666 | if (rmap_name) | |
667 | zebra_add_import_table_route_map(afi, rmap_name, | |
668 | table_id); | |
669 | else { | |
670 | rmap_name = | |
671 | zebra_get_import_table_route_map(afi, table_id); | |
672 | if (rmap_name) { | |
673 | zebra_del_import_table_route_map(afi, table_id); | |
674 | rmap_name = NULL; | |
675 | } | |
676 | } | |
677 | ||
678 | zebra_import_table_used[afi][table_id] = 1; | |
679 | zebra_import_table_distance[afi][table_id] = distance; | |
680 | } else { | |
681 | zebra_import_table_used[afi][table_id] = 0; | |
682 | zebra_import_table_distance[afi][table_id] = | |
683 | ZEBRA_TABLE_DISTANCE_DEFAULT; | |
684 | ||
685 | rmap_name = zebra_get_import_table_route_map(afi, table_id); | |
686 | if (rmap_name) { | |
687 | zebra_del_import_table_route_map(afi, table_id); | |
688 | rmap_name = NULL; | |
689 | } | |
690 | } | |
691 | ||
692 | for (rn = route_top(table); rn; rn = route_next(rn)) { | |
693 | /* For each entry in the non-default routing table, | |
694 | * add the entry in the main table | |
695 | */ | |
696 | if (!rn->info) | |
697 | continue; | |
698 | ||
699 | RNODE_FOREACH_RE (rn, re) { | |
700 | if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) | |
701 | continue; | |
702 | break; | |
703 | } | |
704 | ||
705 | if (!re) | |
706 | continue; | |
707 | ||
708 | if (((afi == AFI_IP) && (rn->p.family == AF_INET)) | |
709 | || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) { | |
710 | if (add) | |
711 | zebra_add_import_table_entry(rn, re, rmap_name); | |
712 | else | |
713 | zebra_del_import_table_entry(rn, re); | |
714 | } | |
715 | } | |
716 | return 0; | |
717 | } | |
718 | ||
719 | int zebra_import_table_config(struct vty *vty) | |
720 | { | |
721 | int i; | |
722 | afi_t afi; | |
723 | int write = 0; | |
724 | char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"}; | |
725 | const char *rmap_name; | |
726 | ||
727 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
728 | for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) { | |
729 | if (!is_zebra_import_table_enabled(afi, i)) | |
730 | continue; | |
731 | ||
732 | if (zebra_import_table_distance[afi][i] | |
733 | != ZEBRA_TABLE_DISTANCE_DEFAULT) { | |
734 | vty_out(vty, "%s import-table %d distance %d", | |
735 | afi_str[afi], i, | |
736 | zebra_import_table_distance[afi][i]); | |
737 | } else { | |
738 | vty_out(vty, "%s import-table %d", afi_str[afi], | |
739 | i); | |
740 | } | |
741 | ||
742 | rmap_name = zebra_get_import_table_route_map(afi, i); | |
743 | if (rmap_name) | |
744 | vty_out(vty, " route-map %s", rmap_name); | |
745 | ||
746 | vty_out(vty, "\n"); | |
747 | write = 1; | |
748 | } | |
749 | } | |
750 | ||
751 | return write; | |
752 | } | |
753 | ||
754 | void zebra_import_table_rm_update(const char *rmap) | |
755 | { | |
756 | afi_t afi; | |
757 | int i; | |
758 | struct route_table *table; | |
759 | struct route_entry *re; | |
760 | struct route_node *rn; | |
761 | const char *rmap_name; | |
762 | ||
763 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
764 | for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) { | |
765 | if (!is_zebra_import_table_enabled(afi, i)) | |
766 | continue; | |
767 | ||
768 | rmap_name = zebra_get_import_table_route_map(afi, i); | |
769 | if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0)) | |
770 | continue; | |
771 | table = zebra_vrf_table_with_table_id(afi, SAFI_UNICAST, | |
772 | VRF_DEFAULT, i); | |
773 | if (!table) { | |
774 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
775 | zlog_debug("%s: Table id=%d not found", | |
776 | __func__, i); | |
777 | continue; | |
778 | } | |
779 | ||
780 | for (rn = route_top(table); rn; rn = route_next(rn)) { | |
781 | /* For each entry in the non-default | |
782 | * routing table, | |
783 | * add the entry in the main table | |
784 | */ | |
785 | if (!rn->info) | |
786 | continue; | |
787 | ||
788 | RNODE_FOREACH_RE (rn, re) { | |
789 | if (CHECK_FLAG(re->status, | |
790 | ROUTE_ENTRY_REMOVED)) | |
791 | continue; | |
792 | break; | |
793 | } | |
794 | ||
795 | if (!re) | |
796 | continue; | |
797 | ||
798 | if (((afi == AFI_IP) | |
799 | && (rn->p.family == AF_INET)) | |
800 | || ((afi == AFI_IP6) | |
801 | && (rn->p.family == AF_INET6))) | |
802 | zebra_add_import_table_entry(rn, re, | |
803 | rmap_name); | |
804 | } | |
805 | } | |
806 | } | |
807 | ||
808 | return; | |
809 | } | |
810 | ||
811 | /* Interface parameters update */ | |
812 | void zebra_interface_parameters_update(struct interface *ifp) | |
813 | { | |
814 | struct listnode *node, *nnode; | |
815 | struct zserv *client; | |
816 | ||
817 | if (IS_ZEBRA_DEBUG_EVENT) | |
818 | zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s(%u)", | |
819 | ifp->name, ifp->vrf_id); | |
820 | ||
821 | for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) | |
822 | zsend_interface_link_params(client, ifp); | |
823 | } |