]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_zebra.c
Merge branch 'cmaster' of ssh://stash.cumulusnetworks.com:7999/quag/quagga into cmaster
[mirror_frr.git] / bgpd / bgp_zebra.c
1 /* zebra client
2 Copyright (C) 1997, 98, 99 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
17 along with GNU Zebra; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "stream.h"
25 #include "network.h"
26 #include "prefix.h"
27 #include "log.h"
28 #include "sockunion.h"
29 #include "zclient.h"
30 #include "routemap.h"
31 #include "thread.h"
32 #include "queue.h"
33 #include "memory.h"
34 #include "lib/json.h"
35
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_zebra.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_mpath.h"
44 #include "bgpd/bgp_nexthop.h"
45 #include "bgpd/bgp_nht.h"
46 #include "bgpd/bgp_bfd.h"
47
48 /* All information about zebra. */
49 struct zclient *zclient = NULL;
50 struct in_addr router_id_zebra;
51
52 /* Growable buffer for nexthops sent to zebra */
53 struct stream *bgp_nexthop_buf = NULL;
54 struct stream *bgp_ifindices_buf = NULL;
55
56 /* These array buffers are used in making a copy of the attributes for
57 route-map apply. Arrays are being used here to minimize mallocs and
58 frees for the temporary copy of the attributes.
59 Given the zapi api expects the nexthop buffer to contain pointer to
60 pointers for nexthops, we couldnt have used a single nexthop variable
61 on the stack, hence we had two options:
62 1. maintain a linked-list and free it after zapi_*_route call
63 2. use an array to avoid number of mallocs.
64 Number of supported next-hops are finite, use of arrays should be ok. */
65 struct attr attr_cp[BGP_MAXIMUM_MAXPATHS];
66 struct attr_extra attr_extra_cp[BGP_MAXIMUM_MAXPATHS];
67 int attr_index = 0;
68
69 /* Once per address-family initialization of the attribute array */
70 #define BGP_INFO_ATTR_BUF_INIT()\
71 do {\
72 memset(attr_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr));\
73 memset(attr_extra_cp, 0, BGP_MAXIMUM_MAXPATHS * sizeof(struct attr_extra));\
74 attr_index = 0;\
75 } while (0)
76
77 #define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
78 do { \
79 *info_dst = *info_src; \
80 assert(attr_index != BGP_MAXIMUM_MAXPATHS);\
81 attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
82 bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
83 bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
84 info_dst->attr = &attr_cp[attr_index]; \
85 attr_index++;\
86 } while (0)
87
88 #define BGP_INFO_ATTR_BUF_FREE(info) \
89 do { \
90 bgp_attr_deep_free(info->attr); \
91 } while (0)
92
93 /* Router-id update message from zebra. */
94 static int
95 bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
96 vrf_id_t vrf_id)
97 {
98 struct prefix router_id;
99 struct listnode *node, *nnode;
100 struct bgp *bgp;
101
102 zebra_router_id_update_read(zclient->ibuf,&router_id);
103
104 if (BGP_DEBUG (zebra, ZEBRA))
105 {
106 char buf[128];
107 prefix2str(&router_id, buf, sizeof(buf));
108 zlog_debug("Zebra rcvd: router id update %s", buf);
109 }
110
111 router_id_zebra = router_id.u.prefix4;
112
113 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
114 {
115 if (!bgp->router_id_static.s_addr)
116 bgp_router_id_set (bgp, &router_id.u.prefix4);
117 }
118
119 return 0;
120 }
121
122 /* Nexthop update message from zebra. */
123 static int
124 bgp_read_nexthop_update (int command, struct zclient *zclient,
125 zebra_size_t length, vrf_id_t vrf_id)
126 {
127 bgp_parse_nexthop_update(command, vrf_id);
128 return 0;
129 }
130
131 static int
132 bgp_read_import_check_update(int command, struct zclient *zclient,
133 zebra_size_t length, vrf_id_t vrf_id)
134 {
135 bgp_parse_nexthop_update(command, vrf_id);
136 return 0;
137 }
138
139 static void
140 bgp_start_interface_nbrs (struct interface *ifp)
141 {
142 struct listnode *node, *nnode, *mnode;
143 struct bgp *bgp;
144 struct peer *peer;
145
146 for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
147 {
148 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
149 {
150 if (peer->conf_if &&
151 (strcmp (peer->conf_if, ifp->name) == 0) &&
152 peer->status != Established)
153 {
154 if (peer_active(peer))
155 BGP_EVENT_ADD (peer, BGP_Stop);
156 BGP_EVENT_ADD (peer, BGP_Start);
157 }
158 }
159 }
160 }
161
162 static void
163 bgp_nbr_connected_add (struct nbr_connected *ifc)
164 {
165 struct listnode *node;
166 struct connected *connected;
167 struct interface *ifp;
168 struct prefix *p;
169
170 /* Kick-off the FSM for any relevant peers only if there is a
171 * valid local address on the interface.
172 */
173 ifp = ifc->ifp;
174 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
175 {
176 p = connected->address;
177 if (p->family == AF_INET6 &&
178 IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
179 break;
180 }
181 if (!connected)
182 return;
183
184 bgp_start_interface_nbrs (ifp);
185 }
186
187 static void
188 bgp_nbr_connected_delete (struct nbr_connected *ifc, int del)
189 {
190 struct listnode *node, *nnode, *mnode;
191 struct bgp *bgp;
192 struct peer *peer;
193 struct interface *ifp;
194
195 for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
196 {
197 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
198 {
199 if (peer->conf_if && (strcmp (peer->conf_if, ifc->ifp->name) == 0))
200 {
201 BGP_EVENT_ADD (peer, BGP_Stop);
202 }
203 }
204 }
205 /* Free neighbor also, if we're asked to. */
206 if (del)
207 {
208 ifp = ifc->ifp;
209 listnode_delete (ifp->nbr_connected, ifc);
210 nbr_connected_free (ifc);
211 }
212 }
213
214 /* Inteface addition message from zebra. */
215 static int
216 bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
217 vrf_id_t vrf_id)
218 {
219 struct interface *ifp;
220
221 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
222
223 if (BGP_DEBUG (zebra, ZEBRA) && ifp)
224 zlog_debug("Zebra rcvd: interface add %s", ifp->name);
225
226 return 0;
227 }
228
229 static int
230 bgp_interface_delete (int command, struct zclient *zclient,
231 zebra_size_t length, vrf_id_t vrf_id)
232 {
233 struct stream *s;
234 struct interface *ifp;
235
236 s = zclient->ibuf;
237 ifp = zebra_interface_state_read (s, vrf_id);
238 ifp->ifindex = IFINDEX_INTERNAL;
239
240 if (BGP_DEBUG (zebra, ZEBRA))
241 zlog_debug("Zebra rcvd: interface delete %s", ifp->name);
242
243 return 0;
244 }
245
246 static int
247 bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
248 vrf_id_t vrf_id)
249 {
250 struct stream *s;
251 struct interface *ifp;
252 struct connected *c;
253 struct nbr_connected *nc;
254 struct listnode *node, *nnode;
255
256 s = zclient->ibuf;
257 ifp = zebra_interface_state_read (s, vrf_id);
258
259 if (! ifp)
260 return 0;
261
262 if (BGP_DEBUG (zebra, ZEBRA))
263 zlog_debug("Zebra rcvd: interface %s up", ifp->name);
264
265 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
266 bgp_connected_add (c);
267
268 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
269 bgp_nbr_connected_add (nc);
270
271 return 0;
272 }
273
274 static int
275 bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
276 vrf_id_t vrf_id)
277 {
278 struct stream *s;
279 struct interface *ifp;
280 struct connected *c;
281 struct nbr_connected *nc;
282 struct listnode *node, *nnode;
283
284 s = zclient->ibuf;
285 ifp = zebra_interface_state_read (s, vrf_id);
286 if (! ifp)
287 return 0;
288
289 if (BGP_DEBUG (zebra, ZEBRA))
290 zlog_debug("Zebra rcvd: interface %s down", ifp->name);
291
292 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
293 bgp_connected_delete (c);
294
295 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
296 bgp_nbr_connected_delete (nc, 1);
297
298 /* Fast external-failover */
299 {
300 struct listnode *mnode;
301 struct bgp *bgp;
302 struct peer *peer;
303
304 for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
305 {
306 if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
307 continue;
308
309 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
310 {
311 if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
312 continue;
313
314 if (ifp == peer->nexthop.ifp)
315 BGP_EVENT_ADD (peer, BGP_Stop);
316 }
317 }
318 }
319
320 return 0;
321 }
322
323 static int
324 bgp_interface_address_add (int command, struct zclient *zclient,
325 zebra_size_t length, vrf_id_t vrf_id)
326 {
327 struct connected *ifc;
328
329 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
330
331 if (ifc == NULL)
332 return 0;
333
334 if (bgp_debug_zebra(ifc->address))
335 {
336 char buf[128];
337 prefix2str(ifc->address, buf, sizeof(buf));
338 zlog_debug("Zebra rcvd: interface %s address add %s",
339 ifc->ifp->name, buf);
340 }
341
342 if (if_is_operative (ifc->ifp))
343 {
344 bgp_connected_add (ifc);
345 /* If we have learnt of any neighbors on this interface,
346 * check to kick off any BGP interface-based neighbors,
347 * but only if this is a link-local address.
348 */
349 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) &&
350 !list_isempty(ifc->ifp->nbr_connected))
351 bgp_start_interface_nbrs (ifc->ifp);
352 }
353
354 return 0;
355 }
356
357 static int
358 bgp_interface_address_delete (int command, struct zclient *zclient,
359 zebra_size_t length, vrf_id_t vrf_id)
360 {
361 struct connected *ifc;
362
363 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
364
365 if (ifc == NULL)
366 return 0;
367
368 if (bgp_debug_zebra(ifc->address))
369 {
370 char buf[128];
371 prefix2str(ifc->address, buf, sizeof(buf));
372 zlog_debug("Zebra rcvd: interface %s address delete %s",
373 ifc->ifp->name, buf);
374 }
375
376 if (if_is_operative (ifc->ifp))
377 bgp_connected_delete (ifc);
378
379 connected_free (ifc);
380
381 return 0;
382 }
383
384 static int
385 bgp_interface_nbr_address_add (int command, struct zclient *zclient,
386 zebra_size_t length, vrf_id_t vrf_id)
387 {
388 struct nbr_connected *ifc = NULL;
389
390 ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
391
392 if (ifc == NULL)
393 return 0;
394
395 if (bgp_debug_zebra(ifc->address))
396 {
397 char buf[128];
398 prefix2str(ifc->address, buf, sizeof(buf));
399 zlog_debug("Zebra rcvd: interface %s nbr address add %s",
400 ifc->ifp->name, buf);
401 }
402
403 if (if_is_operative (ifc->ifp))
404 bgp_nbr_connected_add (ifc);
405
406 return 0;
407 }
408
409 static int
410 bgp_interface_nbr_address_delete (int command, struct zclient *zclient,
411 zebra_size_t length, vrf_id_t vrf_id)
412 {
413 struct nbr_connected *ifc = NULL;
414
415 ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
416
417 if (ifc == NULL)
418 return 0;
419
420 if (bgp_debug_zebra(ifc->address))
421 {
422 char buf[128];
423 prefix2str(ifc->address, buf, sizeof(buf));
424 zlog_debug("Zebra rcvd: interface %s nbr address delete %s",
425 ifc->ifp->name, buf);
426 }
427
428 if (if_is_operative (ifc->ifp))
429 bgp_nbr_connected_delete (ifc, 0);
430
431 nbr_connected_free (ifc);
432
433 return 0;
434 }
435
436 /* Zebra route add and delete treatment. */
437 static int
438 zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
439 vrf_id_t vrf_id)
440 {
441 struct stream *s;
442 struct zapi_ipv4 api;
443 struct in_addr nexthop;
444 struct prefix_ipv4 p;
445 unsigned int ifindex;
446 int i;
447
448 s = zclient->ibuf;
449 nexthop.s_addr = 0;
450
451 /* Type, flags, message. */
452 api.type = stream_getc (s);
453 api.instance = stream_getw (s);
454 api.flags = stream_getc (s);
455 api.message = stream_getc (s);
456
457 /* IPv4 prefix. */
458 memset (&p, 0, sizeof (struct prefix_ipv4));
459 p.family = AF_INET;
460 p.prefixlen = stream_getc (s);
461 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
462
463 /* Nexthop, ifindex, distance, metric. */
464 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
465 {
466 api.nexthop_num = stream_getc (s);
467 nexthop.s_addr = stream_get_ipv4 (s);
468 }
469
470 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
471 {
472 api.ifindex_num = stream_getc (s);
473 ifindex = stream_getl (s); /* ifindex, unused */
474 }
475 else
476 {
477 ifindex = 0;
478 }
479
480 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
481 api.distance = stream_getc (s);
482
483 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
484 api.metric = stream_getl (s);
485 else
486 api.metric = 0;
487
488 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
489 api.tag = stream_getw (s);
490 else
491 api.tag = 0;
492
493 if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD)
494 {
495 if (bgp_debug_zebra((struct prefix *)&p))
496 {
497 char buf[2][INET_ADDRSTRLEN];
498 zlog_debug("Zebra rcvd: IPv4 route add %s[%d] %s/%d nexthop %s metric %u tag %d",
499 zebra_route_string(api.type), api.instance,
500 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
501 p.prefixlen,
502 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
503 api.metric,
504 api.tag);
505 }
506
507 /*
508 * The ADD message is actually an UPDATE and there is no explicit DEL
509 * for a prior redistributed route, if any. So, perform an implicit
510 * DEL processing for the same redistributed route from any other
511 * source type.
512 */
513 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
514 {
515 if (i != api.type)
516 bgp_redistribute_delete((struct prefix *)&p, i, api.instance);
517 }
518
519 /* Now perform the add/update. */
520 bgp_redistribute_add((struct prefix *)&p, &nexthop, NULL, ifindex,
521 api.metric, api.type, api.instance, api.tag);
522 }
523 else if (command == ZEBRA_REDISTRIBUTE_IPV4_DEL)
524 {
525 if (bgp_debug_zebra((struct prefix *)&p))
526 {
527 char buf[2][INET_ADDRSTRLEN];
528 zlog_debug("Zebra rcvd: IPv4 route delete %s[%d] %s/%d "
529 "nexthop %s metric %u tag %d",
530 zebra_route_string(api.type), api.instance,
531 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
532 p.prefixlen,
533 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
534 api.metric,
535 api.tag);
536 }
537 bgp_redistribute_delete((struct prefix *)&p, api.type, api.instance);
538 }
539
540 return 0;
541 }
542
543 #ifdef HAVE_IPV6
544 /* Zebra route add and delete treatment. */
545 static int
546 zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
547 vrf_id_t vrf_id)
548 {
549 struct stream *s;
550 struct zapi_ipv6 api;
551 struct in6_addr nexthop;
552 struct prefix_ipv6 p;
553 unsigned int ifindex;
554 int i;
555
556 s = zclient->ibuf;
557 memset (&nexthop, 0, sizeof (struct in6_addr));
558
559 /* Type, flags, message. */
560 api.type = stream_getc (s);
561 api.instance = stream_getw (s);
562 api.flags = stream_getc (s);
563 api.message = stream_getc (s);
564
565 /* IPv6 prefix. */
566 memset (&p, 0, sizeof (struct prefix_ipv6));
567 p.family = AF_INET6;
568 p.prefixlen = stream_getc (s);
569 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
570
571 /* Nexthop, ifindex, distance, metric. */
572 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
573 {
574 api.nexthop_num = stream_getc (s);
575 stream_get (&nexthop, s, 16);
576 }
577
578 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
579 {
580 api.ifindex_num = stream_getc (s);
581 ifindex = stream_getl (s); /* ifindex, unused */
582 }
583 else
584 {
585 ifindex = 0;
586 }
587
588 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
589 api.distance = stream_getc (s);
590 else
591 api.distance = 0;
592
593 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
594 api.metric = stream_getl (s);
595 else
596 api.metric = 0;
597
598 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
599 api.tag = stream_getw (s);
600 else
601 api.tag = 0;
602
603 /* Simply ignore link-local address. */
604 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
605 return 0;
606
607 if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
608 {
609 if (bgp_debug_zebra((struct prefix *)&p))
610 {
611 char buf[2][INET6_ADDRSTRLEN];
612 zlog_debug("Zebra rcvd: IPv6 route add %s[%d] %s/%d nexthop %s metric %u tag %d",
613 zebra_route_string(api.type), api.instance,
614 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
615 p.prefixlen,
616 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
617 api.metric,
618 api.tag);
619 }
620
621 /*
622 * The ADD message is actually an UPDATE and there is no explicit DEL
623 * for a prior redistributed route, if any. So, perform an implicit
624 * DEL processing for the same redistributed route from any other
625 * source type.
626 */
627 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
628 {
629 if (i != api.type)
630 bgp_redistribute_delete((struct prefix *)&p, i, api.instance);
631 }
632
633 bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop, ifindex,
634 api.metric, api.type, api.instance, api.tag);
635 }
636 else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL)
637 {
638 if (bgp_debug_zebra((struct prefix *)&p))
639 {
640 char buf[2][INET6_ADDRSTRLEN];
641 zlog_debug("Zebra rcvd: IPv6 route delete %s[%d] %s/%d "
642 "nexthop %s metric %u tag %d",
643 zebra_route_string(api.type), api.instance,
644 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
645 p.prefixlen,
646 inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
647 api.metric,
648 api.tag);
649 }
650 bgp_redistribute_delete ((struct prefix *) &p, api.type, api.instance);
651 }
652
653 return 0;
654 }
655 #endif /* HAVE_IPV6 */
656
657 struct interface *
658 if_lookup_by_ipv4 (struct in_addr *addr)
659 {
660 struct listnode *ifnode;
661 struct listnode *cnode;
662 struct interface *ifp;
663 struct connected *connected;
664 struct prefix_ipv4 p;
665 struct prefix *cp;
666
667 p.family = AF_INET;
668 p.prefix = *addr;
669 p.prefixlen = IPV4_MAX_BITLEN;
670
671 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
672 {
673 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
674 {
675 cp = connected->address;
676
677 if (cp->family == AF_INET)
678 if (prefix_match (cp, (struct prefix *)&p))
679 return ifp;
680 }
681 }
682 return NULL;
683 }
684
685 struct interface *
686 if_lookup_by_ipv4_exact (struct in_addr *addr)
687 {
688 struct listnode *ifnode;
689 struct listnode *cnode;
690 struct interface *ifp;
691 struct connected *connected;
692 struct prefix *cp;
693
694 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
695 {
696 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
697 {
698 cp = connected->address;
699
700 if (cp->family == AF_INET)
701 if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
702 return ifp;
703 }
704 }
705 return NULL;
706 }
707
708 #ifdef HAVE_IPV6
709 struct interface *
710 if_lookup_by_ipv6 (struct in6_addr *addr, unsigned int ifindex)
711 {
712 struct listnode *ifnode;
713 struct listnode *cnode;
714 struct interface *ifp;
715 struct connected *connected;
716 struct prefix_ipv6 p;
717 struct prefix *cp;
718
719 p.family = AF_INET6;
720 p.prefix = *addr;
721 p.prefixlen = IPV6_MAX_BITLEN;
722
723 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
724 {
725 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
726 {
727 cp = connected->address;
728
729 if (cp->family == AF_INET6)
730 if (prefix_match (cp, (struct prefix *)&p))
731 {
732 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6.s6_addr32[0]))
733 {
734 if (ifindex == ifp->ifindex)
735 return ifp;
736 }
737 else
738 return ifp;
739 }
740 }
741 }
742 return NULL;
743 }
744
745 struct interface *
746 if_lookup_by_ipv6_exact (struct in6_addr *addr, unsigned int ifindex)
747 {
748 struct listnode *ifnode;
749 struct listnode *cnode;
750 struct interface *ifp;
751 struct connected *connected;
752 struct prefix *cp;
753
754 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
755 {
756 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
757 {
758 cp = connected->address;
759
760 if (cp->family == AF_INET6)
761 if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
762 {
763 if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
764 {
765 if (ifindex == ifp->ifindex)
766 return ifp;
767 }
768 else
769 return ifp;
770 }
771 }
772 }
773 return NULL;
774 }
775
776 static int
777 if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
778 {
779 struct listnode *cnode;
780 struct connected *connected;
781 struct prefix *cp;
782
783 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
784 {
785 cp = connected->address;
786
787 if (cp->family == AF_INET6)
788 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
789 {
790 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
791 return 1;
792 }
793 }
794 return 0;
795 }
796
797 static int
798 if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
799 {
800 struct listnode *cnode;
801 struct connected *connected;
802 struct prefix *cp;
803
804 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
805 {
806 cp = connected->address;
807
808 if (cp->family == AF_INET6)
809 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
810 {
811 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
812 return 1;
813 }
814 }
815 return 0;
816 }
817 #endif /* HAVE_IPV6 */
818
819 static int
820 if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
821 {
822 struct listnode *cnode;
823 struct connected *connected;
824 struct prefix *cp;
825
826 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
827 {
828 cp = connected->address;
829 if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
830 {
831 *addr = cp->u.prefix4;
832 return 1;
833 }
834 }
835 return 0;
836 }
837
838 int
839 bgp_nexthop_set (union sockunion *local, union sockunion *remote,
840 struct bgp_nexthop *nexthop, struct peer *peer)
841 {
842 int ret = 0;
843 struct interface *ifp = NULL;
844
845 memset (nexthop, 0, sizeof (struct bgp_nexthop));
846
847 if (!local)
848 return -1;
849 if (!remote)
850 return -1;
851
852 if (local->sa.sa_family == AF_INET)
853 {
854 nexthop->v4 = local->sin.sin_addr;
855 if (peer->update_if)
856 ifp = if_lookup_by_name (peer->update_if);
857 else
858 ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr);
859 }
860 #ifdef HAVE_IPV6
861 if (local->sa.sa_family == AF_INET6)
862 {
863 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
864 {
865 if (peer->conf_if || peer->ifname)
866 ifp = if_lookup_by_index (if_nametoindex (peer->conf_if ? peer->conf_if : peer->ifname));
867 }
868 else if (peer->update_if)
869 ifp = if_lookup_by_name (peer->update_if);
870 else
871 ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr,
872 local->sin6.sin6_scope_id);
873 }
874 #endif /* HAVE_IPV6 */
875
876 if (!ifp)
877 return -1;
878
879 nexthop->ifp = ifp;
880
881 /* IPv4 connection, fetch and store IPv6 local address(es) if any. */
882 if (local->sa.sa_family == AF_INET)
883 {
884 #ifdef HAVE_IPV6
885 /* IPv6 nexthop*/
886 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
887
888 /* There is no global nexthop. */
889 if (!ret)
890 if_get_ipv6_local (ifp, &nexthop->v6_global);
891 else
892 if_get_ipv6_local (ifp, &nexthop->v6_local);
893 #endif /* HAVE_IPV6 */
894 }
895
896 #ifdef HAVE_IPV6
897 /* IPv6 connection, fetch and store IPv4 local address if any. */
898 if (local->sa.sa_family == AF_INET6)
899 {
900 struct interface *direct = NULL;
901
902 /* IPv4 nexthop. */
903 ret = if_get_ipv4_address(ifp, &nexthop->v4);
904 if (!ret && peer->local_id.s_addr)
905 nexthop->v4 = peer->local_id;
906
907 /* Global address*/
908 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
909 {
910 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
911 IPV6_MAX_BYTELEN);
912
913 /* If directory connected set link-local address. */
914 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr,
915 remote->sin6.sin6_scope_id);
916 if (direct)
917 if_get_ipv6_local (ifp, &nexthop->v6_local);
918 }
919 else
920 /* Link-local address. */
921 {
922 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
923
924 /* If there is no global address. Set link-local address as
925 global. I know this break RFC specification... */
926 /* In this scenario, the expectation for interop is that the
927 * network admin would use a route-map to specify the global
928 * IPv6 nexthop.
929 */
930 if (!ret)
931 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
932 IPV6_MAX_BYTELEN);
933 /* Always set the link-local address */
934 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
935 IPV6_MAX_BYTELEN);
936 }
937 }
938
939 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
940 if_lookup_by_ipv6 (&remote->sin6.sin6_addr, remote->sin6.sin6_scope_id))
941 peer->shared_network = 1;
942 else
943 peer->shared_network = 0;
944
945 /* KAME stack specific treatment. */
946 #ifdef KAME
947 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
948 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
949 {
950 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
951 }
952 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
953 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
954 {
955 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
956 }
957 #endif /* KAME */
958 #endif /* HAVE_IPV6 */
959
960 /* If we have identified the local interface, there is no error for now. */
961 return 0;
962 }
963
964 static struct in6_addr *
965 bgp_info_to_ipv6_nexthop (struct bgp_info *info)
966 {
967 struct in6_addr *nexthop = NULL;
968
969 /* Only global address nexthop exists. */
970 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL)
971 nexthop = &info->attr->extra->mp_nexthop_global;
972
973 /* If both global and link-local address present. */
974 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
975 {
976 /* Workaround for Cisco's nexthop bug. */
977 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
978 && info->peer->su_remote->sa.sa_family == AF_INET6)
979 nexthop = &info->peer->su_remote->sin6.sin6_addr;
980 else
981 nexthop = &info->attr->extra->mp_nexthop_local;
982 }
983
984 return nexthop;
985 }
986
987 static int
988 bgp_table_map_apply (struct route_map *map, struct prefix *p,
989 struct bgp_info *info)
990 {
991 if (route_map_apply(map, p, RMAP_BGP, info) != RMAP_DENYMATCH)
992 return 1;
993
994 if (bgp_debug_zebra(p))
995 {
996 if (p->family == AF_INET)
997 {
998 char buf[2][INET_ADDRSTRLEN];
999 zlog_debug("Zebra rmap deny: IPv4 route %s/%d nexthop %s",
1000 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1001 p->prefixlen,
1002 inet_ntop(AF_INET, &info->attr->nexthop, buf[1],
1003 sizeof(buf[1])));
1004 }
1005 if (p->family == AF_INET6)
1006 {
1007 char buf[2][INET6_ADDRSTRLEN];
1008 zlog_debug("Zebra rmap deny: IPv6 route %s/%d nexthop %s",
1009 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1010 p->prefixlen,
1011 inet_ntop(AF_INET6, bgp_info_to_ipv6_nexthop(info), buf[1],
1012 sizeof(buf[1])));
1013 }
1014 }
1015 return 0;
1016 }
1017
1018 void
1019 bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp,
1020 afi_t afi, safi_t safi)
1021 {
1022 int flags;
1023 u_char distance;
1024 struct peer *peer;
1025 struct bgp_info *mpinfo;
1026 size_t oldsize, newsize;
1027 u_int32_t nhcount, metric;
1028 struct bgp_info local_info;
1029 struct bgp_info *info_cp = &local_info;
1030 u_short tag;
1031
1032 if (zclient->sock < 0)
1033 return;
1034
1035 if ((p->family == AF_INET &&
1036 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], VRF_DEFAULT))
1037 || (p->family == AF_INET6 &&
1038 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], VRF_DEFAULT)))
1039 return;
1040
1041 if (bgp->main_zebra_update_hold)
1042 return;
1043
1044 flags = 0;
1045 peer = info->peer;
1046
1047 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1048 tag = info->attr->extra->tag;
1049 else
1050 tag = 0;
1051
1052 /* When we create an aggregate route we must also install a Null0 route in
1053 * the RIB */
1054 if (info->sub_type == BGP_ROUTE_AGGREGATE)
1055 SET_FLAG (flags, ZEBRA_FLAG_BLACKHOLE);
1056
1057 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED ||
1058 info->sub_type == BGP_ROUTE_AGGREGATE)
1059 {
1060 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1061 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1062 }
1063
1064 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1065 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1066 || bgp_flag_check(bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1067
1068 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1069
1070 nhcount = 1 + bgp_info_mpath_count (info);
1071
1072 if (p->family == AF_INET && !BGP_ATTR_NEXTHOP_AFI_IP6(info->attr))
1073 {
1074 struct zapi_ipv4 api;
1075 struct in_addr *nexthop;
1076 char buf[2][INET_ADDRSTRLEN];
1077 int valid_nh_count = 0;
1078
1079 /* resize nexthop buffer size if necessary */
1080 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1081 (sizeof (struct in_addr *) * nhcount))
1082 {
1083 newsize = (sizeof (struct in_addr *) * nhcount);
1084 newsize = stream_resize (bgp_nexthop_buf, newsize);
1085 if (newsize == oldsize)
1086 {
1087 zlog_err ("can't resize nexthop buffer");
1088 return;
1089 }
1090 }
1091 stream_reset (bgp_nexthop_buf);
1092 nexthop = NULL;
1093
1094 /* Metric is currently based on the best-path only. */
1095 metric = info->attr->med;
1096
1097 if (bgp->table_map[afi][safi].name)
1098 {
1099 BGP_INFO_ATTR_BUF_INIT();
1100
1101 /* Copy info and attributes, so the route-map apply doesn't modify the
1102 BGP route info. */
1103 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1104 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1105 {
1106 metric = info_cp->attr->med;
1107 nexthop = &info_cp->attr->nexthop;
1108
1109 if (info_cp->attr->extra)
1110 tag = info_cp->attr->extra->tag;
1111 }
1112 BGP_INFO_ATTR_BUF_FREE(info_cp);
1113 }
1114 else
1115 {
1116 nexthop = &info->attr->nexthop;
1117 }
1118
1119 if (nexthop)
1120 {
1121 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1122 valid_nh_count++;
1123 }
1124
1125 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
1126 mpinfo = bgp_info_mpath_next (mpinfo))
1127 {
1128 nexthop = NULL;
1129
1130 if (bgp->table_map[afi][safi].name)
1131 {
1132 /* Copy info and attributes, so the route-map apply doesn't modify the
1133 BGP route info. */
1134 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1135 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1136 nexthop = &info_cp->attr->nexthop;
1137 BGP_INFO_ATTR_BUF_FREE(info_cp);
1138 }
1139 else
1140 {
1141 nexthop = &mpinfo->attr->nexthop;
1142 }
1143
1144 if (nexthop == NULL)
1145 continue;
1146
1147 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
1148 valid_nh_count++;
1149 }
1150
1151 api.vrf_id = VRF_DEFAULT;
1152 api.flags = flags;
1153 api.type = ZEBRA_ROUTE_BGP;
1154 api.instance = 0;
1155 api.message = 0;
1156 api.safi = safi;
1157 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1158
1159 /* Note that this currently only applies to Null0 routes for aggregates.
1160 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv4_route to encode a special
1161 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1162 * do not want to also encode the 0.0.0.0 nexthop for the aggregate route.
1163 */
1164 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1165 api.nexthop_num = 0;
1166 else
1167 api.nexthop_num = valid_nh_count;
1168
1169 api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
1170 api.ifindex_num = 0;
1171 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1172 api.metric = metric;
1173
1174 if (tag)
1175 {
1176 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1177 api.tag = tag;
1178 }
1179
1180 distance = bgp_distance_apply (p, info, bgp);
1181
1182 if (distance)
1183 {
1184 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
1185 api.distance = distance;
1186 }
1187
1188 if (bgp_debug_zebra(p))
1189 {
1190 int i;
1191 zlog_debug("Zebra send: IPv4 route %s %s/%d metric %u tag %d"
1192 " count %d", (valid_nh_count ? "add":"delete"),
1193 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1194 p->prefixlen, api.metric, api.tag, api.nexthop_num);
1195 for (i = 0; i < api.nexthop_num; i++)
1196 zlog_debug(" IPv4 [nexthop %d] %s", i+1,
1197 inet_ntop(AF_INET, api.nexthop[i], buf[1], sizeof(buf[1])));
1198 }
1199
1200 zapi_ipv4_route (valid_nh_count ? ZEBRA_IPV4_ROUTE_ADD: ZEBRA_IPV4_ROUTE_DELETE,
1201 zclient, (struct prefix_ipv4 *) p, &api);
1202 }
1203 #ifdef HAVE_IPV6
1204
1205 /* We have to think about a IPv6 link-local address curse. */
1206 if (p->family == AF_INET6 ||
1207 (p->family == AF_INET && BGP_ATTR_NEXTHOP_AFI_IP6(info->attr)))
1208 {
1209 unsigned int ifindex;
1210 struct in6_addr *nexthop;
1211 struct zapi_ipv6 api;
1212 int valid_nh_count = 0;
1213 char buf[2][INET6_ADDRSTRLEN];
1214
1215 /* resize nexthop buffer size if necessary */
1216 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
1217 (sizeof (struct in6_addr *) * nhcount))
1218 {
1219 newsize = (sizeof (struct in6_addr *) * nhcount);
1220 newsize = stream_resize (bgp_nexthop_buf, newsize);
1221 if (newsize == oldsize)
1222 {
1223 zlog_err ("can't resize nexthop buffer");
1224 return;
1225 }
1226 }
1227 stream_reset (bgp_nexthop_buf);
1228
1229 /* resize ifindices buffer size if necessary */
1230 if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
1231 (sizeof (unsigned int) * nhcount))
1232 {
1233 newsize = (sizeof (unsigned int) * nhcount);
1234 newsize = stream_resize (bgp_ifindices_buf, newsize);
1235 if (newsize == oldsize)
1236 {
1237 zlog_err ("can't resize nexthop buffer");
1238 return;
1239 }
1240 }
1241 stream_reset (bgp_ifindices_buf);
1242
1243 ifindex = 0;
1244 nexthop = NULL;
1245
1246 assert (info->attr->extra);
1247
1248 /* Metric is currently based on the best-path only. */
1249 metric = info->attr->med;
1250
1251 if (bgp->table_map[afi][safi].name)
1252 {
1253 BGP_INFO_ATTR_BUF_INIT();
1254
1255 /* Copy info and attributes, so the route-map apply doesn't modify the
1256 BGP route info. */
1257 BGP_INFO_ATTR_BUF_COPY(info, info_cp);
1258 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1259 {
1260 metric = info_cp->attr->med;
1261 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
1262
1263 if (info_cp->attr->extra)
1264 tag = info_cp->attr->extra->tag;
1265 }
1266 BGP_INFO_ATTR_BUF_FREE(info_cp);
1267 }
1268 else
1269 {
1270 nexthop = bgp_info_to_ipv6_nexthop(info);
1271 }
1272
1273 if (nexthop)
1274 {
1275 if (info->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1276 if (info->peer->nexthop.ifp)
1277 ifindex = info->peer->nexthop.ifp->ifindex;
1278
1279 if (!ifindex)
1280 {
1281 if (info->peer->conf_if || info->peer->ifname)
1282 ifindex = if_nametoindex (info->peer->conf_if ? info->peer->conf_if : info->peer->ifname);
1283 else if (info->peer->nexthop.ifp)
1284 ifindex = info->peer->nexthop.ifp->ifindex;
1285 }
1286 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1287 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1288 valid_nh_count++;
1289 }
1290
1291 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
1292 mpinfo = bgp_info_mpath_next (mpinfo))
1293 {
1294 ifindex = 0;
1295 nexthop = NULL;
1296
1297 if (bgp->table_map[afi][safi].name)
1298 {
1299 /* Copy info and attributes, so the route-map apply doesn't modify the
1300 BGP route info. */
1301 BGP_INFO_ATTR_BUF_COPY(mpinfo, info_cp);
1302 if (bgp_table_map_apply(bgp->table_map[afi][safi].map, p, info_cp))
1303 nexthop = bgp_info_to_ipv6_nexthop(info_cp);
1304 BGP_INFO_ATTR_BUF_FREE(info_cp);
1305 }
1306 else
1307 {
1308 nexthop = bgp_info_to_ipv6_nexthop(mpinfo);
1309 }
1310
1311 if (nexthop == NULL)
1312 continue;
1313
1314 if (mpinfo->attr->extra->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
1315 if (mpinfo->peer->nexthop.ifp)
1316 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
1317
1318 if (!ifindex)
1319 {
1320 if (mpinfo->peer->conf_if || mpinfo->peer->ifname)
1321 ifindex = if_nametoindex (mpinfo->peer->conf_if ? mpinfo->peer->conf_if : mpinfo->peer->ifname);
1322 else if (mpinfo->peer->nexthop.ifp)
1323 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
1324 }
1325 if (ifindex == 0)
1326 continue;
1327
1328 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
1329 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
1330 valid_nh_count++;
1331 }
1332
1333 /* Make Zebra API structure. */
1334 api.vrf_id = VRF_DEFAULT;
1335 api.flags = flags;
1336 api.type = ZEBRA_ROUTE_BGP;
1337 api.instance = 0;
1338 api.message = 0;
1339 api.safi = safi;
1340 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1341
1342 /* Note that this currently only applies to Null0 routes for aggregates.
1343 * ZEBRA_FLAG_BLACKHOLE signals zapi_ipv6_route to encode a special
1344 * BLACKHOLE nexthop. We want to set api.nexthop_num to zero since we
1345 * do not want to also encode the :: nexthop for the aggregate route.
1346 */
1347 if (CHECK_FLAG(flags, ZEBRA_FLAG_BLACKHOLE))
1348 api.nexthop_num = 0;
1349 else
1350 api.nexthop_num = valid_nh_count;
1351
1352 api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
1353 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
1354 api.ifindex_num = valid_nh_count;
1355 api.ifindex = (unsigned int *)STREAM_DATA (bgp_ifindices_buf);
1356 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1357 api.metric = metric;
1358
1359 if (tag)
1360 {
1361 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
1362 api.tag = tag;
1363 }
1364
1365 if (p->family == AF_INET)
1366 {
1367 if (bgp_debug_zebra(p))
1368 {
1369 int i;
1370 zlog_debug("Zebra send: IPv4 route %s %s/%d metric %u tag %d",
1371 valid_nh_count ? "add" : "delete",
1372 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1373 p->prefixlen, api.metric, api.tag);
1374 for (i = 0; i < api.nexthop_num; i++)
1375 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1376 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1377 }
1378
1379 if (valid_nh_count)
1380 zapi_ipv4_route_ipv6_nexthop (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
1381 zclient, (struct prefix_ipv4 *) p,
1382 (struct zapi_ipv6 *)&api);
1383 else
1384 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE,
1385 zclient, (struct prefix_ipv4 *) p, (struct zapi_ipv4 *)&api);
1386 }
1387 else
1388 {
1389 if (bgp_debug_zebra(p))
1390 {
1391 int i;
1392 zlog_debug("Zebra send: IPv6 route %s %s/%d metric %u tag %d",
1393 valid_nh_count ? "add" : "delete",
1394 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1395 p->prefixlen, api.metric, api.tag);
1396 for (i = 0; i < api.nexthop_num; i++)
1397 zlog_debug(" IPv6 [nexthop %d] %s", i+1,
1398 inet_ntop(AF_INET6, api.nexthop[i], buf[1], sizeof(buf[1])));
1399 }
1400
1401 zapi_ipv6_route (valid_nh_count ?
1402 ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE,
1403 zclient, (struct prefix_ipv6 *) p, &api);
1404 }
1405 }
1406 #endif /* HAVE_IPV6 */
1407 }
1408
1409 /* Announce all routes of a table to zebra */
1410 void
1411 bgp_zebra_announce_table (struct bgp *bgp, afi_t afi, safi_t safi)
1412 {
1413 struct bgp_node *rn;
1414 struct bgp_table *table;
1415 struct bgp_info *ri;
1416
1417 table = bgp->rib[afi][safi];
1418 if (!table) return;
1419
1420 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
1421 for (ri = rn->info; ri; ri = ri->next)
1422 if (CHECK_FLAG (ri->flags, BGP_INFO_SELECTED)
1423 && ri->type == ZEBRA_ROUTE_BGP
1424 && ri->sub_type == BGP_ROUTE_NORMAL)
1425 bgp_zebra_announce (&rn->p, ri, bgp, afi, safi);
1426 }
1427
1428 void
1429 bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
1430 {
1431 int flags;
1432 struct peer *peer;
1433
1434 if (zclient->sock < 0)
1435 return;
1436
1437 if ((p->family == AF_INET &&
1438 !vrf_bitmap_check (zclient->redist[AFI_IP][ZEBRA_ROUTE_BGP], VRF_DEFAULT))
1439 || (p->family == AF_INET6 &&
1440 !vrf_bitmap_check (zclient->redist[AFI_IP6][ZEBRA_ROUTE_BGP], VRF_DEFAULT)))
1441 return;
1442
1443 peer = info->peer;
1444
1445 flags = 0;
1446
1447 if (peer->sort == BGP_PEER_IBGP)
1448 {
1449 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1450 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1451 }
1452
1453 if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1)
1454 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
1455 || bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
1456 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1457
1458 if (p->family == AF_INET)
1459 {
1460 struct zapi_ipv4 api;
1461
1462 api.vrf_id = VRF_DEFAULT;
1463 api.flags = flags;
1464
1465 api.type = ZEBRA_ROUTE_BGP;
1466 api.instance = 0;
1467 api.message = 0;
1468 api.safi = safi;
1469 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1470 api.nexthop_num = 0;
1471 api.nexthop = NULL;
1472 api.ifindex_num = 0;
1473 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1474 api.metric = info->attr->med;
1475
1476 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1477 {
1478 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1479 api.tag = info->attr->extra->tag;
1480 }
1481
1482 if (bgp_debug_zebra(p))
1483 {
1484 char buf[2][INET_ADDRSTRLEN];
1485 zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u tag %d",
1486 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1487 p->prefixlen, api.metric, api.tag);
1488 }
1489
1490 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
1491 (struct prefix_ipv4 *) p, &api);
1492 }
1493 #ifdef HAVE_IPV6
1494 /* We have to think about a IPv6 link-local address curse. */
1495 if (p->family == AF_INET6)
1496 {
1497 struct zapi_ipv6 api;
1498
1499 assert (info->attr->extra);
1500
1501 api.vrf_id = VRF_DEFAULT;
1502 api.flags = flags;
1503 api.type = ZEBRA_ROUTE_BGP;
1504 api.instance = 0;
1505 api.message = 0;
1506 api.safi = safi;
1507 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1508 api.nexthop_num = 0;
1509 api.nexthop = NULL;
1510 api.ifindex_num = 0;
1511 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1512 api.metric = info->attr->med;
1513
1514 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1515 {
1516 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1517 api.tag = info->attr->extra->tag;
1518 }
1519
1520 if (bgp_debug_zebra(p))
1521 {
1522 char buf[2][INET6_ADDRSTRLEN];
1523 zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u tag %d",
1524 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1525 p->prefixlen, api.metric, api.tag);
1526 }
1527
1528 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
1529 (struct prefix_ipv6 *) p, &api);
1530 }
1531 #endif /* HAVE_IPV6 */
1532 }
1533 struct bgp_redist *
1534 bgp_redist_lookup (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1535 {
1536 struct list *red_list;
1537 struct listnode *node;
1538 struct bgp_redist *red;
1539
1540 red_list = bgp->redist[afi][type];
1541 if (!red_list)
1542 return(NULL);
1543
1544 for (ALL_LIST_ELEMENTS_RO(red_list, node, red))
1545 if (red->instance == instance)
1546 return red;
1547
1548 return NULL;
1549 }
1550
1551 struct bgp_redist *
1552 bgp_redist_add (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1553 {
1554 struct list *red_list;
1555 struct bgp_redist *red;
1556
1557 red = bgp_redist_lookup(bgp, afi, type, instance);
1558 if (red)
1559 return red;
1560
1561 if (!bgp->redist[afi][type])
1562 bgp->redist[afi][type] = list_new();
1563
1564 red_list = bgp->redist[afi][type];
1565 red = (struct bgp_redist *)XCALLOC(MTYPE_BGP_REDIST, sizeof(struct bgp_redist));
1566 red->instance = instance;
1567
1568 listnode_add(red_list, red);
1569
1570 return red;
1571 }
1572
1573 static void
1574 bgp_redist_del (struct bgp *bgp, afi_t afi, u_char type, u_short instance)
1575 {
1576 struct bgp_redist *red;
1577
1578 red = bgp_redist_lookup(bgp, afi, type, instance);
1579
1580 if (red)
1581 {
1582 listnode_delete(bgp->redist[afi][type], red);
1583 if (!bgp->redist[afi][type]->count)
1584 {
1585 list_free(bgp->redist[afi][type]);
1586 bgp->redist[afi][type] = NULL;
1587 }
1588 }
1589 }
1590
1591 /* Other routes redistribution into BGP. */
1592 int
1593 bgp_redistribute_set (afi_t afi, int type, u_short instance)
1594 {
1595
1596 /* Return if already redistribute flag is set. */
1597 if (instance)
1598 {
1599 if (redist_check_instance(&zclient->mi_redist[afi][type], instance))
1600 return CMD_WARNING;
1601
1602 redist_add_instance(&zclient->mi_redist[afi][type], instance);
1603 }
1604 else
1605 {
1606 if (vrf_bitmap_check (zclient->redist[afi][type], VRF_DEFAULT))
1607 return CMD_WARNING;
1608
1609 vrf_bitmap_set (zclient->redist[afi][type], VRF_DEFAULT);
1610 }
1611
1612 /* Return if zebra connection is not established. */
1613 if (zclient->sock < 0)
1614 return CMD_WARNING;
1615
1616 if (BGP_DEBUG (zebra, ZEBRA))
1617 zlog_debug("Zebra send: redistribute add afi %d %s %d", afi,
1618 zebra_route_string(type), instance);
1619
1620 /* Send distribute add message to zebra. */
1621 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1622 instance, VRF_DEFAULT);
1623
1624 return CMD_SUCCESS;
1625 }
1626
1627 int
1628 bgp_redistribute_resend (struct bgp *bgp, afi_t afi, int type, u_short instance)
1629 {
1630 /* Return if zebra connection is not established. */
1631 if (zclient->sock < 0)
1632 return -1;
1633
1634 if (BGP_DEBUG (zebra, ZEBRA))
1635 zlog_debug("Zebra send: redistribute delete/add afi %d %s %d", afi,
1636 zebra_route_string(type), instance);
1637
1638 /* Send distribute add message to zebra. */
1639 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type,
1640 instance, VRF_DEFAULT);
1641 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, afi, type,
1642 instance, VRF_DEFAULT);
1643
1644 return 0;
1645 }
1646
1647 /* Redistribute with route-map specification. */
1648 int
1649 bgp_redistribute_rmap_set (struct bgp_redist *red, const char *name)
1650 {
1651 if (red->rmap.name
1652 && (strcmp (red->rmap.name, name) == 0))
1653 return 0;
1654
1655 if (red->rmap.name)
1656 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1657 red->rmap.name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, name);
1658 red->rmap.map = route_map_lookup_by_name (name);
1659
1660 return 1;
1661 }
1662
1663 /* Redistribute with metric specification. */
1664 int
1665 bgp_redistribute_metric_set (struct bgp *bgp, struct bgp_redist *red, afi_t afi,
1666 int type, u_int32_t metric)
1667 {
1668 struct bgp_node *rn;
1669 struct bgp_info *ri;
1670
1671 if (red->redist_metric_flag
1672 && red->redist_metric == metric)
1673 return 0;
1674
1675 red->redist_metric_flag = 1;
1676 red->redist_metric = metric;
1677
1678 for (rn = bgp_table_top(bgp->rib[afi][SAFI_UNICAST]); rn; rn = bgp_route_next(rn)) {
1679 for (ri = rn->info; ri; ri = ri->next) {
1680 if (ri->sub_type == BGP_ROUTE_REDISTRIBUTE && ri->type == type &&
1681 ri->instance == red->instance) {
1682 ri->attr->med = red->redist_metric;
1683 bgp_info_set_flag(rn, ri, BGP_INFO_ATTR_CHANGED);
1684 bgp_process(bgp, rn, afi, SAFI_UNICAST);
1685 }
1686 }
1687 }
1688
1689 return 1;
1690 }
1691
1692 /* Unset redistribution. */
1693 int
1694 bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type, u_short instance)
1695 {
1696 struct bgp_redist *red;
1697
1698 red = bgp_redist_lookup(bgp, afi, type, instance);
1699 if (!red)
1700 return CMD_SUCCESS;
1701
1702 /* Unset route-map. */
1703 if (red->rmap.name)
1704 XFREE(MTYPE_ROUTE_MAP_NAME, red->rmap.name);
1705 red->rmap.name = NULL;
1706 red->rmap.map = NULL;
1707
1708 /* Unset metric. */
1709 red->redist_metric_flag = 0;
1710 red->redist_metric = 0;
1711
1712 bgp_redist_del(bgp, afi, type, instance);
1713
1714 /* Return if zebra connection is disabled. */
1715 if (instance)
1716 {
1717 if (!redist_check_instance(&zclient->mi_redist[afi][type], instance))
1718 return CMD_WARNING;
1719 redist_del_instance(&zclient->mi_redist[afi][type], instance);
1720 }
1721 else
1722 {
1723 if (! vrf_bitmap_check (zclient->redist[afi][type], VRF_DEFAULT))
1724 return CMD_WARNING;
1725 vrf_bitmap_unset (zclient->redist[afi][type], VRF_DEFAULT);
1726 }
1727
1728 if (zclient->sock >= 0)
1729 {
1730 /* Send distribute delete message to zebra. */
1731 if (BGP_DEBUG (zebra, ZEBRA))
1732 zlog_debug("Zebra send: redistribute delete afi %d %s %d",
1733 afi, zebra_route_string(type), instance);
1734 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, type, instance,
1735 VRF_DEFAULT);
1736 }
1737
1738 /* Withdraw redistributed routes from current BGP's routing table. */
1739 bgp_redistribute_withdraw (bgp, afi, type, instance);
1740
1741 return CMD_SUCCESS;
1742 }
1743
1744 void
1745 bgp_zclient_reset (void)
1746 {
1747 zclient_reset (zclient);
1748 }
1749
1750 static void
1751 bgp_zebra_connected (struct zclient *zclient)
1752 {
1753 zclient_send_requests (zclient, VRF_DEFAULT);
1754
1755 bgp_nht_register_all();
1756 }
1757
1758 void
1759 bgp_zebra_init (struct thread_master *master)
1760 {
1761 /* Set default values. */
1762 zclient = zclient_new (master);
1763 zclient_init (zclient, ZEBRA_ROUTE_BGP, 0);
1764 zclient->zebra_connected = bgp_zebra_connected;
1765 zclient->router_id_update = bgp_router_id_update;
1766 zclient->interface_add = bgp_interface_add;
1767 zclient->interface_delete = bgp_interface_delete;
1768 zclient->interface_address_add = bgp_interface_address_add;
1769 zclient->interface_address_delete = bgp_interface_address_delete;
1770 zclient->interface_nbr_address_add = bgp_interface_nbr_address_add;
1771 zclient->interface_nbr_address_delete = bgp_interface_nbr_address_delete;
1772 zclient->ipv4_route_add = zebra_read_ipv4;
1773 zclient->ipv4_route_delete = zebra_read_ipv4;
1774 zclient->redistribute_route_ipv4_add = zebra_read_ipv4;
1775 zclient->redistribute_route_ipv4_del = zebra_read_ipv4;
1776 zclient->interface_up = bgp_interface_up;
1777 zclient->interface_down = bgp_interface_down;
1778 #ifdef HAVE_IPV6
1779 zclient->ipv6_route_add = zebra_read_ipv6;
1780 zclient->ipv6_route_delete = zebra_read_ipv6;
1781 zclient->redistribute_route_ipv6_add = zebra_read_ipv6;
1782 zclient->redistribute_route_ipv6_del = zebra_read_ipv6;
1783 #endif /* HAVE_IPV6 */
1784 zclient->nexthop_update = bgp_read_nexthop_update;
1785 zclient->import_check_update = bgp_read_import_check_update;
1786
1787 bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
1788 bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
1789 }