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