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 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
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"
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
*);
69 static int eigrp_zebra_read_ipv4 (int , struct zclient
*,
70 zebra_size_t
, vrf_id_t vrf_id
);
72 /* Zebra structure to hold current status. */
73 struct zclient
*zclient
= NULL
;
75 /* For registering threads. */
76 extern struct thread_master
*master
;
77 struct in_addr router_id_zebra
;
79 /* Router-id update message from zebra. */
81 eigrp_router_id_update_zebra (int command
, struct zclient
*zclient
,
82 zebra_size_t length
, vrf_id_t vrf_id
)
85 struct prefix router_id
;
86 zebra_router_id_update_read (zclient
->ibuf
,&router_id
);
88 router_id_zebra
= router_id
.u
.prefix4
;
90 eigrp
= eigrp_lookup ();
93 eigrp_router_id_update (eigrp
);
99 eigrp_zebra_connected (struct zclient
*zclient
)
101 zclient_send_reg_requests (zclient
, VRF_DEFAULT
);
105 eigrp_zebra_init (void)
107 zclient
= zclient_new (master
);
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
;
123 /* Zebra route add and delete treatment. */
125 eigrp_zebra_read_ipv4 (int command
, struct zclient
*zclient
,
126 zebra_size_t length
, vrf_id_t vrf_id
)
129 struct zapi_ipv4 api
;
130 struct prefix_ipv4 p
;
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
);
142 memset (&p
, 0, sizeof (struct prefix_ipv4
));
144 p
.prefixlen
= stream_getc (s
);
145 stream_get (&p
.prefix
, s
, PSIZE (p
.prefixlen
));
147 if (IPV4_NET127(ntohl(p
.prefix
.s_addr
)))
150 /* Nexthop, ifindex, distance, metric. */
151 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_NEXTHOP
))
153 api
.nexthop_num
= stream_getc (s
);
156 if (CHECK_FLAG (api
.message
, ZAPI_MESSAGE_IFINDEX
))
158 api
.ifindex_num
= stream_getc (s
);
159 /* XXX assert(api.ifindex_num == 1); */
160 stream_getl (s
); /* ifindex, unused */
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
);
167 eigrp
= eigrp_lookup ();
171 if (command
== ZEBRA_IPV4_ROUTE_ADD
)
175 else /* if (command == ZEBRA_IPV4_ROUTE_DELETE) */
183 /* Inteface addition message from zebra. */
185 eigrp_interface_add (int command
, struct zclient
*zclient
, zebra_size_t length
,
188 struct interface
*ifp
;
190 ifp
= zebra_interface_add_read (zclient
->ibuf
, vrf_id
);
194 if (!EIGRP_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (ifp
), type
))
196 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), type
);
197 IF_DEF_PARAMS (ifp
)->type
= eigrp_default_iftype (ifp
);
200 eigrp_if_update (ifp
);
206 eigrp_interface_delete (int command
, struct zclient
*zclient
,
207 zebra_size_t length
, vrf_id_t vrf_id
)
209 struct interface
*ifp
;
211 struct route_node
*rn
;
214 /* zebra_interface_state_read () updates interface structure in iflist */
215 ifp
= zebra_interface_state_read (s
, vrf_id
);
221 zlog_warn ("Zebra: got delete of %s, but interface is still up",
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
);
228 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
230 eigrp_if_free ((struct eigrp_interface
*) rn
->info
, INTERFACE_DOWN_BY_ZEBRA
);
232 ifp
->ifindex
= IFINDEX_INTERNAL
;
237 eigrp_interface_address_add (int command
, struct zclient
*zclient
,
238 zebra_size_t length
, vrf_id_t vrf_id
)
242 c
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
247 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
250 prefix2str (c
->address
, buf
, sizeof (buf
));
251 zlog_debug ("Zebra: interface %s address add %s", c
->ifp
->name
, buf
);
254 eigrp_if_update (c
->ifp
);
260 eigrp_interface_address_delete (int command
, struct zclient
*zclient
,
261 zebra_size_t length
, vrf_id_t vrf_id
)
264 struct interface
*ifp
;
265 struct eigrp_interface
*ei
;
266 struct route_node
*rn
;
269 c
= zebra_interface_address_read (command
, zclient
->ibuf
, vrf_id
);
274 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
277 prefix2str (c
->address
, buf
, sizeof (buf
));
278 zlog_debug ("Zebra: interface %s address delete %s", c
->ifp
->name
, buf
);
283 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
285 rn
= route_node_lookup (IF_OIFS (ifp
), &p
);
295 /* Call interface hook functions to clean up */
296 eigrp_if_free (ei
, INTERFACE_DOWN_BY_ZEBRA
);
304 eigrp_interface_state_up (int command
, struct zclient
*zclient
,
305 zebra_size_t length
, vrf_id_t vrf_id
)
307 struct interface
*ifp
;
308 struct eigrp_interface
*ei
;
309 struct route_node
*rn
;
311 ifp
= zebra_interface_if_lookup (zclient
->ibuf
);
316 /* Interface is already up. */
317 if (if_is_operative (ifp
))
319 /* Temporarily keep ifp values. */
320 struct interface if_tmp
;
321 memcpy (&if_tmp
, ifp
, sizeof (struct interface
));
323 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
325 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
326 zlog_debug ("Zebra: Interface[%s] state update.", ifp
->name
);
328 if (if_tmp
.bandwidth
!= ifp
->bandwidth
)
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
);
334 // eigrp_if_recalculate_output_cost (ifp);
337 if (if_tmp
.mtu
!= ifp
->mtu
)
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
);
343 /* Must reset the interface (simulate down/up) when MTU changes. */
344 eigrp_if_reset (ifp
);
349 zebra_interface_if_set_value (zclient
->ibuf
, ifp
);
351 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
352 zlog_debug ("Zebra: Interface[%s] state change to up.", ifp
->name
);
354 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
356 if ((ei
= rn
->info
) == NULL
)
366 eigrp_interface_state_down (int command
, struct zclient
*zclient
,
367 zebra_size_t length
, vrf_id_t vrf_id
)
369 struct interface
*ifp
;
370 struct eigrp_interface
*ei
;
371 struct route_node
*node
;
373 ifp
= zebra_interface_state_read (zclient
->ibuf
, vrf_id
);
378 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_INTERFACE
))
379 zlog_debug ("Zebra: Interface[%s] state change to down.", ifp
->name
);
381 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
383 if ((ei
= node
->info
) == NULL
)
391 static struct interface
*
392 zebra_interface_if_lookup (struct stream
*s
)
394 char ifname_tmp
[INTERFACE_NAMSIZ
];
396 /* Read interface name. */
397 stream_get (ifname_tmp
, s
, INTERFACE_NAMSIZ
);
399 /* And look it up. */
400 return if_lookup_by_name_len (ifname_tmp
,
401 strnlen (ifname_tmp
, INTERFACE_NAMSIZ
),
406 eigrp_zebra_route_add (struct prefix_ipv4
*p
, struct list
*successors
)
408 struct eigrp_neighbor_entry
*te
;
409 struct listnode
*node
;
415 if (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
420 /* EIGRP pass nexthop and metric */
421 SET_FLAG (message
, ZAPI_MESSAGE_NEXTHOP
);
427 /* Put command, type, flags, message. */
428 zclient_create_header (s
, ZEBRA_IPV4_ROUTE_ADD
, VRF_DEFAULT
);
429 stream_putc (s
, ZEBRA_ROUTE_EIGRP
);
431 stream_putl (s
, flags
);
432 stream_putc (s
, message
);
433 stream_putw (s
, SAFI_UNICAST
);
435 /* Put prefix information. */
436 psize
= PSIZE (p
->prefixlen
);
437 stream_putc (s
, p
->prefixlen
);
438 stream_write (s
, (u_char
*) & p
->prefix
, psize
);
441 stream_putc (s
, successors
->count
);
443 /* Nexthop, ifindex, distance and metric information. */
444 for (ALL_LIST_ELEMENTS_RO (successors
, node
, te
))
446 if (te
->adv_router
->src
.s_addr
)
448 stream_putc (s
, NEXTHOP_TYPE_IPV4_IFINDEX
);
449 stream_put_in_addr (s
, &te
->adv_router
->src
);
452 stream_putc (s
, NEXTHOP_TYPE_IFINDEX
);
453 stream_putl (s
, te
->ei
->ifp
->ifindex
);
456 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_REDISTRIBUTE
))
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])),
462 inet_ntop(AF_INET
, 0 /*&p->nexthop*/, buf
[1], sizeof (buf
[1])));
465 stream_putw_at (s
, 0, stream_get_endp (s
));
467 zclient_send_message (zclient
);
472 eigrp_zebra_route_delete (struct prefix_ipv4
*p
)
474 struct zapi_ipv4 api
;
476 if (zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
478 api
.vrf_id
= VRF_DEFAULT
;
479 api
.type
= ZEBRA_ROUTE_EIGRP
;
483 api
.safi
= SAFI_UNICAST
;
484 zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE
, zclient
, p
, &api
);
486 if (IS_DEBUG_EIGRP (zebra
, ZEBRA_REDISTRIBUTE
))
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])),
492 inet_ntop (AF_INET
, 0 /*&p->nexthop*/, buf
[1], sizeof (buf
[1])));
500 eigrp_is_type_redistributed (int type
)
502 return (DEFAULT_ROUTE_TYPE (type
)) ?
503 zclient
->default_information
: zclient
->redist
[AFI_IP
][type
];
507 eigrp_redistribute_set (struct eigrp
*eigrp
, int type
, struct eigrp_metrics metric
)
510 if (eigrp_is_type_redistributed (type
))
512 if (eigrp_metrics_is_same(metric
, eigrp
->dmetric
[type
]))
514 eigrp
->dmetric
[type
] = metric
;
517 eigrp_external_routes_refresh (eigrp
, type
);
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));
526 eigrp
->dmetric
[type
] = metric
;
528 zclient_redistribute (ZEBRA_REDISTRIBUTE_ADD
, zclient
,
529 AFI_IP
, type
, 0, VRF_DEFAULT
);
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));
536 ++eigrp
->redistribute
;
542 eigrp_redistribute_unset (struct eigrp
*eigrp
, int type
)
545 if (eigrp_is_type_redistributed (type
))
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
;
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));