]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_zebra.c
*: make consistent & update GPLv2 file headers
[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_add (int , struct zclient *, zebra_size_t, vrf_id_t);
57 static int eigrp_interface_delete (int , struct zclient *,
58 zebra_size_t, vrf_id_t);
59 static int eigrp_interface_address_add (int, struct zclient *,
60 zebra_size_t, vrf_id_t vrf_id);
61 static int eigrp_interface_address_delete (int, struct zclient *,
62 zebra_size_t, vrf_id_t vrf_id);
63 static int eigrp_interface_state_up (int, struct zclient *,
64 zebra_size_t, vrf_id_t vrf_id);
65 static int eigrp_interface_state_down (int, struct zclient *,
66 zebra_size_t, vrf_id_t vrf_id);
67 static struct interface * zebra_interface_if_lookup (struct stream *);
68
69 static int eigrp_zebra_read_ipv4 (int , struct zclient *,
70 zebra_size_t, vrf_id_t vrf_id);
71
72 /* Zebra structure to hold current status. */
73 struct zclient *zclient = NULL;
74
75 /* For registering threads. */
76 extern struct thread_master *master;
77 struct in_addr router_id_zebra;
78
79 /* Router-id update message from zebra. */
80 static int
81 eigrp_router_id_update_zebra (int command, struct zclient *zclient,
82 zebra_size_t length, vrf_id_t vrf_id)
83 {
84 struct eigrp *eigrp;
85 struct prefix router_id;
86 zebra_router_id_update_read (zclient->ibuf,&router_id);
87
88 router_id_zebra = router_id.u.prefix4;
89
90 eigrp = eigrp_lookup ();
91
92 if (eigrp != NULL)
93 eigrp_router_id_update (eigrp);
94
95 return 0;
96 }
97
98 static void
99 eigrp_zebra_connected (struct zclient *zclient)
100 {
101 zclient_send_reg_requests (zclient, VRF_DEFAULT);
102 }
103
104 void
105 eigrp_zebra_init (void)
106 {
107 zclient = zclient_new (master);
108
109 zclient_init (zclient, ZEBRA_ROUTE_EIGRP, 0);
110 zclient->zebra_connected = eigrp_zebra_connected;
111 zclient->router_id_update = eigrp_router_id_update_zebra;
112 zclient->interface_add = eigrp_interface_add;
113 zclient->interface_delete = eigrp_interface_delete;
114 zclient->interface_up = eigrp_interface_state_up;
115 zclient->interface_down = eigrp_interface_state_down;
116 zclient->interface_address_add = eigrp_interface_address_add;
117 zclient->interface_address_delete = eigrp_interface_address_delete;
118 zclient->redistribute_route_ipv4_add = eigrp_zebra_read_ipv4;
119 zclient->redistribute_route_ipv4_del = eigrp_zebra_read_ipv4;
120 }
121
122
123 /* Zebra route add and delete treatment. */
124 static int
125 eigrp_zebra_read_ipv4 (int command, struct zclient *zclient,
126 zebra_size_t length, vrf_id_t vrf_id)
127 {
128 struct stream *s;
129 struct zapi_ipv4 api;
130 struct prefix_ipv4 p;
131 struct eigrp *eigrp;
132
133 s = zclient->ibuf;
134
135 /* Type, flags, message. */
136 api.type = stream_getc (s);
137 api.instance = stream_getw (s);
138 api.flags = stream_getc (s);
139 api.message = stream_getc (s);
140
141 /* IPv4 prefix. */
142 memset (&p, 0, sizeof (struct prefix_ipv4));
143 p.family = AF_INET;
144 p.prefixlen = stream_getc (s);
145 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
146
147 if (IPV4_NET127(ntohl(p.prefix.s_addr)))
148 return 0;
149
150 /* Nexthop, ifindex, distance, metric. */
151 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
152 {
153 api.nexthop_num = stream_getc (s);
154 stream_get_ipv4 (s);
155 }
156 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
157 {
158 api.ifindex_num = stream_getc (s);
159 /* XXX assert(api.ifindex_num == 1); */
160 stream_getl (s); /* ifindex, unused */
161 }
162 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
163 api.distance = stream_getc (s);
164 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
165 api.metric = stream_getl (s);
166
167 eigrp = eigrp_lookup ();
168 if (eigrp == NULL)
169 return 0;
170
171 if (command == ZEBRA_IPV4_ROUTE_ADD)
172 {
173
174 }
175 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
176 {
177
178 }
179
180 return 0;
181 }
182
183 /* Inteface addition message from zebra. */
184 static int
185 eigrp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
186 vrf_id_t vrf_id)
187 {
188 struct interface *ifp;
189
190 ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
191
192 assert (ifp->info);
193
194 if (!EIGRP_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp), type))
195 {
196 SET_IF_PARAM (IF_DEF_PARAMS (ifp), type);
197 IF_DEF_PARAMS (ifp)->type = eigrp_default_iftype (ifp);
198 }
199
200 eigrp_if_update (ifp);
201
202 return 0;
203 }
204
205 static int
206 eigrp_interface_delete (int command, struct zclient *zclient,
207 zebra_size_t length, vrf_id_t vrf_id)
208 {
209 struct interface *ifp;
210 struct stream *s;
211 struct route_node *rn;
212
213 s = zclient->ibuf;
214 /* zebra_interface_state_read () updates interface structure in iflist */
215 ifp = zebra_interface_state_read (s, vrf_id);
216
217 if (ifp == NULL)
218 return 0;
219
220 if (if_is_up (ifp))
221 zlog_warn ("Zebra: got delete of %s, but interface is still up",
222 ifp->name);
223
224 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
225 zlog_debug("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
226 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags, ifp->metric, ifp->mtu);
227
228 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
229 if (rn->info)
230 eigrp_if_free ((struct eigrp_interface *) rn->info, INTERFACE_DOWN_BY_ZEBRA);
231
232 ifp->ifindex = IFINDEX_INTERNAL;
233 return 0;
234 }
235
236 static int
237 eigrp_interface_address_add (int command, struct zclient *zclient,
238 zebra_size_t length, vrf_id_t vrf_id)
239 {
240 struct connected *c;
241
242 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
243
244 if (c == NULL)
245 return 0;
246
247 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
248 {
249 char buf[128];
250 prefix2str (c->address, buf, sizeof (buf));
251 zlog_debug ("Zebra: interface %s address add %s", c->ifp->name, buf);
252 }
253
254 eigrp_if_update (c->ifp);
255
256 return 0;
257 }
258
259 static int
260 eigrp_interface_address_delete (int command, struct zclient *zclient,
261 zebra_size_t length, vrf_id_t vrf_id)
262 {
263 struct connected *c;
264 struct interface *ifp;
265 struct eigrp_interface *ei;
266 struct route_node *rn;
267 struct prefix p;
268
269 c = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
270
271 if (c == NULL)
272 return 0;
273
274 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
275 {
276 char buf[128];
277 prefix2str (c->address, buf, sizeof (buf));
278 zlog_debug ("Zebra: interface %s address delete %s", c->ifp->name, buf);
279 }
280
281 ifp = c->ifp;
282 p = *c->address;
283 p.prefixlen = IPV4_MAX_PREFIXLEN;
284
285 rn = route_node_lookup (IF_OIFS (ifp), &p);
286 if (!rn)
287 {
288 connected_free (c);
289 return 0;
290 }
291
292 assert (rn->info);
293 ei = rn->info;
294
295 /* Call interface hook functions to clean up */
296 eigrp_if_free (ei, INTERFACE_DOWN_BY_ZEBRA);
297
298 connected_free (c);
299
300 return 0;
301 }
302
303 static int
304 eigrp_interface_state_up (int command, struct zclient *zclient,
305 zebra_size_t length, vrf_id_t vrf_id)
306 {
307 struct interface *ifp;
308 struct eigrp_interface *ei;
309 struct route_node *rn;
310
311 ifp = zebra_interface_if_lookup (zclient->ibuf);
312
313 if (ifp == NULL)
314 return 0;
315
316 /* Interface is already up. */
317 if (if_is_operative (ifp))
318 {
319 /* Temporarily keep ifp values. */
320 struct interface if_tmp;
321 memcpy (&if_tmp, ifp, sizeof (struct interface));
322
323 zebra_interface_if_set_value (zclient->ibuf, ifp);
324
325 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
326 zlog_debug ("Zebra: Interface[%s] state update.", ifp->name);
327
328 if (if_tmp.bandwidth != ifp->bandwidth)
329 {
330 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
331 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
332 ifp->name, if_tmp.bandwidth, ifp->bandwidth);
333
334 // eigrp_if_recalculate_output_cost (ifp);
335 }
336
337 if (if_tmp.mtu != ifp->mtu)
338 {
339 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
340 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
341 ifp->name, if_tmp.mtu, ifp->mtu);
342
343 /* Must reset the interface (simulate down/up) when MTU changes. */
344 eigrp_if_reset (ifp);
345 }
346 return 0;
347 }
348
349 zebra_interface_if_set_value (zclient->ibuf, ifp);
350
351 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
352 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp->name);
353
354 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
355 {
356 if ((ei = rn->info) == NULL)
357 continue;
358
359 eigrp_if_up (ei);
360 }
361
362 return 0;
363 }
364
365 static int
366 eigrp_interface_state_down (int command, struct zclient *zclient,
367 zebra_size_t length, vrf_id_t vrf_id)
368 {
369 struct interface *ifp;
370 struct eigrp_interface *ei;
371 struct route_node *node;
372
373 ifp = zebra_interface_state_read (zclient->ibuf, vrf_id);
374
375 if (ifp == NULL)
376 return 0;
377
378 if (IS_DEBUG_EIGRP (zebra, ZEBRA_INTERFACE))
379 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp->name);
380
381 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
382 {
383 if ((ei = node->info) == NULL)
384 continue;
385 eigrp_if_down (ei);
386 }
387
388 return 0;
389 }
390
391 static struct interface *
392 zebra_interface_if_lookup (struct stream *s)
393 {
394 char ifname_tmp[INTERFACE_NAMSIZ];
395
396 /* Read interface name. */
397 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
398
399 /* And look it up. */
400 return if_lookup_by_name_len (ifname_tmp,
401 strnlen (ifname_tmp, INTERFACE_NAMSIZ),
402 VRF_DEFAULT);
403 }
404
405 void
406 eigrp_zebra_route_add (struct prefix_ipv4 *p, struct list *successors)
407 {
408 struct eigrp_neighbor_entry *te;
409 struct listnode *node;
410 u_char message;
411 u_char flags;
412 int psize;
413 struct stream *s;
414
415 if (zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
416 {
417 message = 0;
418 flags = 0;
419
420 /* EIGRP pass nexthop and metric */
421 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
422
423 /* Make packet. */
424 s = zclient->obuf;
425 stream_reset (s);
426
427 /* Put command, type, flags, message. */
428 zclient_create_header (s, ZEBRA_IPV4_ROUTE_ADD, VRF_DEFAULT);
429 stream_putc (s, ZEBRA_ROUTE_EIGRP);
430 stream_putw (s, 0);
431 stream_putl (s, flags);
432 stream_putc (s, message);
433 stream_putw (s, SAFI_UNICAST);
434
435 /* Put prefix information. */
436 psize = PSIZE (p->prefixlen);
437 stream_putc (s, p->prefixlen);
438 stream_write (s, (u_char *) & p->prefix, psize);
439
440 /* Nexthop count. */
441 stream_putc (s, successors->count);
442
443 /* Nexthop, ifindex, distance and metric information. */
444 for (ALL_LIST_ELEMENTS_RO (successors, node, te))
445 {
446 if (te->adv_router->src.s_addr)
447 {
448 stream_putc (s, NEXTHOP_TYPE_IPV4_IFINDEX);
449 stream_put_in_addr (s, &te->adv_router->src);
450 }
451 else
452 stream_putc (s, NEXTHOP_TYPE_IFINDEX);
453 stream_putl (s, te->ei->ifp->ifindex);
454 }
455
456 if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
457 {
458 char buf[2][INET_ADDRSTRLEN];
459 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
460 inet_ntop(AF_INET, &p->prefix, buf[0], sizeof (buf[0])),
461 p->prefixlen,
462 inet_ntop(AF_INET, 0 /*&p->nexthop*/, buf[1], sizeof (buf[1])));
463 }
464
465 stream_putw_at (s, 0, stream_get_endp (s));
466
467 zclient_send_message (zclient);
468 }
469 }
470
471 void
472 eigrp_zebra_route_delete (struct prefix_ipv4 *p)
473 {
474 struct zapi_ipv4 api;
475
476 if (zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
477 {
478 api.vrf_id = VRF_DEFAULT;
479 api.type = ZEBRA_ROUTE_EIGRP;
480 api.instance = 0;
481 api.flags = 0;
482 api.message = 0;
483 api.safi = SAFI_UNICAST;
484 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, p, &api);
485
486 if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
487 {
488 char buf[2][INET_ADDRSTRLEN];
489 zlog_debug ("Zebra: Route del %s/%d nexthop %s",
490 inet_ntop (AF_INET, &p->prefix, buf[0], sizeof (buf[0])),
491 p->prefixlen,
492 inet_ntop (AF_INET, 0 /*&p->nexthop*/, buf[1], sizeof (buf[1])));
493 }
494 }
495
496 return;
497 }
498
499 vrf_bitmap_t
500 eigrp_is_type_redistributed (int type)
501 {
502 return (DEFAULT_ROUTE_TYPE (type)) ?
503 zclient->default_information : zclient->redist[AFI_IP][type];
504 }
505
506 int
507 eigrp_redistribute_set (struct eigrp *eigrp, int type, struct eigrp_metrics metric)
508 {
509
510 if (eigrp_is_type_redistributed (type))
511 {
512 if (eigrp_metrics_is_same(&metric, &eigrp->dmetric[type]))
513 {
514 eigrp->dmetric[type] = metric;
515 }
516
517 eigrp_external_routes_refresh (eigrp, type);
518
519 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
520 // zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
521 // eigrp_redist_string(type),
522 // metric_type (eigrp, type), metric_value (eigrp, type));
523 return CMD_SUCCESS;
524 }
525
526 eigrp->dmetric[type] = metric;
527
528 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD, zclient,
529 AFI_IP, type, 0, VRF_DEFAULT);
530
531 // if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
532 // zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
533 // ospf_redist_string(type),
534 // metric_type (ospf, type), metric_value (ospf, type));
535
536 ++eigrp->redistribute;
537
538 return CMD_SUCCESS;
539 }
540
541 int
542 eigrp_redistribute_unset (struct eigrp *eigrp, int type)
543 {
544
545 if (eigrp_is_type_redistributed (type))
546 {
547 memset(&eigrp->dmetric[type], 0, sizeof(struct eigrp_metrics));
548 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE, zclient,
549 AFI_IP, type, 0, VRF_DEFAULT);
550 --eigrp->redistribute;
551 }
552
553 // if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
554 // zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
555 // ospf_redist_string(type),
556 // metric_type (ospf, type), metric_value (ospf, type));
557
558 return CMD_SUCCESS;
559 }
560