1 /* RIPd and zebra interface.
2 * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "ripd/ripd.h"
32 #include "ripd/rip_debug.h"
33 #include "ripd/rip_interface.h"
35 /* All information about zebra. */
36 struct zclient
*zclient
= NULL
;
38 /* Send ECMP routes to zebra. */
39 static void rip_zebra_ipv4_send(struct route_node
*rp
, uint8_t cmd
)
41 struct list
*list
= (struct list
*)rp
->info
;
42 struct zapi_route api
;
43 struct zapi_nexthop
*api_nh
;
44 struct listnode
*listnode
= NULL
;
45 struct rip_info
*rinfo
= NULL
;
48 memset(&api
, 0, sizeof(api
));
49 api
.vrf_id
= VRF_DEFAULT
;
50 api
.type
= ZEBRA_ROUTE_RIP
;
51 api
.safi
= SAFI_UNICAST
;
53 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
54 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
55 if (count
>= MULTIPATH_NUM
)
57 api_nh
= &api
.nexthops
[count
];
58 api_nh
->vrf_id
= VRF_DEFAULT
;
59 api_nh
->gate
= rinfo
->nh
.gate
;
60 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
61 if (cmd
== ZEBRA_ROUTE_ADD
)
62 SET_FLAG(rinfo
->flags
, RIP_RTF_FIB
);
64 UNSET_FLAG(rinfo
->flags
, RIP_RTF_FIB
);
69 api
.nexthop_num
= count
;
71 rinfo
= listgetdata(listhead(list
));
73 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
74 api
.metric
= rinfo
->metric
;
76 if (rinfo
->distance
&& rinfo
->distance
!= ZEBRA_RIP_DISTANCE_DEFAULT
) {
77 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
78 api
.distance
= rinfo
->distance
;
82 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
86 zclient_route_send(cmd
, zclient
, &api
);
88 if (IS_RIP_DEBUG_ZEBRA
) {
90 zlog_debug("%s: %s/%d nexthops %d",
91 (cmd
== ZEBRA_ROUTE_ADD
)
92 ? "Install into zebra"
93 : "Delete from zebra",
94 inet_ntoa(rp
->p
.u
.prefix4
), rp
->p
.prefixlen
,
97 zlog_debug("%s: %s/%d",
98 (cmd
== ZEBRA_ROUTE_ADD
)
99 ? "Install into zebra"
100 : "Delete from zebra",
101 inet_ntoa(rp
->p
.u
.prefix4
), rp
->p
.prefixlen
);
104 rip_global_route_changes
++;
107 /* Add/update ECMP routes to zebra. */
108 void rip_zebra_ipv4_add(struct route_node
*rp
)
110 rip_zebra_ipv4_send(rp
, ZEBRA_ROUTE_ADD
);
113 /* Delete ECMP routes from zebra. */
114 void rip_zebra_ipv4_delete(struct route_node
*rp
)
116 rip_zebra_ipv4_send(rp
, ZEBRA_ROUTE_DELETE
);
119 /* Zebra route add and delete treatment. */
120 static int rip_zebra_read_route(int command
, struct zclient
*zclient
,
121 zebra_size_t length
, vrf_id_t vrf_id
)
123 struct zapi_route api
;
129 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
132 memset(&nh
, 0, sizeof(nh
));
133 nh
.type
= api
.nexthops
[0].type
;
134 nh
.gate
.ipv4
= api
.nexthops
[0].gate
.ipv4
;
135 nh
.ifindex
= api
.nexthops
[0].ifindex
;
137 /* Then fetch IPv4 prefixes. */
138 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
139 rip_redistribute_add(api
.type
, RIP_ROUTE_REDISTRIBUTE
,
140 (struct prefix_ipv4
*)&api
.prefix
, &nh
,
141 api
.metric
, api
.distance
, api
.tag
);
142 else if (command
== ZEBRA_REDISTRIBUTE_ROUTE_DEL
)
143 rip_redistribute_delete(api
.type
, RIP_ROUTE_REDISTRIBUTE
,
144 (struct prefix_ipv4
*)&api
.prefix
,
150 void rip_zclient_reset(void)
152 zclient_reset(zclient
);
155 void rip_redistribute_conf_update(int type
)
157 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
,
161 void rip_redistribute_conf_delete(int type
)
163 if (zclient
->sock
> 0)
164 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
165 AFI_IP
, type
, 0, VRF_DEFAULT
);
167 /* Remove the routes from RIP table. */
168 rip_redistribute_withdraw(type
);
171 int rip_redistribute_check(int type
)
173 return vrf_bitmap_check(zclient
->redist
[AFI_IP
][type
], VRF_DEFAULT
);
176 void rip_redistribute_clean(void)
178 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
179 if (!vrf_bitmap_check(zclient
->redist
[AFI_IP
][i
], VRF_DEFAULT
))
182 if (zclient
->sock
> 0)
183 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
,
184 zclient
, AFI_IP
, i
, 0,
187 vrf_bitmap_unset(zclient
->redist
[AFI_IP
][i
], VRF_DEFAULT
);
189 /* Remove the routes from RIP table. */
190 rip_redistribute_withdraw(i
);
194 void rip_show_redistribute_config(struct vty
*vty
)
196 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
197 if (i
== zclient
->redist_default
198 || !vrf_bitmap_check(zclient
->redist
[AFI_IP
][i
],
202 vty_out(vty
, " %s", zebra_route_string(i
));
206 static void rip_zebra_connected(struct zclient
*zclient
)
208 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
211 void rip_zclient_init(struct thread_master
*master
)
213 /* Set default value to the zebra client structure. */
214 zclient
= zclient_new(master
, &zclient_options_default
);
215 zclient_init(zclient
, ZEBRA_ROUTE_RIP
, 0, &ripd_privs
);
216 zclient
->zebra_connected
= rip_zebra_connected
;
217 zclient
->interface_add
= rip_interface_add
;
218 zclient
->interface_delete
= rip_interface_delete
;
219 zclient
->interface_address_add
= rip_interface_address_add
;
220 zclient
->interface_address_delete
= rip_interface_address_delete
;
221 zclient
->interface_up
= rip_interface_up
;
222 zclient
->interface_down
= rip_interface_down
;
223 zclient
->redistribute_route_add
= rip_zebra_read_route
;
224 zclient
->redistribute_route_del
= rip_zebra_read_route
;
227 void rip_zclient_stop(void)
229 zclient_stop(zclient
);
230 zclient_free(zclient
);