Tomato 1.28
[tomato.git] / release / src / router / zebra / bgpd / bgp_zebra.c
blobe5b9ada30cdee03b85066c3d860837bdab9f7efd
1 /* zebra client
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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.
22 #include <zebra.h>
24 #include "command.h"
25 #include "stream.h"
26 #include "network.h"
27 #include "prefix.h"
28 #include "log.h"
29 #include "sockunion.h"
30 #include "zclient.h"
31 #include "routemap.h"
33 #include "bgpd/bgpd.h"
34 #include "bgpd/bgp_route.h"
35 #include "bgpd/bgp_attr.h"
36 #include "bgpd/bgp_nexthop.h"
38 int bgp_interface_add (int, struct zclient *, zebra_size_t);
39 int bgp_interface_delete (int, struct zclient *, zebra_size_t);
40 int bgp_interface_address_add (int, struct zclient *, zebra_size_t);
41 int bgp_interface_address_delete (int, struct zclient *, zebra_size_t);
43 /* All information about zebra. */
44 static struct zclient *zclient = NULL;
46 /* Update default router id. */
47 int
48 bgp_if_update (struct interface *ifp)
50 struct bgp *bgp;
51 listnode cn;
52 struct listnode *nn;
53 struct listnode *nm;
54 struct peer_conf *conf;
56 for (cn = listhead (ifp->connected); cn; nextnode (cn))
58 struct connected *co;
59 struct in_addr addr;
61 co = getdata (cn);
63 if (co->address->family == AF_INET)
65 addr = co->address->u.prefix4;
67 /* Ignore NET127. */
68 if (IPV4_NET127 (ntohl (addr.s_addr)))
69 continue;
71 LIST_LOOP (bgp_list, bgp, nn)
73 /* Respect configured router id */
74 if (! (bgp->config & BGP_CONFIG_ROUTER_ID))
75 if (ntohl (bgp->id.s_addr) < ntohl (addr.s_addr))
77 bgp->id = addr;
78 LIST_LOOP (bgp->peer_conf, conf, nm)
80 conf->peer->local_id = addr;
86 return 0;
89 int
90 bgp_if_update_all ()
92 listnode node;
93 struct interface *ifp;
95 for (node = listhead (iflist); node; node = nextnode (node))
97 ifp = getdata (node);
98 bgp_if_update (ifp);
100 return 0;
103 /* Inteface addition message from zebra. */
105 bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length)
107 struct interface *ifp;
109 ifp = zebra_interface_add_read (zclient->ibuf);
111 #if 0
112 if (IS_BGP_DEBUG_ZEBRA)
113 zlog_info ("BGP interface add %s index %d flags %d metric %d mtu %d",
114 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
115 #endif /* 0 */
117 bgp_if_update (ifp);
119 return 0;
123 bgp_interface_delete (int command, struct zclient *zclient,
124 zebra_size_t length)
126 struct stream *s;
127 struct interface *ifp;
129 s = zclient->ibuf;
130 ifp = zebra_interface_state_read (s);
132 return 0;
136 bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length)
138 struct stream *s;
139 struct interface *ifp;
140 struct connected *c;
141 listnode node;
143 s = zclient->ibuf;
144 ifp = zebra_interface_state_read (s);
146 if (! ifp)
147 return 0;
149 for (node = listhead (ifp->connected); node; nextnode (node))
151 c = getdata (node);
152 bgp_connected_add (c);
155 return 0;
159 bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length)
161 struct stream *s;
162 struct interface *ifp;
163 struct connected *c;
164 listnode node;
166 s = zclient->ibuf;
167 ifp = zebra_interface_state_read (s);
168 if (! ifp)
169 return 0;
171 for (node = listhead (ifp->connected); node; nextnode (node))
173 c = getdata (node);
174 bgp_connected_delete (c);
177 return 0;
181 bgp_interface_address_add (int command, struct zclient *zclient,
182 zebra_size_t length)
184 struct connected *ifc;
186 ifc = zebra_interface_address_add_read (zclient->ibuf);
188 if (ifc == NULL)
189 return 0;
191 #if 0
192 if (IS_BGP_DEBUG_ZEBRA)
194 struct prefix *p;
195 char buf[INET6_ADDRSTRLEN];
197 p = c->address;
198 if (p->family == AF_INET6)
199 zlog_info ("BGP connected address %s/%d",
200 inet_ntop (AF_INET6, &p->u.prefix6, buf, INET6_ADDRSTRLEN),
201 p->prefixlen);
203 #endif /* 0 */
205 bgp_if_update (ifc->ifp);
207 if (if_is_up (ifc->ifp))
208 bgp_connected_add (ifc);
210 return 0;
214 bgp_interface_address_delete (int command, struct zclient *zclient,
215 zebra_size_t length)
217 struct connected *ifc;
219 ifc = zebra_interface_address_delete_read (zclient->ibuf);
221 if (ifc == NULL)
222 return 0;
224 bgp_if_update (ifc->ifp);
226 if (if_is_up (ifc->ifp))
227 bgp_connected_delete (ifc);
229 connected_free (ifc);
231 return 0;
234 /* Zebra route add and delete treatment. */
236 zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length)
238 struct stream *s;
239 struct zapi_ipv4 api;
240 unsigned long ifindex;
241 struct in_addr nexthop;
242 struct prefix_ipv4 p;
244 s = zclient->ibuf;
245 ifindex = 0;
246 nexthop.s_addr = 0;
248 /* Type, flags, message. */
249 api.type = stream_getc (s);
250 api.flags = stream_getc (s);
251 api.message = stream_getc (s);
253 /* IPv4 prefix. */
254 memset (&p, 0, sizeof (struct prefix_ipv4));
255 p.family = AF_INET;
256 p.prefixlen = stream_getc (s);
257 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
259 /* Nexthop, ifindex, distance, metric. */
260 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
262 api.nexthop_num = stream_getc (s);
263 nexthop.s_addr = stream_get_ipv4 (s);
265 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
267 api.ifindex_num = stream_getc (s);
268 ifindex = stream_getl (s);
270 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
271 api.distance = stream_getc (s);
272 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
273 api.metric = stream_getl (s);
274 else
275 api.metric = 0;
277 if (command == ZEBRA_IPV4_ROUTE_ADD)
278 bgp_redistribute_add ((struct prefix *)&p, &nexthop, api.metric, api.type);
279 else
280 bgp_redistribute_delete ((struct prefix *)&p, api.type);
282 return 0;
285 #ifdef HAVE_IPV6
286 /* Zebra route add and delete treatment. */
288 zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length)
290 struct stream *s;
291 struct zapi_ipv6 api;
292 unsigned long ifindex;
293 struct in6_addr nexthop;
294 struct prefix_ipv6 p;
296 s = zclient->ibuf;
297 ifindex = 0;
298 memset (&nexthop, 0, sizeof (struct in6_addr));
300 /* Type, flags, message. */
301 api.type = stream_getc (s);
302 api.flags = stream_getc (s);
303 api.message = stream_getc (s);
305 /* IPv6 prefix. */
306 memset (&p, 0, sizeof (struct prefix_ipv6));
307 p.family = AF_INET6;
308 p.prefixlen = stream_getc (s);
309 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
311 /* Nexthop, ifindex, distance, metric. */
312 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
314 api.nexthop_num = stream_getc (s);
315 stream_get (&nexthop, s, 16);
317 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
319 api.ifindex_num = stream_getc (s);
320 ifindex = stream_getl (s);
322 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
323 api.distance = stream_getc (s);
324 else
325 api.distance = 0;
326 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
327 api.metric = stream_getl (s);
328 else
329 api.metric = 0;
331 /* Simply ignore link-local address. */
332 if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
333 return 0;
335 if (command == ZEBRA_IPV6_ROUTE_ADD)
336 bgp_redistribute_add ((struct prefix *)&p, NULL, api.metric, api.type);
337 else
338 bgp_redistribute_delete ((struct prefix *) &p, api.type);
340 return 0;
342 #endif /* HAVE_IPV6 */
344 /* Other routes redistribution into BGP. */
345 void
346 bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type)
348 /* Set flag to BGP instance. */
349 bgp->redist[afi][type] = 1;
351 /* Return if already redistribute flag is set. */
352 if (zclient->redist[type])
353 return;
355 zclient->redist[type] = 1;
357 /* Return if zebra connection is not established. */
358 if (zclient->sock < 0)
359 return;
361 /* Send distribute add message to zebra. */
362 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, type);
365 /* Redistribute with route-map specification. */
366 void
367 bgp_redistribute_routemap_set (struct bgp *bgp, afi_t afi, int type,
368 char *name)
370 bgp_redistribute_set (bgp, afi, type);
372 if (bgp->rmap[afi][type].name)
373 free (bgp->rmap[afi][type].name);
375 bgp->rmap[afi][type].name = strdup (name);
376 bgp->rmap[afi][type].map = route_map_lookup_by_name (name);
379 /* Unset redistribution. */
380 void
381 bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type)
383 /* Unset flag from BGP instance. */
384 bgp->redist[afi][type] = 0;
386 /* Unset route-map. */
387 if (bgp->rmap[afi][type].name)
388 free (bgp->rmap[afi][type].name);
389 bgp->rmap[afi][type].name = NULL;
390 bgp->rmap[afi][type].map = NULL;
392 /* Return if zebra connection is disabled. */
393 if (! zclient->redist[type])
394 return;
395 zclient->redist[type] = 0;
397 if (bgp->redist[AFI_IP][type] == 0
398 && bgp->redist[AFI_IP6][type] == 0
399 && zclient->sock >= 0)
400 /* Send distribute delete message to zebra. */
401 zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient->sock, type);
403 /* Withdraw redistributed routes from current BGP's routing table. */
404 bgp_redistribute_withdraw (bgp, afi, type);
407 DEFUN (bgp_redistribute_kernel,
408 bgp_redistribute_kernel_cmd,
409 "redistribute kernel",
410 "Redistribute information from another routing protocol\n"
411 "Kernel routes\n")
413 bgp_redistribute_set (vty->index, AFI_IP, ZEBRA_ROUTE_KERNEL);
414 return CMD_SUCCESS;
417 DEFUN (bgp_redistribute_kernel_routemap,
418 bgp_redistribute_kernel_routemap_cmd,
419 "redistribute kernel route-map WORD",
420 "Redistribute information from another routing protocol\n"
421 "Kernel routes\n"
422 "Route map reference\n"
423 "Pointer to route-map entries\n")
425 bgp_redistribute_routemap_set (vty->index, AFI_IP, ZEBRA_ROUTE_KERNEL, argv[0]);
426 return CMD_SUCCESS;
429 DEFUN (no_bgp_redistribute_kernel,
430 no_bgp_redistribute_kernel_cmd,
431 "no redistribute kernel",
432 NO_STR
433 "Redistribute information from another routing protocol\n"
434 "Kernel routes\n")
436 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_KERNEL);
437 return CMD_SUCCESS;
440 DEFUN (no_bgp_redistribute_kernel_routemap,
441 no_bgp_redistribute_kernel_routemap_cmd,
442 "no redistribute kernel route-map WORD",
443 NO_STR
444 "Redistribute information from another routing protocol\n"
445 "Kernel routes\n"
446 "Route map reference\n"
447 "Pointer to route-map entries\n")
449 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_KERNEL);
450 return CMD_SUCCESS;
453 DEFUN (bgp_redistribute_static,
454 bgp_redistribute_static_cmd,
455 "redistribute static",
456 "Redistribute information from another routing protocol\n"
457 "Static routes\n")
459 bgp_redistribute_set (vty->index, AFI_IP, ZEBRA_ROUTE_STATIC);
460 return CMD_SUCCESS;
463 DEFUN (bgp_redistribute_static_routemap,
464 bgp_redistribute_static_routemap_cmd,
465 "redistribute static route-map WORD",
466 "Redistribute information from another routing protocol\n"
467 "Static routes\n"
468 "Route map reference\n"
469 "Pointer to route-map entries\n")
471 bgp_redistribute_routemap_set (vty->index, AFI_IP, ZEBRA_ROUTE_STATIC, argv[0]);
472 return CMD_SUCCESS;
475 DEFUN (no_bgp_redistribute_static,
476 no_bgp_redistribute_static_cmd,
477 "no redistribute static",
478 NO_STR
479 "Redistribute information from another routing protocol\n"
480 "Static routes\n")
482 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_STATIC);
483 return CMD_SUCCESS;
486 DEFUN (no_bgp_redistribute_static_routemap,
487 no_bgp_redistribute_static_routemap_cmd,
488 "no redistribute static route-map WORD",
489 NO_STR
490 "Redistribute information from another routing protocol\n"
491 "Static routes\n"
492 "Route map reference\n"
493 "Pointer to route-map entries\n")
495 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_STATIC);
496 return CMD_SUCCESS;
499 DEFUN (bgp_redistribute_connected,
500 bgp_redistribute_connected_cmd,
501 "redistribute connected",
502 "Redistribute information from another routing protocol\n"
503 "Connected\n")
505 bgp_redistribute_set (vty->index, AFI_IP, ZEBRA_ROUTE_CONNECT);
506 return CMD_SUCCESS;
509 DEFUN (bgp_redistribute_connected_routemap,
510 bgp_redistribute_connected_routemap_cmd,
511 "redistribute connected route-map WORD",
512 "Redistribute information from another routing protocol\n"
513 "Connected\n"
514 "Route map reference\n"
515 "Pointer to route-map entries\n")
517 bgp_redistribute_routemap_set (vty->index, AFI_IP, ZEBRA_ROUTE_CONNECT, argv[0]);
518 return CMD_SUCCESS;
521 DEFUN (no_bgp_redistribute_connected,
522 no_bgp_redistribute_connected_cmd,
523 "no redistribute connected",
524 NO_STR
525 "Redistribute information from another routing protocol\n"
526 "Connected\n")
528 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_CONNECT);
529 return CMD_SUCCESS;
532 DEFUN (no_bgp_redistribute_connected_routemap,
533 no_bgp_redistribute_connected_routemap_cmd,
534 "no redistribute connected route-map WORD",
535 NO_STR
536 "Redistribute information from another routing protocol\n"
537 "Connected\n"
538 "Route map reference\n"
539 "Pointer to route-map entries\n")
541 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_CONNECT);
542 return CMD_SUCCESS;
545 DEFUN (bgp_redistribute_rip,
546 bgp_redistribute_rip_cmd,
547 "redistribute rip",
548 "Redistribute information from another routing protocol\n"
549 "Routing Information Protocol (RIP)\n")
551 bgp_redistribute_set (vty->index, AFI_IP, ZEBRA_ROUTE_RIP);
552 return CMD_SUCCESS;
555 DEFUN (bgp_redistribute_rip_routemap,
556 bgp_redistribute_rip_routemap_cmd,
557 "redistribute rip route-map WORD",
558 "Redistribute information from another routing protocol\n"
559 "Routing Information Protocol (RIP)\n"
560 "Route map reference\n"
561 "Pointer to route-map entries\n")
563 bgp_redistribute_routemap_set (vty->index, AFI_IP, ZEBRA_ROUTE_RIP, argv[0]);
564 return CMD_SUCCESS;
567 DEFUN (no_bgp_redistribute_rip,
568 no_bgp_redistribute_rip_cmd,
569 "no redistribute rip",
570 NO_STR
571 "Redistribute information from another routing protocol\n"
572 "Routing Information Protocol (RIP)\n")
574 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_RIP);
575 return CMD_SUCCESS;
578 DEFUN (no_bgp_redistribute_rip_routemap,
579 no_bgp_redistribute_rip_routemap_cmd,
580 "no redistribute rip route-map WORD",
581 NO_STR
582 "Redistribute information from another routing protocol\n"
583 "Routing Information Protocol (RIP)\n"
584 "Route map reference\n"
585 "Pointer to route-map entries\n")
587 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_RIP);
588 return CMD_SUCCESS;
591 DEFUN (bgp_redistribute_ospf,
592 bgp_redistribute_ospf_cmd,
593 "redistribute ospf",
594 "Redistribute information from another routing protocol\n"
595 "Open Shortest Path First (OSPF)\n")
597 bgp_redistribute_set (vty->index, AFI_IP, ZEBRA_ROUTE_OSPF);
598 return CMD_SUCCESS;
601 DEFUN (bgp_redistribute_ospf_routemap,
602 bgp_redistribute_ospf_routemap_cmd,
603 "redistribute ospf route-map WORD",
604 "Redistribute information from another routing protocol\n"
605 "Open Shortest Path First (OSPF)\n"
606 "Route map reference\n"
607 "Pointer to route-map entries\n")
609 bgp_redistribute_routemap_set (vty->index, AFI_IP, ZEBRA_ROUTE_OSPF, argv[0]);
610 return CMD_SUCCESS;
613 DEFUN (no_bgp_redistribute_ospf,
614 no_bgp_redistribute_ospf_cmd,
615 "no redistribute ospf",
616 NO_STR
617 "Redistribute information from another routing protocol\n"
618 "Open Shortest Path First (OSPF)\n")
620 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_OSPF);
621 return CMD_SUCCESS;
624 DEFUN (no_bgp_redistribute_ospf_routemap,
625 no_bgp_redistribute_ospf_routemap_cmd,
626 "no redistribute ospf route-map WORD",
627 NO_STR
628 "Redistribute information from another routing protocol\n"
629 "Open Shortest Path First (OSPF)\n"
630 "Route map reference\n"
631 "Pointer to route-map entries\n")
633 bgp_redistribute_unset (vty->index, AFI_IP, ZEBRA_ROUTE_OSPF);
634 return CMD_SUCCESS;
637 #ifdef HAVE_IPV6
638 DEFUN (ipv6_bgp_redistribute_kernel,
639 ipv6_bgp_redistribute_kernel_cmd,
640 "redistribute kernel",
641 "Redistribute information from another routing protocol\n"
642 "Kernel routes\n")
644 bgp_redistribute_set (vty->index, AFI_IP6, ZEBRA_ROUTE_KERNEL);
645 return CMD_SUCCESS;
648 DEFUN (ipv6_bgp_redistribute_kernel_routemap,
649 ipv6_bgp_redistribute_kernel_routemap_cmd,
650 "redistribute kernel route-map WORD",
651 "Redistribute information from another routing protocol\n"
652 "Kernel routes\n"
653 "Route map reference\n"
654 "Pointer to route-map entries\n")
656 bgp_redistribute_routemap_set (vty->index, AFI_IP6, ZEBRA_ROUTE_KERNEL, argv[0]);
657 return CMD_SUCCESS;
660 DEFUN (no_ipv6_bgp_redistribute_kernel,
661 no_ipv6_bgp_redistribute_kernel_cmd,
662 "no redistribute kernel",
663 NO_STR
664 "Redistribute information from another routing protocol\n"
665 "Kernel routes\n")
667 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_KERNEL);
668 return CMD_SUCCESS;
671 DEFUN (no_ipv6_bgp_redistribute_kernel_routemap,
672 no_ipv6_bgp_redistribute_kernel_routemap_cmd,
673 "no redistribute kernel route-map WORD",
674 NO_STR
675 "Redistribute information from another routing protocol\n"
676 "Kernel routes\n"
677 "Route map reference\n"
678 "Pointer to route-map entries\n")
680 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_KERNEL);
681 return CMD_SUCCESS;
684 DEFUN (ipv6_bgp_redistribute_static,
685 ipv6_bgp_redistribute_static_cmd,
686 "redistribute static",
687 "Redistribute information from another routing protocol\n"
688 "Static routes\n")
690 bgp_redistribute_set (vty->index, AFI_IP6, ZEBRA_ROUTE_STATIC);
691 return CMD_SUCCESS;
694 DEFUN (ipv6_bgp_redistribute_static_routemap,
695 ipv6_bgp_redistribute_static_routemap_cmd,
696 "redistribute static route-map WORD",
697 "Redistribute information from another routing protocol\n"
698 "Static routes\n"
699 "Route map reference\n"
700 "Pointer to route-map entries\n")
702 bgp_redistribute_routemap_set (vty->index, AFI_IP6, ZEBRA_ROUTE_STATIC, argv[0]);
703 return CMD_SUCCESS;
706 DEFUN (no_ipv6_bgp_redistribute_static,
707 no_ipv6_bgp_redistribute_static_cmd,
708 "no redistribute static",
709 NO_STR
710 "Redistribute information from another routing protocol\n"
711 "Static routes\n")
713 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_STATIC);
714 return CMD_SUCCESS;
717 DEFUN (no_ipv6_bgp_redistribute_static_routemap,
718 no_ipv6_bgp_redistribute_static_routemap_cmd,
719 "no redistribute static route-map WORD",
720 NO_STR
721 "Redistribute information from another routing protocol\n"
722 "Static routes\n"
723 "Route map reference\n"
724 "Pointer to route-map entries\n")
726 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_STATIC);
727 return CMD_SUCCESS;
730 DEFUN (ipv6_bgp_redistribute_connected,
731 ipv6_bgp_redistribute_connected_cmd,
732 "redistribute connected",
733 "Redistribute information from another routing protocol\n"
734 "Connected\n")
736 bgp_redistribute_set (vty->index, AFI_IP6, ZEBRA_ROUTE_CONNECT);
737 return CMD_SUCCESS;
740 DEFUN (ipv6_bgp_redistribute_connected_routemap,
741 ipv6_bgp_redistribute_connected_routemap_cmd,
742 "redistribute connected route-map WORD",
743 "Redistribute information from another routing protocol\n"
744 "Connected\n"
745 "Route map reference\n"
746 "Pointer to route-map entries\n")
748 bgp_redistribute_routemap_set (vty->index, AFI_IP6, ZEBRA_ROUTE_CONNECT, argv[0]);
749 return CMD_SUCCESS;
752 DEFUN (no_ipv6_bgp_redistribute_connected,
753 no_ipv6_bgp_redistribute_connected_cmd,
754 "no redistribute connected",
755 NO_STR
756 "Redistribute information from another routing protocol\n"
757 "Connected\n")
759 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_CONNECT);
760 return CMD_SUCCESS;
763 DEFUN (no_ipv6_bgp_redistribute_connected_routemap,
764 no_ipv6_bgp_redistribute_connected_routemap_cmd,
765 "no redistribute connected route-map WORD",
766 NO_STR
767 "Redistribute information from another routing protocol\n"
768 "Connected\n"
769 "Route map reference\n"
770 "Pointer to route-map entries\n")
772 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_CONNECT);
773 return CMD_SUCCESS;
776 DEFUN (ipv6_bgp_redistribute_ripng,
777 ipv6_bgp_redistribute_ripng_cmd,
778 "redistribute ripng",
779 "Redistribute information from another routing protocol\n"
780 "IPv6 Routing Information Protocol (RIPng)\n")
782 bgp_redistribute_set (vty->index, AFI_IP6, ZEBRA_ROUTE_RIPNG);
783 return CMD_SUCCESS;
786 DEFUN (ipv6_bgp_redistribute_ripng_routemap,
787 ipv6_bgp_redistribute_ripng_routemap_cmd,
788 "redistribute ripng route-map WORD",
789 "Redistribute information from another routing protocol\n"
790 "IPv6 Routing Information Protocol (RIPng)\n"
791 "Route map reference\n"
792 "Pointer to route-map entries\n")
794 bgp_redistribute_routemap_set (vty->index, AFI_IP6, ZEBRA_ROUTE_RIPNG, argv[0]);
795 return CMD_SUCCESS;
798 DEFUN (no_ipv6_bgp_redistribute_ripng,
799 no_ipv6_bgp_redistribute_ripng_cmd,
800 "no redistribute ripng",
801 NO_STR
802 "Redistribute information from another routing protocol\n"
803 "IPv6 Routing Information Protocol (RIPng)\n")
805 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_RIPNG);
806 return CMD_SUCCESS;
809 DEFUN (no_ipv6_bgp_redistribute_ripng_routemap,
810 no_ipv6_bgp_redistribute_ripng_routemap_cmd,
811 "no redistribute ripng route-map WORD",
812 NO_STR
813 "Redistribute information from another routing protocol\n"
814 "IPv6 Routing Information Protocol (RIPng)\n"
815 "Route map reference\n"
816 "Pointer to route-map entries\n")
818 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_RIPNG);
819 return CMD_SUCCESS;
822 DEFUN (ipv6_bgp_redistribute_ospf6,
823 ipv6_bgp_redistribute_ospf6_cmd,
824 "redistribute ospf6",
825 "Redistribute information from another routing protocol\n"
826 "IPv6 Open Shortest Path First (OSPFv3)\n")
828 bgp_redistribute_set (vty->index, AFI_IP6, ZEBRA_ROUTE_OSPF6);
829 return CMD_SUCCESS;
832 DEFUN (ipv6_bgp_redistribute_ospf6_routemap,
833 ipv6_bgp_redistribute_ospf6_routemap_cmd,
834 "redistribute ospf6 route-map WORD",
835 "Redistribute information from another routing protocol\n"
836 "IPv6 Open Shortest Path First (OSPFv3)\n"
837 "Route map reference\n"
838 "Pointer to route-map entries\n")
840 bgp_redistribute_routemap_set (vty->index, AFI_IP6, ZEBRA_ROUTE_OSPF6, argv[0]);
841 return CMD_SUCCESS;
844 DEFUN (no_ipv6_bgp_redistribute_ospf6,
845 no_ipv6_bgp_redistribute_ospf6_cmd,
846 "no redistribute ospf6",
847 NO_STR
848 "Redistribute information from another routing protocol\n"
849 "IPv6 Open Shortest Path First (OSPFv3)\n")
851 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_OSPF6);
852 return CMD_SUCCESS;
855 DEFUN (no_ipv6_bgp_redistribute_ospf6_routemap,
856 no_ipv6_bgp_redistribute_ospf6_routemap_cmd,
857 "no redistribute ospf6 route-map WORD",
858 NO_STR
859 "Redistribute information from another routing protocol\n"
860 "IPv6 Open Shortest Path First (OSPFv3)\n"
861 "Route map reference\n"
862 "Pointer to route-map entries\n")
864 bgp_redistribute_unset (vty->index, AFI_IP6, ZEBRA_ROUTE_OSPF6);
865 return CMD_SUCCESS;
868 /* Old config. */
869 ALIAS (ipv6_bgp_redistribute_kernel,
870 old_ipv6_bgp_redistribute_kernel_cmd,
871 "ipv6 bgp redistribute kernel",
872 IPV6_STR
873 BGP_STR
874 "Redistribute information from another routing protocol\n"
875 "Kernel routes\n")
877 ALIAS (ipv6_bgp_redistribute_kernel_routemap,
878 old_ipv6_bgp_redistribute_kernel_routemap_cmd,
879 "ipv6 bgp redistribute kernel route-map WORD",
880 IPV6_STR
881 BGP_STR
882 "Redistribute information from another routing protocol\n"
883 "Kernel routes\n"
884 "Route map reference\n"
885 "Pointer to route-map entries\n")
887 ALIAS (no_ipv6_bgp_redistribute_kernel,
888 old_no_ipv6_bgp_redistribute_kernel_cmd,
889 "no ipv6 bgp redistribute kernel",
890 NO_STR
891 IPV6_STR
892 BGP_STR
893 "Redistribute information from another routing protocol\n"
894 "Kernel routes\n")
896 ALIAS (no_ipv6_bgp_redistribute_kernel_routemap,
897 old_no_ipv6_bgp_redistribute_kernel_routemap_cmd,
898 "no ipv6 bgp redistribute kernel route-map WORD",
899 NO_STR
900 IPV6_STR
901 BGP_STR
902 "Redistribute information from another routing protocol\n"
903 "Kernel routes\n"
904 "Route map reference\n"
905 "Pointer to route-map entries\n")
907 ALIAS (ipv6_bgp_redistribute_static,
908 old_ipv6_bgp_redistribute_static_cmd,
909 "ipv6 bgp redistribute static",
910 IPV6_STR
911 BGP_STR
912 "Redistribute information from another routing protocol\n"
913 "Static routes\n")
915 ALIAS (ipv6_bgp_redistribute_static_routemap,
916 old_ipv6_bgp_redistribute_static_routemap_cmd,
917 "ipv6 bgp redistribute static route-map WORD",
918 IPV6_STR
919 BGP_STR
920 "Redistribute information from another routing protocol\n"
921 "Static routes\n"
922 "Route map reference\n"
923 "Pointer to route-map entries\n")
925 ALIAS (no_ipv6_bgp_redistribute_static,
926 old_no_ipv6_bgp_redistribute_static_cmd,
927 "no ipv6 bgp redistribute static",
928 NO_STR
929 IPV6_STR
930 BGP_STR
931 "Redistribute information from another routing protocol\n"
932 "Static routes\n")
934 ALIAS (no_ipv6_bgp_redistribute_static_routemap,
935 old_no_ipv6_bgp_redistribute_static_routemap_cmd,
936 "no ipv6 bgp redistribute static route-map WORD",
937 NO_STR
938 IPV6_STR
939 BGP_STR
940 "Redistribute information from another routing protocol\n"
941 "Static routes\n"
942 "Route map reference\n"
943 "Pointer to route-map entries\n")
945 ALIAS (ipv6_bgp_redistribute_connected,
946 old_ipv6_bgp_redistribute_connected_cmd,
947 "ipv6 bgp redistribute connected",
948 IPV6_STR
949 BGP_STR
950 "Redistribute information from another routing protocol\n"
951 "Connected\n")
953 ALIAS (ipv6_bgp_redistribute_connected_routemap,
954 old_ipv6_bgp_redistribute_connected_routemap_cmd,
955 "ipv6 bgp redistribute connected route-map WORD",
956 IPV6_STR
957 BGP_STR
958 "Redistribute information from another routing protocol\n"
959 "Connected\n"
960 "Route map reference\n"
961 "Pointer to route-map entries\n")
963 ALIAS (no_ipv6_bgp_redistribute_connected,
964 old_no_ipv6_bgp_redistribute_connected_cmd,
965 "no ipv6 bgp redistribute connected",
966 NO_STR
967 IPV6_STR
968 BGP_STR
969 "Redistribute information from another routing protocol\n"
970 "Connected\n")
972 ALIAS (no_ipv6_bgp_redistribute_connected_routemap,
973 old_no_ipv6_bgp_redistribute_connected_routemap_cmd,
974 "no ipv6 bgp redistribute connected route-map WORD",
975 NO_STR
976 IPV6_STR
977 BGP_STR
978 "Redistribute information from another routing protocol\n"
979 "Connected\n"
980 "Route map reference\n"
981 "Pointer to route-map entries\n")
983 ALIAS (ipv6_bgp_redistribute_ripng,
984 old_ipv6_bgp_redistribute_ripng_cmd,
985 "ipv6 bgp redistribute ripng",
986 IPV6_STR
987 BGP_STR
988 "Redistribute information from another routing protocol\n"
989 "IPv6 Routing Information Protocol (RIPng)\n")
991 ALIAS (ipv6_bgp_redistribute_ripng_routemap,
992 old_ipv6_bgp_redistribute_ripng_routemap_cmd,
993 "ipv6 bgp redistribute ripng route-map WORD",
994 IPV6_STR
995 BGP_STR
996 "Redistribute information from another routing protocol\n"
997 "IPv6 Routing Information Protocol (RIPng)\n"
998 "Route map reference\n"
999 "Pointer to route-map entries\n")
1001 ALIAS (no_ipv6_bgp_redistribute_ripng,
1002 old_no_ipv6_bgp_redistribute_ripng_cmd,
1003 "no ipv6 bgp redistribute ripng",
1004 NO_STR
1005 IPV6_STR
1006 BGP_STR
1007 "Redistribute information from another routing protocol\n"
1008 "IPv6 Routing Information Protocol (RIPng)\n")
1010 ALIAS (no_ipv6_bgp_redistribute_ripng_routemap,
1011 old_no_ipv6_bgp_redistribute_ripng_routemap_cmd,
1012 "no ipv6 bgp redistribute ripng route-map WORD",
1013 NO_STR
1014 IPV6_STR
1015 BGP_STR
1016 "Redistribute information from another routing protocol\n"
1017 "IPv6 Routing Information Protocol (RIPng)\n"
1018 "Route map reference\n"
1019 "Pointer to route-map entries\n")
1021 ALIAS (ipv6_bgp_redistribute_ospf6,
1022 old_ipv6_bgp_redistribute_ospf6_cmd,
1023 "ipv6 bgp redistribute ospf6",
1024 IPV6_STR
1025 BGP_STR
1026 "Redistribute information from another routing protocol\n"
1027 "IPv6 Open Shortest Path First (OSPFv3)\n")
1029 ALIAS (ipv6_bgp_redistribute_ospf6_routemap,
1030 old_ipv6_bgp_redistribute_ospf6_routemap_cmd,
1031 "ipv6 bgp redistribute ospf6 route-map WORD",
1032 IPV6_STR
1033 BGP_STR
1034 "Redistribute information from another routing protocol\n"
1035 "IPv6 Open Shortest Path First (OSPFv3)\n"
1036 "Route map reference\n"
1037 "Pointer to route-map entries\n")
1039 ALIAS (no_ipv6_bgp_redistribute_ospf6,
1040 old_no_ipv6_bgp_redistribute_ospf6_cmd,
1041 "no ipv6 bgp redistribute ospf6",
1042 NO_STR
1043 IPV6_STR
1044 BGP_STR
1045 "Redistribute information from another routing protocol\n"
1046 "IPv6 Open Shortest Path First (OSPFv3)\n")
1048 ALIAS (no_ipv6_bgp_redistribute_ospf6_routemap,
1049 old_no_ipv6_bgp_redistribute_ospf6_routemap_cmd,
1050 "no ipv6 bgp redistribute ospf6 route-map WORD",
1051 NO_STR
1052 IPV6_STR
1053 BGP_STR
1054 "Redistribute information from another routing protocol\n"
1055 "IPv6 Open Shortest Path First (OSPFv3)\n"
1056 "Route map reference\n"
1057 "Pointer to route-map entries\n")
1058 #endif /* HAVE_IPV6 */
1060 struct interface *
1061 if_lookup_by_ipv4 (struct in_addr *addr)
1063 listnode ifnode;
1064 listnode cnode;
1065 struct interface *ifp;
1066 struct connected *connected;
1067 struct prefix_ipv4 p;
1068 struct prefix *cp;
1070 p.family = AF_INET;
1071 p.prefix = *addr;
1072 p.prefixlen = IPV4_MAX_BITLEN;
1074 for (ifnode = listhead (iflist); ifnode; nextnode (ifnode))
1076 ifp = getdata (ifnode);
1078 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
1080 connected = getdata (cnode);
1081 cp = connected->address;
1083 if (cp->family == AF_INET)
1084 if (prefix_match (cp, (struct prefix *)&p))
1085 return ifp;
1088 return NULL;
1091 #ifdef HAVE_IPV6
1092 struct interface *
1093 if_lookup_by_ipv6 (struct in6_addr *addr)
1095 listnode ifnode;
1096 listnode cnode;
1097 struct interface *ifp;
1098 struct connected *connected;
1099 struct prefix_ipv6 p;
1100 struct prefix *cp;
1102 p.family = AF_INET6;
1103 p.prefix = *addr;
1104 p.prefixlen = IPV6_MAX_BITLEN;
1106 for (ifnode = listhead (iflist); ifnode; nextnode (ifnode))
1108 ifp = getdata (ifnode);
1110 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
1112 connected = getdata (cnode);
1113 cp = connected->address;
1115 if (cp->family == AF_INET6)
1116 if (prefix_match (cp, (struct prefix *)&p))
1117 return ifp;
1120 return NULL;
1122 #endif /* HAVE_IPV6 */
1124 #ifdef HAVE_IPV6
1126 if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
1128 listnode cnode;
1129 struct connected *connected;
1130 struct prefix *cp;
1132 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
1134 connected = getdata (cnode);
1135 cp = connected->address;
1137 if (cp->family == AF_INET6)
1138 if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
1140 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
1141 return 1;
1144 return 0;
1148 if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
1150 listnode cnode;
1151 struct connected *connected;
1152 struct prefix *cp;
1154 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
1156 connected = getdata (cnode);
1157 cp = connected->address;
1159 if (cp->family == AF_INET6)
1160 if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
1162 memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
1163 return 1;
1166 return 0;
1168 #endif /* HAVE_IPV6 */
1171 bgp_nexthop_set (union sockunion *local, union sockunion *remote,
1172 struct bgp_nexthop *nexthop, struct peer *peer)
1174 int ret = 0;
1175 struct interface *ifp = NULL;
1177 memset (nexthop, 0, sizeof (struct bgp_nexthop));
1179 if (!local)
1180 return -1;
1181 if (!remote)
1182 return -1;
1184 if (local->sa.sa_family == AF_INET)
1186 nexthop->v4 = local->sin.sin_addr;
1187 ifp = if_lookup_by_ipv4 (&local->sin.sin_addr);
1189 #ifdef HAVE_IPV6
1190 if (local->sa.sa_family == AF_INET6)
1192 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1194 if (peer->ifname)
1195 ifp = if_lookup_by_index (if_nametoindex (peer->ifname));
1197 else
1198 ifp = if_lookup_by_ipv6 (&local->sin6.sin6_addr);
1200 #endif /* HAVE_IPV6 */
1202 if (!ifp)
1203 return -1;
1205 nexthop->ifp = ifp;
1207 /* IPv4 connection. */
1208 if (local->sa.sa_family == AF_INET)
1210 #ifdef HAVE_IPV6
1211 /* IPv6 nexthop*/
1212 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1214 /* There is no global nexthop. */
1215 if (!ret)
1216 if_get_ipv6_local (ifp, &nexthop->v6_global);
1217 else
1218 if_get_ipv6_local (ifp, &nexthop->v6_local);
1219 #endif /* HAVE_IPV6 */
1222 #ifdef HAVE_IPV6
1223 /* IPv6 connection. */
1224 if (local->sa.sa_family == AF_INET6)
1226 struct interface *direct = NULL;
1228 /* IPv4 nexthop. I don't care about it. */
1229 if (peer->local_id.s_addr)
1230 nexthop->v4 = peer->local_id;
1232 /* Global address*/
1233 if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr))
1235 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1236 IPV6_MAX_BYTELEN);
1238 /* If directory connected set link-local address. */
1239 direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr);
1240 if (direct)
1241 if_get_ipv6_local (ifp, &nexthop->v6_local);
1243 else
1244 /* Link-local address. */
1246 ret = if_get_ipv6_global (ifp, &nexthop->v6_global);
1248 /* If there is no global address. Set link-local address as
1249 global. I know this break RFC specification... */
1250 if (!ret)
1251 memcpy (&nexthop->v6_global, &local->sin6.sin6_addr,
1252 IPV6_MAX_BYTELEN);
1253 else
1254 memcpy (&nexthop->v6_local, &local->sin6.sin6_addr,
1255 IPV6_MAX_BYTELEN);
1259 if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) ||
1260 if_lookup_by_ipv6 (&remote->sin6.sin6_addr))
1261 peer->shared_network = 1;
1262 else
1263 peer->shared_network = 0;
1265 /* KAME stack specific treatment. */
1266 #ifdef KAME
1267 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global)
1268 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global))
1270 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0);
1272 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local)
1273 && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local))
1275 SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0);
1277 #endif /* KAME */
1278 #endif /* HAVE_IPV6 */
1279 return ret;
1282 #ifdef HAVE_IPV6
1283 unsigned int
1284 bgp_ifindex_by_nexthop (struct in6_addr *addr)
1286 listnode ifnode;
1287 listnode cnode;
1288 struct interface *ifp;
1289 struct connected *connected;
1290 struct prefix_ipv6 p;
1292 p.family = AF_INET6;
1293 p.prefix = *addr;
1294 p.prefixlen = IPV6_MAX_BITLEN;
1296 for (ifnode = listhead (iflist); ifnode; nextnode (ifnode))
1298 ifp = getdata (ifnode);
1300 for (cnode = listhead (ifp->connected); cnode; nextnode (cnode))
1302 struct prefix *cp;
1304 connected = getdata (cnode);
1305 cp = connected->address;
1307 if (cp->family == AF_INET6)
1309 if (prefix_match (cp, (struct prefix *)&p))
1310 return ifp->ifindex;
1314 return 0;
1316 #endif /* HAVE_IPV6 */
1318 void
1319 bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp)
1321 int flags;
1322 u_char distance;
1323 struct peer *peer;
1325 if (zclient->sock < 0)
1326 return;
1328 if (! zclient->redist[ZEBRA_ROUTE_BGP])
1329 return;
1331 flags = 0;
1332 peer = info->peer;
1334 if (peer_sort (peer) == BGP_PEER_IBGP || peer_sort (peer) == BGP_PEER_CONFED)
1336 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1337 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1340 if (peer_sort (peer) == BGP_PEER_EBGP && peer->ttl != 1)
1341 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1343 if (p->family == AF_INET)
1345 struct zapi_ipv4 api;
1346 struct in_addr *nexthop;
1348 api.flags = flags;
1349 nexthop = &info->attr->nexthop;
1351 api.type = ZEBRA_ROUTE_BGP;
1352 api.message = 0;
1353 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1354 api.nexthop_num = 1;
1355 api.nexthop = &nexthop;
1356 api.ifindex_num = 0;
1357 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1358 api.metric = info->attr->med;
1360 distance = bgp_distance_apply (p, info, bgp);
1362 if (distance)
1364 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
1365 api.distance = distance;
1367 zapi_ipv4_add (zclient, (struct prefix_ipv4 *) p, &api);
1369 #ifdef HAVE_IPV6
1370 /* We have to think about a IPv6 link-local address curse. */
1371 if (p->family == AF_INET6)
1373 unsigned int ifindex;
1374 struct in6_addr *nexthop;
1375 struct zapi_ipv6 api;
1377 ifindex = 0;
1378 nexthop = NULL;
1380 /* Only global address nexthop exists. */
1381 if (info->attr->mp_nexthop_len == 16)
1382 nexthop = &info->attr->mp_nexthop_global;
1384 /* If both global and link-local address present. */
1385 if (info->attr->mp_nexthop_len == 32)
1387 /* Workaround for Cisco's nexthop bug. */
1388 if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->mp_nexthop_global)
1389 && peer->su_remote->sa.sa_family == AF_INET6)
1390 nexthop = &peer->su_remote->sin6.sin6_addr;
1391 else
1392 nexthop = &info->attr->mp_nexthop_local;
1394 if (info->peer->nexthop.ifp)
1395 ifindex = info->peer->nexthop.ifp->ifindex;
1398 if (nexthop == NULL)
1399 return;
1401 if (IN6_IS_ADDR_LINKLOCAL (nexthop) && ! ifindex)
1403 if (info->peer->ifname)
1404 ifindex = if_nametoindex (info->peer->ifname);
1405 else if (info->peer->nexthop.ifp)
1406 ifindex = info->peer->nexthop.ifp->ifindex;
1409 /* Make Zebra API structure. */
1410 api.flags = flags;
1411 api.type = ZEBRA_ROUTE_BGP;
1412 api.message = 0;
1413 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1414 api.nexthop_num = 1;
1415 api.nexthop = &nexthop;
1416 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
1417 api.ifindex_num = 1;
1418 api.ifindex = &ifindex;
1419 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1420 api.metric = info->attr->med;
1422 zapi_ipv6_add (zclient, (struct prefix_ipv6 *) p, &api);
1424 #endif /* HAVE_IPV6 */
1427 void
1428 bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info)
1430 int flags;
1431 struct peer *peer;
1433 if (zclient->sock < 0)
1434 return;
1436 if (! zclient->redist[ZEBRA_ROUTE_BGP])
1437 return;
1439 peer = info->peer;
1440 flags = 0;
1442 if (peer_sort (peer) == BGP_PEER_IBGP)
1444 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1445 SET_FLAG (flags, ZEBRA_FLAG_IBGP);
1448 if (peer_sort (peer) == BGP_PEER_EBGP && peer->ttl != 1)
1449 SET_FLAG (flags, ZEBRA_FLAG_INTERNAL);
1451 if (p->family == AF_INET)
1453 struct zapi_ipv4 api;
1454 struct in_addr *nexthop;
1456 api.flags = flags;
1457 nexthop = &info->attr->nexthop;
1459 api.type = ZEBRA_ROUTE_BGP;
1460 api.message = 0;
1461 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1462 api.nexthop_num = 1;
1463 api.nexthop = &nexthop;
1464 api.ifindex_num = 0;
1465 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1466 api.metric = info->attr->med;
1468 zapi_ipv4_delete (zclient, (struct prefix_ipv4 *) p, &api);
1470 #ifdef HAVE_IPV6
1471 /* We have to think about a IPv6 link-local address curse. */
1472 if (p->family == AF_INET6)
1474 struct zapi_ipv6 api;
1475 unsigned int ifindex;
1476 struct in6_addr *nexthop;
1478 ifindex = 0;
1479 nexthop = NULL;
1481 /* Only global address nexthop exists. */
1482 if (info->attr->mp_nexthop_len == 16)
1483 nexthop = &info->attr->mp_nexthop_global;
1485 /* If both global and link-local address present. */
1486 if (info->attr->mp_nexthop_len == 32)
1488 nexthop = &info->attr->mp_nexthop_local;
1489 if (info->peer->nexthop.ifp)
1490 ifindex = info->peer->nexthop.ifp->ifindex;
1493 if (nexthop == NULL)
1494 return;
1496 if (IN6_IS_ADDR_LINKLOCAL (nexthop) && ! ifindex)
1497 if (info->peer->ifname)
1498 ifindex = if_nametoindex (info->peer->ifname);
1500 api.flags = flags;
1501 api.type = ZEBRA_ROUTE_BGP;
1502 api.message = 0;
1503 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
1504 api.nexthop_num = 1;
1505 api.nexthop = &nexthop;
1506 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
1507 api.ifindex_num = 1;
1508 api.ifindex = &ifindex;
1509 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
1510 api.metric = info->attr->med;
1512 zapi_ipv6_delete (zclient, (struct prefix_ipv6 *) p, &api);
1514 #endif /* HAVE_IPV6 */
1517 DEFUN (router_zebra,
1518 router_zebra_cmd,
1519 "router zebra",
1520 "Enable a routing process\n"
1521 "Make connection to zebra daemon\n")
1523 vty->node = ZEBRA_NODE;
1524 zclient->enable = 1;
1525 zclient_start (zclient);
1526 return CMD_SUCCESS;
1529 DEFUN (no_router_zebra,
1530 no_router_zebra_cmd,
1531 "no router zebra",
1532 NO_STR
1533 "Configure routing process\n"
1534 "Disable connection to zebra daemon\n")
1536 zclient->enable = 0;
1537 zclient_stop (zclient);
1538 return CMD_SUCCESS;
1541 DEFUN (redistribute_bgp,
1542 redistribute_bgp_cmd,
1543 "redistribute bgp",
1544 "Redistribute control\n"
1545 "BGP route\n")
1547 zclient->redist[ZEBRA_ROUTE_BGP] = 1;
1548 return CMD_SUCCESS;
1551 DEFUN (no_redistribute_bgp,
1552 no_redistribute_bgp_cmd,
1553 "no redistribute bgp",
1554 NO_STR
1555 "Redistribute control\n"
1556 "BGP route\n")
1558 zclient->redist[ZEBRA_ROUTE_BGP] = 0;
1559 return CMD_SUCCESS;
1562 /* Zebra configuration write function. */
1564 zebra_config_write (struct vty *vty)
1566 if (! zclient->enable)
1568 vty_out (vty, "no router zebra%s", VTY_NEWLINE);
1569 return 1;
1571 else if (! zclient->redist[ZEBRA_ROUTE_BGP])
1573 vty_out (vty, "router zebra%s", VTY_NEWLINE);
1574 vty_out (vty, " no redistribute bgp%s", VTY_NEWLINE);
1575 return 1;
1577 return 0;
1580 /* Redistribute configuration. */
1582 bgp_config_write_redistribute (struct vty *vty, struct bgp *bgp, afi_t afi,
1583 safi_t safi, int *write)
1585 int i;
1586 char *str[] = { "system", "kernel", "connected", "static", "rip",
1587 "ripng", "ospf", "ospf6", "bgp"};
1589 if (safi != SAFI_UNICAST)
1590 return 0;
1592 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1594 if (i != ZEBRA_ROUTE_BGP && bgp->redist[afi][i])
1596 /* "address-family" display. */
1597 bgp_config_write_family_header (vty, afi, safi, write);
1599 if (bgp->rmap[afi][i].name)
1600 vty_out (vty, "%sredistribute %s route-map %s%s",
1601 afi == AFI_IP ? " " : " ",
1602 str[i], bgp->rmap[afi][i].name, VTY_NEWLINE);
1603 else
1604 vty_out (vty, "%sredistribute %s%s",
1605 afi == AFI_IP ? " " : " ",
1606 str[i], VTY_NEWLINE);
1609 return *write;
1612 /* Zebra node structure. */
1613 struct cmd_node zebra_node =
1615 ZEBRA_NODE,
1616 "%s(config-router)# ",
1619 void
1620 bgp_zclient_reset ()
1622 zclient_reset (zclient);
1625 void
1626 zebra_init (int enable)
1628 /* Set default values. */
1629 zclient = zclient_new ();
1630 zclient_init (zclient, ZEBRA_ROUTE_BGP);
1631 zclient->interface_add = bgp_interface_add;
1632 zclient->interface_delete = bgp_interface_delete;
1633 zclient->interface_address_add = bgp_interface_address_add;
1634 zclient->interface_address_delete = bgp_interface_address_delete;
1635 zclient->ipv4_route_add = zebra_read_ipv4;
1636 zclient->ipv4_route_delete = zebra_read_ipv4;
1637 zclient->interface_up = bgp_interface_up;
1638 zclient->interface_down = bgp_interface_down;
1639 #ifdef HAVE_IPV6
1640 zclient->ipv6_route_add = zebra_read_ipv6;
1641 zclient->ipv6_route_delete = zebra_read_ipv6;
1642 #endif /* HAVE_IPV6 */
1644 /* Install zebra node. */
1645 install_node (&zebra_node, zebra_config_write);
1647 install_element (CONFIG_NODE, &router_zebra_cmd);
1648 install_element (CONFIG_NODE, &no_router_zebra_cmd);
1649 install_default (ZEBRA_NODE);
1650 install_element (ZEBRA_NODE, &redistribute_bgp_cmd);
1651 install_element (ZEBRA_NODE, &no_redistribute_bgp_cmd);
1653 install_element (BGP_NODE, &bgp_redistribute_kernel_cmd);
1654 install_element (BGP_NODE, &bgp_redistribute_kernel_routemap_cmd);
1655 install_element (BGP_NODE, &no_bgp_redistribute_kernel_cmd);
1656 install_element (BGP_NODE, &no_bgp_redistribute_kernel_routemap_cmd);
1657 install_element (BGP_NODE, &bgp_redistribute_static_cmd);
1658 install_element (BGP_NODE, &bgp_redistribute_static_routemap_cmd);
1659 install_element (BGP_NODE, &no_bgp_redistribute_static_cmd);
1660 install_element (BGP_NODE, &no_bgp_redistribute_static_routemap_cmd);
1661 install_element (BGP_NODE, &bgp_redistribute_connected_cmd);
1662 install_element (BGP_NODE, &bgp_redistribute_connected_routemap_cmd);
1663 install_element (BGP_NODE, &no_bgp_redistribute_connected_cmd);
1664 install_element (BGP_NODE, &no_bgp_redistribute_connected_routemap_cmd);
1665 install_element (BGP_NODE, &bgp_redistribute_rip_cmd);
1666 install_element (BGP_NODE, &bgp_redistribute_rip_routemap_cmd);
1667 install_element (BGP_NODE, &no_bgp_redistribute_rip_cmd);
1668 install_element (BGP_NODE, &no_bgp_redistribute_rip_routemap_cmd);
1669 install_element (BGP_NODE, &bgp_redistribute_ospf_cmd);
1670 install_element (BGP_NODE, &bgp_redistribute_ospf_routemap_cmd);
1671 install_element (BGP_NODE, &no_bgp_redistribute_ospf_cmd);
1672 install_element (BGP_NODE, &no_bgp_redistribute_ospf_routemap_cmd);
1674 #ifdef HAVE_IPV6
1675 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_kernel_cmd);
1676 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_kernel_routemap_cmd);
1677 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_kernel_cmd);
1678 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_kernel_routemap_cmd);
1679 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_static_cmd);
1680 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_static_routemap_cmd);
1681 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_static_cmd);
1682 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_static_routemap_cmd);
1683 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_connected_cmd);
1684 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_connected_routemap_cmd);
1685 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_connected_cmd);
1686 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_connected_routemap_cmd);
1687 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_ripng_cmd);
1688 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_ripng_routemap_cmd);
1689 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_ripng_cmd);
1690 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_ripng_routemap_cmd);
1691 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_ospf6_cmd);
1692 install_element (BGP_IPV6_NODE, &ipv6_bgp_redistribute_ospf6_routemap_cmd);
1693 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_ospf6_cmd);
1694 install_element (BGP_IPV6_NODE, &no_ipv6_bgp_redistribute_ospf6_routemap_cmd);
1696 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_kernel_cmd);
1697 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_kernel_routemap_cmd);
1698 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_kernel_cmd);
1699 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_kernel_routemap_cmd);
1700 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_static_cmd);
1701 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_static_routemap_cmd);
1702 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_static_cmd);
1703 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_static_routemap_cmd);
1704 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_connected_cmd);
1705 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_connected_routemap_cmd);
1706 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_connected_cmd);
1707 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_connected_routemap_cmd);
1708 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_ripng_cmd);
1709 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_ripng_routemap_cmd);
1710 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_ripng_cmd);
1711 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_ripng_routemap_cmd);
1712 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_ospf6_cmd);
1713 install_element (BGP_NODE, &old_ipv6_bgp_redistribute_ospf6_routemap_cmd);
1714 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_ospf6_cmd);
1715 install_element (BGP_NODE, &old_no_ipv6_bgp_redistribute_ospf6_routemap_cmd);
1716 #endif /* HAVE_IPV6 */
1718 /* Interface related init. */
1719 if_init ();