]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_zebra.c
Merge branch 'master' into evpn-session-vrf
[mirror_frr.git] / ripngd / ripng_zebra.c
1 /*
2 * RIPngd and zebra interface.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "command.h"
25 #include "prefix.h"
26 #include "agg_table.h"
27 #include "stream.h"
28 #include "memory.h"
29 #include "routemap.h"
30 #include "zclient.h"
31 #include "log.h"
32
33 #include "ripngd/ripngd.h"
34 #include "ripngd/ripng_debug.h"
35
36 /* All information about zebra. */
37 struct zclient *zclient = NULL;
38
39 /* Send ECMP routes to zebra. */
40 static void ripng_zebra_ipv6_send(struct agg_node *rp, uint8_t cmd)
41 {
42 struct list *list = (struct list *)rp->info;
43 struct zapi_route api;
44 struct zapi_nexthop *api_nh;
45 struct listnode *listnode = NULL;
46 struct ripng_info *rinfo = NULL;
47 int count = 0;
48
49 memset(&api, 0, sizeof(api));
50 api.vrf_id = VRF_DEFAULT;
51 api.type = ZEBRA_ROUTE_RIPNG;
52 api.safi = SAFI_UNICAST;
53 api.prefix = rp->p;
54
55 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
56 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
57 if (count >= MULTIPATH_NUM)
58 break;
59 api_nh = &api.nexthops[count];
60 api_nh->vrf_id = VRF_DEFAULT;
61 api_nh->gate.ipv6 = rinfo->nexthop;
62 api_nh->ifindex = rinfo->ifindex;
63 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
64 count++;
65 if (cmd == ZEBRA_ROUTE_ADD)
66 SET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
67 else
68 UNSET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
69 }
70
71 api.nexthop_num = count;
72
73 rinfo = listgetdata(listhead(list));
74
75 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
76 api.metric = rinfo->metric;
77
78 if (rinfo->tag) {
79 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
80 api.tag = rinfo->tag;
81 }
82
83 zclient_route_send(cmd, zclient, &api);
84
85 if (IS_RIPNG_DEBUG_ZEBRA) {
86 if (ripng->ecmp)
87 zlog_debug("%s: %s/%d nexthops %d",
88 (cmd == ZEBRA_ROUTE_ADD)
89 ? "Install into zebra"
90 : "Delete from zebra",
91 inet6_ntoa(rp->p.u.prefix6), rp->p.prefixlen,
92 count);
93 else
94 zlog_debug(
95 "%s: %s/%d",
96 (cmd == ZEBRA_ROUTE_ADD) ? "Install into zebra"
97 : "Delete from zebra",
98 inet6_ntoa(rp->p.u.prefix6), rp->p.prefixlen);
99 }
100 }
101
102 /* Add/update ECMP routes to zebra. */
103 void ripng_zebra_ipv6_add(struct agg_node *rp)
104 {
105 ripng_zebra_ipv6_send(rp, ZEBRA_ROUTE_ADD);
106 }
107
108 /* Delete ECMP routes from zebra. */
109 void ripng_zebra_ipv6_delete(struct agg_node *rp)
110 {
111 ripng_zebra_ipv6_send(rp, ZEBRA_ROUTE_DELETE);
112 }
113
114 /* Zebra route add and delete treatment. */
115 static int ripng_zebra_read_route(int command, struct zclient *zclient,
116 zebra_size_t length, vrf_id_t vrf_id)
117 {
118 struct zapi_route api;
119 struct in6_addr nexthop;
120 unsigned long ifindex;
121
122 if (zapi_route_decode(zclient->ibuf, &api) < 0)
123 return -1;
124
125 /* we completely ignore srcdest routes for now. */
126 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
127 return 0;
128
129 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
130 return 0;
131
132 nexthop = api.nexthops[0].gate.ipv6;
133 ifindex = api.nexthops[0].ifindex;
134
135 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
136 ripng_redistribute_add(api.type, RIPNG_ROUTE_REDISTRIBUTE,
137 (struct prefix_ipv6 *)&api.prefix,
138 ifindex, &nexthop, api.tag);
139 else
140 ripng_redistribute_delete(api.type, RIPNG_ROUTE_REDISTRIBUTE,
141 (struct prefix_ipv6 *)&api.prefix,
142 ifindex);
143
144 return 0;
145 }
146
147 void ripng_redistribute_conf_update(int type)
148 {
149 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, type, 0,
150 VRF_DEFAULT);
151 }
152
153 void ripng_redistribute_conf_delete(int type)
154 {
155 if (zclient->sock > 0)
156 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
157 AFI_IP6, type, 0, VRF_DEFAULT);
158
159 ripng_redistribute_withdraw(type);
160 }
161
162 int ripng_redistribute_check(int type)
163 {
164 return vrf_bitmap_check(zclient->redist[AFI_IP6][type], VRF_DEFAULT);
165 }
166
167 void ripng_redistribute_clean(void)
168 {
169 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
170 if (!vrf_bitmap_check(zclient->redist[AFI_IP6][i], VRF_DEFAULT))
171 continue;
172
173 if (zclient->sock > 0)
174 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE,
175 zclient, AFI_IP6, i, 0,
176 VRF_DEFAULT);
177
178 vrf_bitmap_unset(zclient->redist[AFI_IP6][i], VRF_DEFAULT);
179
180 /* Remove the routes from RIP table. */
181 ripng_redistribute_withdraw(i);
182 }
183 }
184
185 void ripng_redistribute_write(struct vty *vty)
186 {
187 int i;
188
189 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
190 if (i == zclient->redist_default
191 || !vrf_bitmap_check(zclient->redist[AFI_IP6][i],
192 VRF_DEFAULT))
193 continue;
194
195 vty_out(vty, " %s", zebra_route_string(i));
196 }
197 }
198
199 static void ripng_zebra_connected(struct zclient *zclient)
200 {
201 zclient_send_reg_requests(zclient, VRF_DEFAULT);
202 }
203
204 /* Initialize zebra structure and it's commands. */
205 void zebra_init(struct thread_master *master)
206 {
207 /* Allocate zebra structure. */
208 zclient = zclient_new(master, &zclient_options_default);
209 zclient_init(zclient, ZEBRA_ROUTE_RIPNG, 0, &ripngd_privs);
210
211 zclient->zebra_connected = ripng_zebra_connected;
212 zclient->interface_up = ripng_interface_up;
213 zclient->interface_down = ripng_interface_down;
214 zclient->interface_add = ripng_interface_add;
215 zclient->interface_delete = ripng_interface_delete;
216 zclient->interface_address_add = ripng_interface_address_add;
217 zclient->interface_address_delete = ripng_interface_address_delete;
218 zclient->redistribute_route_add = ripng_zebra_read_route;
219 zclient->redistribute_route_del = ripng_zebra_read_route;
220 }
221
222 void ripng_zebra_stop(void)
223 {
224 zclient_stop(zclient);
225 zclient_free(zclient);
226 }