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 "filter.h"
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"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_mpath.h"
42 #include "bgpd/bgp_nexthop.h"
43 #include "bgpd/bgp_nht.h"
44
45 /* All information about zebra. */
46 struct zclient *zclient = NULL;
47 struct in_addr router_id_zebra;
48
49 /* Growable buffer for nexthops sent to zebra */
50 struct stream *bgp_nexthop_buf = NULL;
51 struct stream *bgp_ifindices_buf = NULL;
52
53 int zclient_num_connects;
54
55 /* Router-id update message from zebra. */
56 static int
bgp_router_id_update(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)57 bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
58 vrf_id_t vrf_id)
59 {
60 struct prefix router_id;
61
62 zebra_router_id_update_read(zclient->ibuf,&router_id);
63
64 if (BGP_DEBUG(zebra, ZEBRA))
65 {
66 char buf[128];
67 prefix2str(&router_id, buf, sizeof(buf));
68 zlog_debug("Zebra rcvd: router id update %s", buf);
69 }
70
71 router_id_zebra = router_id.u.prefix4;
72
73 bgp_router_id_zebra_bump ();
74 return 0;
75 }
76
77 /* Nexthop update message from zebra. */
78 static int
bgp_read_nexthop_update(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)79 bgp_read_nexthop_update (int command, struct zclient *zclient,
80 zebra_size_t length, vrf_id_t vrf_id)
81 {
82 bgp_parse_nexthop_update();
83 return 0;
84 }
85
86 /* Inteface addition message from zebra. */
87 static int
bgp_interface_add(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)88 bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
89 vrf_id_t vrf_id)
90 {
91 struct interface *ifp;
92
93 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
94
95 if (BGP_DEBUG(zebra, ZEBRA) && ifp)
96 zlog_debug("Zebra rcvd: interface add %s", ifp->name);
97
98 return 0;
99 }
100
101 static int
bgp_interface_delete(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)102 bgp_interface_delete (int command, struct zclient *zclient,
103 zebra_size_t length, vrf_id_t vrf_id)
104 {
105 struct stream *s;
106 struct interface *ifp;
107
108 s = zclient->ibuf;
109 ifp = zebra_interface_state_read (s, vrf_id);
110 if (! ifp)
111 return 0;
112
113 ifp->ifindex = IFINDEX_INTERNAL;
114
115 if (BGP_DEBUG(zebra, ZEBRA))
116 zlog_debug("Zebra rcvd: interface delete %s", ifp->name);
117
118 return 0;
119 }
120
121 static int
bgp_interface_up(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)122 bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
123 vrf_id_t vrf_id)
124 {
125 struct stream *s;
126 struct interface *ifp;
127 struct connected *c;
128 struct listnode *node, *nnode;
129
130 s = zclient->ibuf;
131 ifp = zebra_interface_state_read (s, vrf_id);
132
133 if (! ifp)
134 return 0;
135
136 if (BGP_DEBUG(zebra, ZEBRA))
137 zlog_debug("Zebra rcvd: interface %s up", ifp->name);
138
139 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
140 bgp_connected_add (c);
141
142 return 0;
143 }
144
145 static int
bgp_interface_down(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)146 bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
147 vrf_id_t vrf_id)
148 {
149 struct stream *s;
150 struct interface *ifp;
151 struct connected *c;
152 struct listnode *node, *nnode;
153
154 s = zclient->ibuf;
155 ifp = zebra_interface_state_read (s, vrf_id);
156 if (! ifp)
157 return 0;
158
159 if (BGP_DEBUG(zebra, ZEBRA))
160 zlog_debug("Zebra rcvd: interface %s down", ifp->name);
161
162 for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
163 bgp_connected_delete (c);
164
165 /* Fast external-failover */
166 {
167 struct listnode *mnode;
168 struct bgp *bgp;
169 struct peer *peer;
170
171 for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp))
172 {
173 if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
174 continue;
175
176 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
177 {
178 if (peer->gtsm_hops != 1 && peer_ttl (peer) != 1)
179 continue;
180 if (ifp == peer->nexthop.ifp)
181 BGP_EVENT_ADD (peer, BGP_Stop);
182 }
183 }
184 }
185
186 return 0;
187 }
188
189 static int
bgp_interface_address_add(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)190 bgp_interface_address_add (int command, struct zclient *zclient,
191 zebra_size_t length, vrf_id_t vrf_id)
192 {
193 struct connected *ifc;
194
195 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
196
197 if (ifc == NULL)
198 return 0;
199
200 if (BGP_DEBUG(zebra, ZEBRA))
201 {
202 char buf[128];
203 prefix2str(ifc->address, buf, sizeof(buf));
204 zlog_debug("Zebra rcvd: interface %s address add %s",
205 ifc->ifp->name, buf);
206 }
207
208 if (if_is_operative (ifc->ifp))
209 bgp_connected_add (ifc);
210
211 return 0;
212 }
213
214 static int
bgp_interface_address_delete(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)215 bgp_interface_address_delete (int command, struct zclient *zclient,
216 zebra_size_t length, vrf_id_t vrf_id)
217 {
218 struct connected *ifc;
219
220 ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
221
222 if (ifc == NULL)
223 return 0;
224
225 if (BGP_DEBUG(zebra, ZEBRA))
226 {
227 char buf[128];
228 prefix2str(ifc->address, buf, sizeof(buf));
229 zlog_debug("Zebra rcvd: interface %s address delete %s",
230 ifc->ifp->name, buf);
231 }
232
233 if (if_is_operative (ifc->ifp))
234 bgp_connected_delete (ifc);
235
236 connected_free (ifc);
237
238 return 0;
239 }
240
241 /* Zebra route add and delete treatment. */
242 static int
zebra_read_ipv4(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)243 zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
244 vrf_id_t vrf_id)
245 {
246 struct stream *s;
247 struct zapi_ipv4 api;
248 struct in_addr nexthop;
249 struct prefix_ipv4 p;
250 unsigned char plength = 0;
251
252 s = zclient->ibuf;
253 nexthop.s_addr = 0;
254
255 /* Type, flags, message. */
256 api.type = stream_getc (s);
257 api.flags = stream_getc (s);
258 api.message = stream_getc (s);
259
260 /* IPv4 prefix. */
261 memset (&p, 0, sizeof (struct prefix_ipv4));
262 p.family = AF_INET;
263 plength = stream_getc (s);
264 p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength);
265 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
266
267 /* Nexthop, ifindex, distance, metric. */
268 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
269 {
270 api.nexthop_num = stream_getc (s);
271 nexthop.s_addr = stream_get_ipv4 (s);
272 }
273 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
274 {
275 api.ifindex_num = stream_getc (s);
276 stream_getl (s); /* ifindex, unused */
277 }
278 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
279 api.distance = stream_getc (s);
280 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
281 api.metric = stream_getl (s);
282 else
283 api.metric = 0;
284
285 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
286 api.tag = stream_getl (s);
287 else
288 api.tag = 0;
289
290 if (command == ZEBRA_IPV4_ROUTE_ADD)
291 {
292 if (BGP_DEBUG(zebra, ZEBRA))
293 {
294 char buf[2][INET_ADDRSTRLEN];
295 zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u tag %d",
296 zebra_route_string(api.type),
297 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
298 p.prefixlen,
299 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
300 api.metric,
301 api.tag);
302 }
303 bgp_redistribute_add ((struct prefix *)&p, &nexthop, NULL,
304 api.metric, api.type, api.tag);
305 }
306 else
307 {
308 if (BGP_DEBUG(zebra, ZEBRA))
309 {
310 char buf[2][INET_ADDRSTRLEN];
311 zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d "
312 "nexthop %s metric %u tag %d",
313 zebra_route_string(api.type),
314 inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
315 p.prefixlen,
316 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
317 api.metric,
318 api.tag);
319 }
320 bgp_redistribute_delete((struct prefix *)&p, api.type);
321 }
322
323 return 0;
324 }
325
326 /* Zebra route add and delete treatment. */
327 static int
zebra_read_ipv6(int command,struct zclient * zclient,zebra_size_t length,vrf_id_t vrf_id)328 zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
329 vrf_id_t vrf_id)
330 {
331 struct stream *s;
332 struct zapi_ipv6 api;
333 struct in6_addr nexthop;
334 struct prefix_ipv6 p;
335 unsigned char plength = 0;
336
337 s = zclient->ibuf;
338 memset (&nexthop, 0, sizeof (struct in6_addr));
339
340 /* Type, flags, message. */
341 api.type = stream_getc (s);
342 api.flags = stream_getc (s);
343 api.message = stream_getc (s);
344
345 /* IPv6 prefix. */
346 memset (&p, 0, sizeof (struct prefix_ipv6));
347 p.family = AF_INET6;
348 plength = stream_getc (s);
349 p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength);
350 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
351
352 /* Nexthop, ifindex, distance, metric. */
353 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
354 {
355 api.nexthop_num = stream_getc (s);
356 stream_get (&nexthop, s, 16);
357 }
358 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
359 {
360 api.ifindex_num = stream_getc (s);
361 stream_getl (s); /* ifindex, unused */
362 }
363 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
364 api.distance = stream_getc (s);
365 else
366 api.distance = 0;
367 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
368 api.metric = stream_getl (s);
369 else
370 api.metric = 0;
371
372 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
373 api.tag = stream_getl (s);
374 else
375 api.tag = 0;
376
377 /* Simply ignore link-local address. */
378 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
379 return 0;
380
381 if (command == ZEBRA_IPV6_ROUTE_ADD)
382 {
383 if (BGP_DEBUG(zebra, ZEBRA))
384 {
385 char buf[2][INET6_ADDRSTRLEN];
386 zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u tag %d",
387 zebra_route_string(api.type),
388 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
389 p.prefixlen,
390 inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
391 api.metric,
392 api.tag);
393 }
394 bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop,
395 api.metric, api.type, api.tag);
396 }
397 else
398 {
399 if (BGP_DEBUG(zebra, ZEBRA))
400 {
401 char buf[2][INET6_ADDRSTRLEN];
402 zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d "
403 "nexthop %s metric %u tag %d",
404 zebra_route_string(api.type),
405 inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
406 p.prefixlen,
407 inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
408 api.metric,
409 api.tag);
410 }
411 bgp_redistribute_delete ((struct prefix *) &p, api.type);
412 }
413
414 return 0;
415 }
416
417 struct interface *
if_lookup_by_ipv4(struct in_addr * addr)418 if_lookup_by_ipv4 (struct in_addr *addr)
419 {
420 struct listnode *ifnode;
421 struct listnode *cnode;
422 struct interface *ifp;
423 struct connected *connected;
424 struct prefix_ipv4 p;
425 struct prefix *cp;
426
427 p.family = AF_INET;
428 p.prefix = *addr;
429 p.prefixlen = IPV4_MAX_BITLEN;
430
431 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
432 {
433 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
434 {
435 cp = connected->address;
436
437 if (cp->family == AF_INET)
438 if (prefix_match (cp, (struct prefix *)&p))
439 return ifp;
440 }
441 }
442 return NULL;
443 }
444
445 struct interface *
if_lookup_by_ipv4_exact(struct in_addr * addr)446 if_lookup_by_ipv4_exact (struct in_addr *addr)
447 {
448 struct listnode *ifnode;
449 struct listnode *cnode;
450 struct interface *ifp;
451 struct connected *connected;
452 struct prefix *cp;
453
454 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
455 {
456 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
457 {
458 cp = connected->address;
459
460 if (cp->family == AF_INET)
461 if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
462 return ifp;
463 }
464 }
465 return NULL;
466 }
467
468 struct interface *
if_lookup_by_ipv6(struct in6_addr * addr)469 if_lookup_by_ipv6 (struct in6_addr *addr)
470 {
471 struct listnode *ifnode;
472 struct listnode *cnode;
473 struct interface *ifp;
474 struct connected *connected;
475 struct prefix_ipv6 p;
476 struct prefix *cp;
477
478 p.family = AF_INET6;
479 p.prefix = *addr;
480 p.prefixlen = IPV6_MAX_BITLEN;
481
482 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
483 {
484 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
485 {
486 cp = connected->address;
487
488 if (cp->family == AF_INET6)
489 if (prefix_match (cp, (struct prefix *)&p))
490 return ifp;
491 }
492 }
493 return NULL;
494 }
495
496 struct interface *
if_lookup_by_ipv6_exact(struct in6_addr * addr)497 if_lookup_by_ipv6_exact (struct in6_addr *addr)
498 {
499 struct listnode *ifnode;
500 struct listnode *cnode;
501 struct interface *ifp;
502 struct connected *connected;
503 struct prefix *cp;
504
505 for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
506 {
507 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
508 {
509 cp = connected->address;
510
511 if (cp->family == AF_INET6)
512 if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
513 return ifp;
514 }
515 }
516 return NULL;
517 }
518
519 static int
if_get_ipv6_global(struct interface * ifp,struct in6_addr * addr)520 if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
521 {
522 struct listnode *cnode;
523 struct connected *connected;
524 struct prefix *cp;
525
526 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
527 {
528 cp = connected->address;
529
530 if (cp->family == AF_INET6)
531 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
532 {
533 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
534 return 1;
535 }
536 }
537 return 0;
538 }
539
540 static int
if_get_ipv6_local(struct interface * ifp,struct in6_addr * addr)541 if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
542 {
543 struct listnode *cnode;
544 struct connected *connected;
545 struct prefix *cp;
546
547 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
548 {
549 cp = connected->address;
550
551 if (cp->family == AF_INET6)
552 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
553 {
554 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
555 return 1;
556 }
557 }
558 return 0;
559 }
560
561 static int
if_get_ipv4_address(struct interface * ifp,struct in_addr * addr)562 if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
563 {
564 struct listnode *cnode;
565 struct connected *connected;
566 struct prefix *cp;
567
568 for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
569 {
570 cp = connected->address;
571 if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4)))
572 {
573 *addr = cp->u.prefix4;
574 return 1;
575 }
576 }
577 return 0;
578 }
579
580 int
bgp_nexthop_set(union sockunion * local,union sockunion * remote,struct bgp_nexthop * nexthop,struct peer * peer)581 bgp_nexthop_set (union sockunion *local, union sockunion *remote,
582 struct bgp_nexthop *nexthop, struct peer *peer)
583 {
584 int ret = 0;
585 struct interface *ifp = NULL;
586
587 memset (nexthop, 0, sizeof (struct bgp_nexthop));
588
589 if (!local)
590 return -1;
591 if (!remote)
592 return -1;
593
594 if (local->sa.sa_family == AF_INET)
595 {
596 nexthop->v4 = local->sin.sin_addr;
597 if (peer->update_if)
598 ifp = if_lookup_by_name (peer->update_if);
599 else
600 ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr);
601 }
602 if (local->sa.sa_family == AF_INET6)
603 {
604 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
605 {
606 if (peer->ifname)
607 ifp = if_lookup_by_name (peer->ifname);
608 }
609 else if (peer->update_if)
610 ifp = if_lookup_by_name (peer->update_if);
611 else
612 ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr);
613 }
614
615 if (!ifp)
616 return -1;
617
618 nexthop->ifp = ifp;
619
620 /* IPv4 connection. */
621 if (local->sa.sa_family == AF_INET)
622 {
623 /* IPv6 nexthop*/
624 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
625
626 /* There is no global nexthop. */
627 if (!ret)
628 if_get_ipv6_local (ifp, &nexthop->v6_global);
629 else
630 if_get_ipv6_local (ifp, &nexthop->v6_local);
631 }
632
633 /* IPv6 connection. */
634 if (local->sa.sa_family == AF_INET6)
635 {
636 struct interface *direct = NULL;
637
638 /* IPv4 nexthop. */
639 ret = if_get_ipv4_address(ifp, &nexthop->v4);
640 if (!ret && peer->local_id.s_addr)
641 nexthop->v4 = peer->local_id;
642
643 /* Global address*/
644 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
645 {
646 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
647 IPV6_MAX_BYTELEN);
648
649 /* If directory connected set link-local address. */
650 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr);
651 if (direct)
652 if_get_ipv6_local (ifp, &nexthop->v6_local);
653 }
654 else
655 /* Link-local address. */
656 {
657 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
658
659 /* If there is no global address. Set link-local address as
660 global. I know this break RFC specification... */
661 if (!ret)
662 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
663 IPV6_MAX_BYTELEN);
664 else
665 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
666 IPV6_MAX_BYTELEN);
667 }
668 }
669
670 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
671 if_lookup_by_ipv6 (&remote->sin6.sin6_addr))
672 peer->shared_network = 1;
673 else
674 peer->shared_network = 0;
675
676 /* KAME stack specific treatment. */
677 #ifdef KAME
678 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
679 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
680 {
681 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
682 }
683 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
684 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
685 {
686 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
687 }
688 #endif /* KAME */
689 return ret;
690 }
691
692 void
bgp_zebra_announce(struct prefix * p,struct bgp_info * info,struct bgp * bgp,safi_t safi)693 bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi)
694 {
695 int flags;
696 u_char distance;
697 struct peer *peer;
698 struct bgp_info *mpinfo;
699 size_t oldsize, newsize;
700 u_int32_t nhcount;
701 route_tag_t tag = 0;
702
703 if (zclient->sock < 0)
704 return;
705
706 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
707 return;
708
709 flags = 0;
710 peer = info->peer;
711
712 if ((info->attr->extra) && (info->attr->extra->tag != 0))
713 tag = info->attr->extra->tag;
714
715 if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED)
716 {
717 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
718 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
719 }
720
721 if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
722 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
723 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
724
725 nhcount = 1 + bgp_info_mpath_count (info);
726
727 if (p->family == AF_INET)
728 {
729 struct zapi_ipv4 api;
730 struct in_addr *nexthop;
731
732 /* resize nexthop buffer size if necessary */
733 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
734 (sizeof (struct in_addr *) * nhcount))
735 {
736 newsize = (sizeof (struct in_addr *) * nhcount);
737 newsize = stream_resize (bgp_nexthop_buf, newsize);
738 if (newsize == oldsize)
739 {
740 zlog_err ("can't resize nexthop buffer");
741 return;
742 }
743 }
744 stream_reset (bgp_nexthop_buf);
745
746 api.vrf_id = VRF_DEFAULT;
747 api.flags = flags;
748 nexthop = &info->attr->nexthop;
749 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
750 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
751 mpinfo = bgp_info_mpath_next (mpinfo))
752 {
753 nexthop = &mpinfo->attr->nexthop;
754 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *));
755 }
756
757 api.type = ZEBRA_ROUTE_BGP;
758 api.message = 0;
759 api.safi = safi;
760 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
761 api.nexthop_num = nhcount;
762 api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf);
763 api.ifindex_num = 0;
764 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
765 api.metric = info->attr->med;
766
767 if (tag)
768 {
769 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
770 api.tag = tag;
771 }
772
773 distance = bgp_distance_apply (p, info, bgp);
774
775 if (distance)
776 {
777 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
778 api.distance = distance;
779 }
780
781 if (BGP_DEBUG(zebra, ZEBRA))
782 {
783 int i;
784 char buf[2][INET_ADDRSTRLEN];
785 zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u"
786 " tag %u count %d",
787 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
788 p->prefixlen,
789 inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])),
790 api.metric, api.tag, api.nexthop_num);
791 for (i = 1; i < api.nexthop_num; i++)
792 zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s",
793 i, inet_ntop(AF_INET, api.nexthop[i], buf[1],
794 sizeof(buf[1])));
795 }
796
797 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient,
798 (struct prefix_ipv4 *) p, &api);
799 }
800
801 /* We have to think about a IPv6 link-local address curse. */
802 if (p->family == AF_INET6)
803 {
804 ifindex_t ifindex;
805 struct in6_addr *nexthop;
806 struct zapi_ipv6 api;
807 int valid_nh_count = 0;
808
809 /* resize nexthop buffer size if necessary */
810 if ((oldsize = stream_get_size (bgp_nexthop_buf)) <
811 (sizeof (struct in6_addr *) * nhcount))
812 {
813 newsize = (sizeof (struct in6_addr *) * nhcount);
814 newsize = stream_resize (bgp_nexthop_buf, newsize);
815 if (newsize == oldsize)
816 {
817 zlog_err ("can't resize nexthop buffer");
818 return;
819 }
820 }
821 stream_reset (bgp_nexthop_buf);
822
823 /* resize ifindices buffer size if necessary */
824 if ((oldsize = stream_get_size (bgp_ifindices_buf)) <
825 (sizeof (unsigned int) * nhcount))
826 {
827 newsize = (sizeof (unsigned int) * nhcount);
828 newsize = stream_resize (bgp_ifindices_buf, newsize);
829 if (newsize == oldsize)
830 {
831 zlog_err ("can't resize nexthop buffer");
832 return;
833 }
834 }
835 stream_reset (bgp_ifindices_buf);
836
837 ifindex = 0;
838 nexthop = NULL;
839
840 assert (info->attr->extra);
841
842 /* Only global address nexthop exists. */
843 if (info->attr->extra->mp_nexthop_len == 16)
844 nexthop = &info->attr->extra->mp_nexthop_global;
845
846 /* If both global and link-local address present. */
847 if (info->attr->extra->mp_nexthop_len == 32)
848 {
849 /* Workaround for Cisco's nexthop bug. */
850 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
851 && peer->su_remote->sa.sa_family == AF_INET6)
852 nexthop = &peer->su_remote->sin6.sin6_addr;
853 else
854 nexthop = &info->attr->extra->mp_nexthop_local;
855
856 if (info->peer->nexthop.ifp)
857 ifindex = info->peer->nexthop.ifp->ifindex;
858 }
859
860 if (nexthop == NULL)
861 return;
862
863 if (!ifindex)
864 {
865 if (info->peer->ifname)
866 ifindex = ifname2ifindex (info->peer->ifname);
867 else if (info->peer->nexthop.ifp)
868 ifindex = info->peer->nexthop.ifp->ifindex;
869 }
870 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
871 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
872 valid_nh_count++;
873
874 for (mpinfo = bgp_info_mpath_first (info); mpinfo;
875 mpinfo = bgp_info_mpath_next (mpinfo))
876 {
877 ifindex = 0;
878
879 /* Only global address nexthop exists. */
880 if (mpinfo->attr->extra->mp_nexthop_len == 16)
881 nexthop = &mpinfo->attr->extra->mp_nexthop_global;
882
883 /* If both global and link-local address present. */
884 if (mpinfo->attr->extra->mp_nexthop_len == 32)
885 {
886 /* Workaround for Cisco's nexthop bug. */
887 if (IN6_IS_ADDR_UNSPECIFIED (&mpinfo->attr->extra->mp_nexthop_global)
888 && mpinfo->peer->su_remote->sa.sa_family == AF_INET6)
889 {
890 nexthop = &mpinfo->peer->su_remote->sin6.sin6_addr;
891 }
892 else
893 {
894 nexthop = &mpinfo->attr->extra->mp_nexthop_local;
895 }
896
897 if (mpinfo->peer->nexthop.ifp)
898 {
899 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
900 }
901 }
902
903 if (nexthop == NULL)
904 {
905 continue;
906 }
907
908 if (!ifindex)
909 {
910 if (mpinfo->peer->ifname)
911 {
912 ifindex = if_nametoindex (mpinfo->peer->ifname);
913 }
914 else if (mpinfo->peer->nexthop.ifp)
915 {
916 ifindex = mpinfo->peer->nexthop.ifp->ifindex;
917 }
918 }
919
920 if (ifindex == 0)
921 {
922 continue;
923 }
924
925 stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *));
926 stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int));
927 valid_nh_count++;
928 }
929
930 /* Make Zebra API structure. */
931 api.vrf_id = VRF_DEFAULT;
932 api.flags = flags;
933 api.type = ZEBRA_ROUTE_BGP;
934 api.message = 0;
935 api.safi = safi;
936 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
937 api.nexthop_num = valid_nh_count;
938 api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf);
939 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
940 api.ifindex_num = valid_nh_count;
941 api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf);
942 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
943 api.metric = info->attr->med;
944
945 distance = ipv6_bgp_distance_apply (p, info, bgp);
946
947 if (distance)
948 {
949 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
950 api.distance = distance;
951 }
952
953 if (tag)
954 {
955 SET_FLAG (api.message, ZAPI_MESSAGE_TAG);
956 api.tag = tag;
957 }
958
959 if (BGP_DEBUG(zebra, ZEBRA))
960 {
961 char buf[2][INET6_ADDRSTRLEN];
962 zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u"
963 " tag %u",
964 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
965 p->prefixlen,
966 inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])),
967 api.metric, api.tag);
968 }
969
970 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient,
971 (struct prefix_ipv6 *) p, &api);
972 }
973 }
974
975 void
bgp_zebra_withdraw(struct prefix * p,struct bgp_info * info,safi_t safi)976 bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi)
977 {
978 int flags;
979 struct peer *peer;
980
981 if (zclient->sock < 0)
982 return;
983
984 if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT))
985 return;
986
987 peer = info->peer;
988 flags = 0;
989
990 if (peer->sort == BGP_PEER_IBGP)
991 {
992 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
993 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
994 }
995
996 if ((peer->sort == BGP_PEER_EBGP && peer_ttl (peer) != 1)
997 || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
998 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
999
1000 if (p->family == AF_INET)
1001 {
1002 struct zapi_ipv4 api;
1003
1004 api.vrf_id = VRF_DEFAULT;
1005 api.flags = flags;
1006
1007 api.type = ZEBRA_ROUTE_BGP;
1008 api.message = 0;
1009 api.safi = safi;
1010 api.nexthop_num = 0;
1011 api.ifindex_num = 0;
1012 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1013 api.metric = info->attr->med;
1014
1015 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1016 {
1017 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1018 api.tag = info->attr->extra->tag;
1019 }
1020
1021 if (BGP_DEBUG(zebra, ZEBRA))
1022 {
1023 char buf[2][INET_ADDRSTRLEN];
1024 zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u tag %d",
1025 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
1026 p->prefixlen,
1027 api.metric,
1028 api.tag);
1029 }
1030
1031 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient,
1032 (struct prefix_ipv4 *) p, &api);
1033 }
1034
1035 /* We have to think about a IPv6 link-local address curse. */
1036 if (p->family == AF_INET6)
1037 {
1038 struct zapi_ipv6 api;
1039
1040 api.vrf_id = VRF_DEFAULT;
1041 api.flags = flags;
1042 api.type = ZEBRA_ROUTE_BGP;
1043 api.message = 0;
1044 api.safi = safi;
1045 api.nexthop_num = 0;
1046 api.ifindex_num = 0;
1047 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1048 api.metric = info->attr->med;
1049
1050 if ((info->attr->extra) && (info->attr->extra->tag != 0))
1051 {
1052 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
1053 api.tag = info->attr->extra->tag;
1054 }
1055
1056 if (BGP_DEBUG(zebra, ZEBRA))
1057 {
1058 char buf[2][INET6_ADDRSTRLEN];
1059 zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u tag %d",
1060 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
1061 p->prefixlen,
1062 api.metric,
1063 api.tag);
1064 }
1065
1066 zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient,
1067 (struct prefix_ipv6 *) p, &api);
1068 }
1069 }
1070
1071 /* Other routes redistribution into BGP. */
1072 int
bgp_redistribute_set(struct bgp * bgp,afi_t afi,int type)1073 bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type)
1074 {
1075 /* Set flag to BGP instance. */
1076 bgp->redist[afi][type] = 1;
1077
1078 /* Return if already redistribute flag is set. */
1079 if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
1080 return CMD_WARNING;
1081
1082 vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT);
1083
1084 /* Return if zebra connection is not established. */
1085 if (zclient->sock < 0)
1086 return CMD_WARNING;
1087
1088 if (BGP_DEBUG(zebra, ZEBRA))
1089 zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type));
1090
1091 /* Send distribute add message to zebra. */
1092 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT);
1093
1094 return CMD_SUCCESS;
1095 }
1096
1097 /* Redistribute with route-map specification. */
1098 int
bgp_redistribute_rmap_set(struct bgp * bgp,afi_t afi,int type,const char * name)1099 bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type,
1100 const char *name)
1101 {
1102 if (bgp->rmap[afi][type].name
1103 && (strcmp (bgp->rmap[afi][type].name, name) == 0))
1104 return 0;
1105
1106 if (bgp->rmap[afi][type].name)
1107 free (bgp->rmap[afi][type].name);
1108 bgp->rmap[afi][type].name = strdup (name);
1109 bgp->rmap[afi][type].map = route_map_lookup_by_name (name);
1110
1111 return 1;
1112 }
1113
1114 /* Redistribute with metric specification. */
1115 int
bgp_redistribute_metric_set(struct bgp * bgp,afi_t afi,int type,u_int32_t metric)1116 bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type,
1117 u_int32_t metric)
1118 {
1119 if (bgp->redist_metric_flag[afi][type]
1120 && bgp->redist_metric[afi][type] == metric)
1121 return 0;
1122
1123 bgp->redist_metric_flag[afi][type] = 1;
1124 bgp->redist_metric[afi][type] = metric;
1125
1126 return 1;
1127 }
1128
1129 /* Unset redistribution. */
1130 int
bgp_redistribute_unset(struct bgp * bgp,afi_t afi,int type)1131 bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type)
1132 {
1133 /* Unset flag from BGP instance. */
1134 bgp->redist[afi][type] = 0;
1135
1136 /* Unset route-map. */
1137 if (bgp->rmap[afi][type].name)
1138 free (bgp->rmap[afi][type].name);
1139 bgp->rmap[afi][type].name = NULL;
1140 bgp->rmap[afi][type].map = NULL;
1141
1142 /* Unset metric. */
1143 bgp->redist_metric_flag[afi][type] = 0;
1144 bgp->redist_metric[afi][type] = 0;
1145
1146 /* Return if zebra connection is disabled. */
1147 if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT))
1148 return CMD_WARNING;
1149 vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT);
1150
1151 if (bgp->redist[AFI_IP][type] == 0
1152 && bgp->redist[AFI_IP6][type] == 0
1153 && zclient->sock >= 0)
1154 {
1155 /* Send distribute delete message to zebra. */
1156 if (BGP_DEBUG(zebra, ZEBRA))
1157 zlog_debug("Zebra send: redistribute delete %s",
1158 zebra_route_string(type));
1159 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type,
1160 VRF_DEFAULT);
1161 }
1162
1163 /* Withdraw redistributed routes from current BGP's routing table. */
1164 bgp_redistribute_withdraw (bgp, afi, type);
1165
1166 return CMD_SUCCESS;
1167 }
1168
1169 void
bgp_zclient_reset(void)1170 bgp_zclient_reset (void)
1171 {
1172 zclient_reset (zclient);
1173 }
1174
1175 static void
bgp_zebra_connected(struct zclient * zclient)1176 bgp_zebra_connected (struct zclient *zclient)
1177 {
1178 zclient_num_connects++;
1179 zclient_send_requests (zclient, VRF_DEFAULT);
1180 }
1181
1182 void
bgp_zebra_init(struct thread_master * master)1183 bgp_zebra_init (struct thread_master *master)
1184 {
1185 zclient_num_connects = 0;
1186
1187 /* Set default values. */
1188 zclient = zclient_new (master);
1189 zclient_init (zclient, ZEBRA_ROUTE_BGP);
1190 zclient->zebra_connected = bgp_zebra_connected;
1191 zclient->router_id_update = bgp_router_id_update;
1192 zclient->interface_add = bgp_interface_add;
1193 zclient->interface_delete = bgp_interface_delete;
1194 zclient->interface_address_add = bgp_interface_address_add;
1195 zclient->interface_address_delete = bgp_interface_address_delete;
1196 zclient->ipv4_route_add = zebra_read_ipv4;
1197 zclient->ipv4_route_delete = zebra_read_ipv4;
1198 zclient->interface_up = bgp_interface_up;
1199 zclient->interface_down = bgp_interface_down;
1200 zclient->ipv6_route_add = zebra_read_ipv6;
1201 zclient->ipv6_route_delete = zebra_read_ipv6;
1202 zclient->nexthop_update = bgp_read_nexthop_update;
1203
1204 bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
1205 bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE);
1206 }
1207
1208 void
bgp_zebra_destroy(void)1209 bgp_zebra_destroy(void)
1210 {
1211 if (zclient == NULL)
1212 return;
1213 zclient_stop(zclient);
1214 zclient_free(zclient);
1215 zclient = NULL;
1216 }
1217
1218 int
bgp_zebra_num_connects(void)1219 bgp_zebra_num_connects(void)
1220 {
1221 return zclient_num_connects;
1222 }
1223