]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_zebra.c
Merge pull request #4770 from kssoman/fib
[mirror_frr.git] / eigrpd / eigrp_zebra.c
1 /*
2 * Zebra connect library for EIGRP.
3 * Copyright (C) 2013-2014
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 *
11 * This file is part of GNU Zebra.
12 *
13 * GNU Zebra is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * GNU Zebra is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 #include <zebra.h>
29
30 #include "thread.h"
31 #include "command.h"
32 #include "network.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "table.h"
36 #include "stream.h"
37 #include "memory.h"
38 #include "zclient.h"
39 #include "filter.h"
40 #include "plist.h"
41 #include "log.h"
42 #include "nexthop.h"
43
44 #include "eigrpd/eigrp_structs.h"
45 #include "eigrpd/eigrpd.h"
46 #include "eigrpd/eigrp_interface.h"
47 #include "eigrpd/eigrp_neighbor.h"
48 #include "eigrpd/eigrp_packet.h"
49 #include "eigrpd/eigrp_zebra.h"
50 #include "eigrpd/eigrp_vty.h"
51 #include "eigrpd/eigrp_dump.h"
52 #include "eigrpd/eigrp_network.h"
53 #include "eigrpd/eigrp_topology.h"
54 #include "eigrpd/eigrp_fsm.h"
55
56 static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS);
57 static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS);
58
59 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS);
60
61 /* Zebra structure to hold current status. */
62 struct zclient *zclient = NULL;
63
64 /* For registering threads. */
65 extern struct thread_master *master;
66 struct in_addr router_id_zebra;
67
68 /* Router-id update message from zebra. */
69 static int eigrp_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
70 {
71 struct eigrp *eigrp;
72 struct prefix router_id;
73 zebra_router_id_update_read(zclient->ibuf, &router_id);
74
75 router_id_zebra = router_id.u.prefix4;
76
77 eigrp = eigrp_lookup(vrf_id);
78
79 if (eigrp != NULL)
80 eigrp_router_id_update(eigrp);
81
82 return 0;
83 }
84
85 static int eigrp_zebra_route_notify_owner(ZAPI_CALLBACK_ARGS)
86 {
87 struct prefix p;
88 enum zapi_route_notify_owner note;
89 uint32_t table;
90
91 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note, NULL,
92 NULL))
93 return -1;
94
95 return 0;
96 }
97
98 static void eigrp_zebra_connected(struct zclient *zclient)
99 {
100 zclient_send_reg_requests(zclient, VRF_DEFAULT);
101 }
102
103 void eigrp_zebra_init(void)
104 {
105 struct zclient_options opt = {.receive_notify = false};
106
107 zclient = zclient_new(master, &opt);
108
109 zclient_init(zclient, ZEBRA_ROUTE_EIGRP, 0, &eigrpd_privs);
110 zclient->zebra_connected = eigrp_zebra_connected;
111 zclient->router_id_update = eigrp_router_id_update_zebra;
112 zclient->interface_address_add = eigrp_interface_address_add;
113 zclient->interface_address_delete = eigrp_interface_address_delete;
114 zclient->redistribute_route_add = eigrp_zebra_read_route;
115 zclient->redistribute_route_del = eigrp_zebra_read_route;
116 zclient->route_notify_owner = eigrp_zebra_route_notify_owner;
117 }
118
119
120 /* Zebra route add and delete treatment. */
121 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS)
122 {
123 struct zapi_route api;
124 struct eigrp *eigrp;
125
126 if (zapi_route_decode(zclient->ibuf, &api) < 0)
127 return -1;
128
129 if (IPV4_NET127(ntohl(api.prefix.u.prefix4.s_addr)))
130 return 0;
131
132 eigrp = eigrp_lookup(vrf_id);
133 if (eigrp == NULL)
134 return 0;
135
136 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
137
138 } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
139 {
140 }
141
142 return 0;
143 }
144
145 static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS)
146 {
147 struct connected *c;
148
149 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
150
151 if (c == NULL)
152 return 0;
153
154 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
155 zlog_debug("Zebra: interface %s address add %pFX", c->ifp->name,
156 c->address);
157
158 eigrp_if_update(c->ifp);
159
160 return 0;
161 }
162
163 static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS)
164 {
165 struct connected *c;
166 struct interface *ifp;
167 struct eigrp_interface *ei;
168
169 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
170
171 if (c == NULL)
172 return 0;
173
174 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
175 zlog_debug("Zebra: interface %s address delete %pFX",
176 c->ifp->name, c->address);
177
178 ifp = c->ifp;
179 ei = ifp->info;
180 if (!ei)
181 return 0;
182
183 /* Call interface hook functions to clean up */
184 if (prefix_cmp(&ei->address, c->address) == 0)
185 eigrp_if_free(ei, INTERFACE_DOWN_BY_ZEBRA);
186
187 connected_free(&c);
188
189 return 0;
190 }
191
192 void eigrp_zebra_route_add(struct eigrp *eigrp, struct prefix *p,
193 struct list *successors, uint32_t distance)
194 {
195 struct zapi_route api;
196 struct zapi_nexthop *api_nh;
197 struct eigrp_nexthop_entry *te;
198 struct listnode *node;
199 int count = 0;
200
201 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
202 return;
203
204 memset(&api, 0, sizeof(api));
205 api.vrf_id = eigrp->vrf_id;
206 api.type = ZEBRA_ROUTE_EIGRP;
207 api.safi = SAFI_UNICAST;
208 api.metric = distance;
209 memcpy(&api.prefix, p, sizeof(*p));
210
211 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
212 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
213
214 /* Nexthop, ifindex, distance and metric information. */
215 for (ALL_LIST_ELEMENTS_RO(successors, node, te)) {
216 if (count >= MULTIPATH_NUM)
217 break;
218 api_nh = &api.nexthops[count];
219 api_nh->vrf_id = eigrp->vrf_id;
220 if (te->adv_router->src.s_addr) {
221 api_nh->gate.ipv4 = te->adv_router->src;
222 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
223 } else
224 api_nh->type = NEXTHOP_TYPE_IFINDEX;
225 api_nh->ifindex = te->ei->ifp->ifindex;
226
227 count++;
228 }
229 api.nexthop_num = count;
230
231 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
232 char buf[PREFIX_STRLEN];
233 zlog_debug("Zebra: Route add %pFX nexthop %s", p,
234 inet_ntop(AF_INET, 0, buf, PREFIX_STRLEN));
235 }
236
237 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
238 }
239
240 void eigrp_zebra_route_delete(struct eigrp *eigrp, struct prefix *p)
241 {
242 struct zapi_route api;
243
244 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
245 return;
246
247 memset(&api, 0, sizeof(api));
248 api.vrf_id = eigrp->vrf_id;
249 api.type = ZEBRA_ROUTE_EIGRP;
250 api.safi = SAFI_UNICAST;
251 memcpy(&api.prefix, p, sizeof(*p));
252 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
253
254 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
255 zlog_debug("Zebra: Route del %pFX", p);
256
257 return;
258 }
259
260 static int eigrp_is_type_redistributed(int type, vrf_id_t vrf_id)
261 {
262 return ((DEFAULT_ROUTE_TYPE(type))
263 ? vrf_bitmap_check(zclient->default_information[AFI_IP],
264 vrf_id)
265 : vrf_bitmap_check(zclient->redist[AFI_IP][type],
266 vrf_id));
267 }
268
269 int eigrp_redistribute_set(struct eigrp *eigrp, int type,
270 struct eigrp_metrics metric)
271 {
272
273 if (eigrp_is_type_redistributed(type, eigrp->vrf_id)) {
274 if (eigrp_metrics_is_same(metric, eigrp->dmetric[type])) {
275 eigrp->dmetric[type] = metric;
276 }
277
278 eigrp_external_routes_refresh(eigrp, type);
279
280 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
281 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
282 // Metric[%d]",
283 // eigrp_redist_string(type),
284 // metric_type (eigrp, type), metric_value
285 // (eigrp, type));
286 return CMD_SUCCESS;
287 }
288
289 eigrp->dmetric[type] = metric;
290
291 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, 0,
292 eigrp->vrf_id);
293
294 ++eigrp->redistribute;
295
296 return CMD_SUCCESS;
297 }
298
299 int eigrp_redistribute_unset(struct eigrp *eigrp, int type)
300 {
301
302 if (eigrp_is_type_redistributed(type, eigrp->vrf_id)) {
303 memset(&eigrp->dmetric[type], 0, sizeof(struct eigrp_metrics));
304 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP,
305 type, 0, eigrp->vrf_id);
306 --eigrp->redistribute;
307 }
308
309 return CMD_SUCCESS;
310 }