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