2 * Zebra connect library for EIGRP.
3 * Copyright (C) 2013-2014
11 * This file is part of GNU Zebra.
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
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.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU Zebra; see the file COPYING. If not, write to the Free
25 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
45 #include "eigrpd/eigrp_structs.h"
46 #include "eigrpd/eigrpd.h"
47 #include "eigrpd/eigrp_interface.h"
48 #include "eigrpd/eigrp_neighbor.h"
49 #include "eigrpd/eigrp_packet.h"
50 #include "eigrpd/eigrp_zebra.h"
51 #include "eigrpd/eigrp_vty.h"
52 #include "eigrpd/eigrp_dump.h"
53 #include "eigrpd/eigrp_network.h"
54 #include "eigrpd/eigrp_topology.h"
55 #include "eigrpd/eigrp_fsm.h"
57 static int eigrp_interface_add (int , struct zclient
*, zebra_size_t
, vrf_id_t
);
58 static int eigrp_interface_delete (int , struct zclient
*,
59 zebra_size_t
, vrf_id_t
);
60 static int eigrp_interface_address_add (int, struct zclient
*,
61 zebra_size_t
, vrf_id_t vrf_id
);
62 static int eigrp_interface_address_delete (int, struct zclient
*,
63 zebra_size_t
, vrf_id_t vrf_id
);
64 static int eigrp_interface_state_up (int, struct zclient
*,
65 zebra_size_t
, vrf_id_t vrf_id
);
66 static int eigrp_interface_state_down (int, struct zclient
*,
67 zebra_size_t
, vrf_id_t vrf_id
);
68 static struct interface
* zebra_interface_if_lookup (struct stream
*);
70 static int eigrp_zebra_read_ipv4 (int , struct zclient
*,
71 zebra_size_t
, vrf_id_t vrf_id
);
73 /* Zebra structure to hold current status. */
74 struct zclient
*zclient
= NULL
;
76 /* For registering threads. */
77 extern struct thread_master
*master
;
78 struct in_addr router_id_zebra
;
80 /* Router-id update message from zebra. */
82 eigrp_router_id_update_zebra (int command
, struct zclient
*zclient
,
83 zebra_size_t length
, vrf_id_t vrf_id
)
86 struct prefix router_id
;
87 zebra_router_id_update_read (zclient
->ibuf
,&router_id
);
89 router_id_zebra
= router_id
.u
.prefix4
;
91 eigrp
= eigrp_lookup ();
94 eigrp_router_id_update (eigrp
);
100 eigrp_zebra_connected (struct zclient
*zclient
)
102 zclient_send_reg_requests (zclient
, VRF_DEFAULT
);
106 eigrp_zebra_init (void)
108 zclient
= zclient_new (master
);
110 zclient_init (zclient
, ZEBRA_ROUTE_EIGRP
, 0);
111 zclient
->zebra_connected
= eigrp_zebra_connected
;
112 zclient
->router_id_update
= eigrp_router_id_update_zebra
;
113 zclient
->interface_add
= eigrp_interface_add
;
114 zclient
->interface_delete
= eigrp_interface_delete
;
115 zclient
->interface_up
= eigrp_interface_state_up
;
116 zclient
->interface_down
= eigrp_interface_state_down
;
117 zclient
->interface_address_add
= eigrp_interface_address_add
;
118 zclient
->interface_address_delete
= eigrp_interface_address_delete
;
119 zclient
->redistribute_route_ipv4_add
= eigrp_zebra_read_ipv4
;
120 zclient
->redistribute_route_ipv4_del
= eigrp_zebra_read_ipv4
;
124 /* Zebra route add and delete treatment. */
126 eigrp_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
127 zebra_size_t length
, vrf_id_t vrf_id
)
130 struct zapi_ipv4 api
;
131 struct prefix_ipv4 p
;
136 /* Type, flags, message. */
137 api
.type
= stream_getc (s
);
138 api
.instance
= stream_getw (s
);
139 api
.flags
= stream_getc (s
);
140 api
.message
= stream_getc (s
);
143 memset (&p
, 0, sizeof (struct prefix_ipv4
));
145 p
.prefixlen
= stream_getc (s
);
146 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
148 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
151 /* Nexthop, ifindex, distance, metric. */
152 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
154 api
.nexthop_num
= stream_getc (s
);
157 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
159 api
.ifindex_num
= stream_getc (s
);
160 /* XXX assert(api.ifindex_num == 1); */
161 stream_getl (s
); /* ifindex, unused */
163 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_DISTANCE
))
164 api
.distance
= stream_getc (s
);
165 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_METRIC
))
166 api
.metric
= stream_getl (s
);
168 eigrp
= eigrp_lookup ();
172 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
176 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
184 /* Inteface addition message from zebra. */
186 eigrp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
189 struct interface
*ifp
;
191 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
195 if (!EIGRP_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
197 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
198 IF_DEF_PARAMS (ifp
)->type
= eigrp_default_iftype (ifp
);
201 eigrp_if_update (ifp
);
207 eigrp_interface_delete (int command
, struct zclient
*zclient
,
208 zebra_size_t length
, vrf_id_t vrf_id
)
210 struct interface
*ifp
;
212 struct route_node
*rn
;
215 /* zebra_interface_state_read () updates interface structure in iflist */
216 ifp
= zebra_interface_state_read (s
, vrf_id
);
222 zlog_warn ("Zebra: got delete of %s, but interface is still up",
225 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
226 zlog_debug("Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
227 ifp
->name
, ifp
->ifindex
, (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
229 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
231 eigrp_if_free ((struct eigrp_interface
*) rn
->info
, INTERFACE_DOWN_BY_ZEBRA
);
233 ifp
->ifindex
= IFINDEX_INTERNAL
;
238 eigrp_interface_address_add (int command
, struct zclient
*zclient
,
239 zebra_size_t length
, vrf_id_t vrf_id
)
243 c
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
248 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
251 prefix2str (c
->address
, buf
, sizeof (buf
));
252 zlog_debug ("Zebra: interface %s address add %s", c
->ifp
->name
, buf
);
255 eigrp_if_update (c
->ifp
);
261 eigrp_interface_address_delete (int command
, struct zclient
*zclient
,
262 zebra_size_t length
, vrf_id_t vrf_id
)
265 struct interface
*ifp
;
266 struct eigrp_interface
*ei
;
267 struct route_node
*rn
;
270 c
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
275 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
278 prefix2str (c
->address
, buf
, sizeof (buf
));
279 zlog_debug ("Zebra: interface %s address delete %s", c
->ifp
->name
, buf
);
284 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
286 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
296 /* Call interface hook functions to clean up */
297 eigrp_if_free (ei
, INTERFACE_DOWN_BY_ZEBRA
);
305 eigrp_interface_state_up (int command
, struct zclient
*zclient
,
306 zebra_size_t length
, vrf_id_t vrf_id
)
308 struct interface
*ifp
;
309 struct eigrp_interface
*ei
;
310 struct route_node
*rn
;
312 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
317 /* Interface is already up. */
318 if (if_is_operative (ifp
))
320 /* Temporarily keep ifp values. */
321 struct interface if_tmp
;
322 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
324 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
326 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
327 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
329 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
331 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
332 zlog_debug ("Zebra: Interface[%s] bandwidth change %d -> %d.",
333 ifp
->name
, if_tmp
.bandwidth
, ifp
->bandwidth
);
335 // eigrp_if_recalculate_output_cost (ifp);
338 if (if_tmp
.mtu
!= ifp
->mtu
)
340 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
341 zlog_debug ("Zebra: Interface[%s] MTU change %u -> %u.",
342 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
344 /* Must reset the interface (simulate down/up) when MTU changes. */
345 eigrp_if_reset (ifp
);
350 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
352 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
353 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp
->name
);
355 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
357 if ((ei
= rn
->info
) == NULL
)
367 eigrp_interface_state_down (int command
, struct zclient
*zclient
,
368 zebra_size_t length
, vrf_id_t vrf_id
)
370 struct interface
*ifp
;
371 struct eigrp_interface
*ei
;
372 struct route_node
*node
;
374 ifp
= zebra_interface_state_read (zclient
->ibuf
, vrf_id
);
379 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
380 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
382 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
384 if ((ei
= node
->info
) == NULL
)
392 static struct interface
*
393 zebra_interface_if_lookup (struct stream
*s
)
395 char ifname_tmp
[INTERFACE_NAMSIZ
];
397 /* Read interface name. */
398 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
400 /* And look it up. */
401 return if_lookup_by_name_len (ifname_tmp
,
402 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
407 eigrp_zebra_route_add (struct prefix_ipv4
*p
, struct list
*successors
)
409 struct eigrp_neighbor_entry
*te
;
410 struct listnode
*node
;
416 if (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
421 /* EIGRP pass nexthop and metric */
422 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
428 /* Put command, type, flags, message. */
429 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_ADD
, VRF_DEFAULT
);
430 stream_putc (s
, ZEBRA_ROUTE_EIGRP
);
432 stream_putl (s
, flags
);
433 stream_putc (s
, message
);
434 stream_putw (s
, SAFI_UNICAST
);
436 /* Put prefix information. */
437 psize
= PSIZE (p
->prefixlen
);
438 stream_putc (s
, p
->prefixlen
);
439 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
442 stream_putc (s
, successors
->count
);
444 /* Nexthop, ifindex, distance and metric information. */
445 for (ALL_LIST_ELEMENTS_RO (successors
, node
, te
))
447 stream_putc (s
, NEXTHOP_TYPE_IPV4_IFINDEX
);
448 stream_put_in_addr (s
, &te
->adv_router
->src
);
449 stream_putl (s
, te
->ei
->ifp
->ifindex
);
452 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_REDISTRIBUTE
))
454 char buf
[2][INET_ADDRSTRLEN
];
455 zlog_debug ("Zebra: Route add %s/%d nexthop %s",
456 inet_ntop(AF_INET
, &p
->prefix
, buf
[0], sizeof (buf
[0])),
458 inet_ntop(AF_INET
, 0 /*&p->nexthop*/, buf
[1], sizeof (buf
[1])));
461 stream_putw_at (s
, 0, stream_get_endp (s
));
463 zclient_send_message (zclient
);
468 eigrp_zebra_route_delete (struct prefix_ipv4
*p
)
470 struct zapi_ipv4 api
;
472 if (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
474 api
.vrf_id
= VRF_DEFAULT
;
475 api
.type
= ZEBRA_ROUTE_EIGRP
;
479 api
.safi
= SAFI_UNICAST
;
480 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
482 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_REDISTRIBUTE
))
484 char buf
[2][INET_ADDRSTRLEN
];
485 zlog_debug ("Zebra: Route del %s/%d nexthop %s",
486 inet_ntop (AF_INET
, &p
->prefix
, buf
[0], sizeof (buf
[0])),
488 inet_ntop (AF_INET
, 0 /*&p->nexthop*/, buf
[1], sizeof (buf
[1])));
496 eigrp_is_type_redistributed (int type
)
498 return (DEFAULT_ROUTE_TYPE (type
)) ?
499 zclient
->default_information
: zclient
->redist
[AFI_IP
][type
];
503 eigrp_redistribute_set (struct eigrp
*eigrp
, int type
, struct eigrp_metrics metric
)
506 if (eigrp_is_type_redistributed (type
))
508 if (eigrp_metrics_is_same(&metric
, &eigrp
->dmetric
[type
]))
510 eigrp
->dmetric
[type
] = metric
;
513 eigrp_external_routes_refresh (eigrp
, type
);
515 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
516 // zlog_debug ("Redistribute[%s]: Refresh Type[%d], Metric[%d]",
517 // eigrp_redist_string(type),
518 // metric_type (eigrp, type), metric_value (eigrp, type));
522 eigrp
->dmetric
[type
] = metric
;
524 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
,
525 AFI_IP
, type
, 0, VRF_DEFAULT
);
527 // if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
528 // zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
529 // ospf_redist_string(type),
530 // metric_type (ospf, type), metric_value (ospf, type));
532 ++eigrp
->redistribute
;
538 eigrp_redistribute_unset (struct eigrp
*eigrp
, int type
)
541 if (eigrp_is_type_redistributed (type
))
543 memset(&eigrp
->dmetric
[type
], 0, sizeof(struct eigrp_metrics
));
544 zclient_redistribute (ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
545 AFI_IP
, type
, 0, VRF_DEFAULT
);
546 --eigrp
->redistribute
;
549 // if (IS_DEBUG_EIGRP (zebra, ZEBRA_REDISTRIBUTE))
550 // zlog_debug ("Redistribute[%s]: Start Type[%d], Metric[%d]",
551 // ospf_redist_string(type),
552 // metric_type (ospf, type), metric_value (ospf, type));