]>
Commit | Line | Data |
---|---|---|
1 | /* RIPd and zebra interface. | |
2 | * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org> | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
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 | |
9 | * later version. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "prefix.h" | |
25 | #include "table.h" | |
26 | #include "stream.h" | |
27 | #include "memory.h" | |
28 | #include "zclient.h" | |
29 | #include "log.h" | |
30 | #include "vrf.h" | |
31 | #include "ripd/ripd.h" | |
32 | #include "ripd/rip_debug.h" | |
33 | #include "ripd/rip_interface.h" | |
34 | ||
35 | /* All information about zebra. */ | |
36 | struct zclient *zclient = NULL; | |
37 | ||
38 | /* Send ECMP routes to zebra. */ | |
39 | static void rip_zebra_ipv4_send(struct route_node *rp, uint8_t cmd) | |
40 | { | |
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; | |
46 | int count = 0; | |
47 | ||
48 | memset(&api, 0, sizeof(api)); | |
49 | api.vrf_id = VRF_DEFAULT; | |
50 | api.type = ZEBRA_ROUTE_RIP; | |
51 | api.safi = SAFI_UNICAST; | |
52 | ||
53 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
54 | for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) { | |
55 | if (count >= MULTIPATH_NUM) | |
56 | break; | |
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); | |
63 | else | |
64 | UNSET_FLAG(rinfo->flags, RIP_RTF_FIB); | |
65 | count++; | |
66 | } | |
67 | ||
68 | api.prefix = rp->p; | |
69 | api.nexthop_num = count; | |
70 | ||
71 | rinfo = listgetdata(listhead(list)); | |
72 | ||
73 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); | |
74 | api.metric = rinfo->metric; | |
75 | ||
76 | if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) { | |
77 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
78 | api.distance = rinfo->distance; | |
79 | } | |
80 | ||
81 | if (rinfo->tag) { | |
82 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
83 | api.tag = rinfo->tag; | |
84 | } | |
85 | ||
86 | zclient_route_send(cmd, zclient, &api); | |
87 | ||
88 | if (IS_RIP_DEBUG_ZEBRA) { | |
89 | if (rip->ecmp) | |
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, | |
95 | count); | |
96 | else | |
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); | |
102 | } | |
103 | ||
104 | rip_global_route_changes++; | |
105 | } | |
106 | ||
107 | /* Add/update ECMP routes to zebra. */ | |
108 | void rip_zebra_ipv4_add(struct route_node *rp) | |
109 | { | |
110 | rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_ADD); | |
111 | } | |
112 | ||
113 | /* Delete ECMP routes from zebra. */ | |
114 | void rip_zebra_ipv4_delete(struct route_node *rp) | |
115 | { | |
116 | rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_DELETE); | |
117 | } | |
118 | ||
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) | |
122 | { | |
123 | struct zapi_route api; | |
124 | struct nexthop nh; | |
125 | ||
126 | if (!rip) | |
127 | return 0; | |
128 | ||
129 | if (zapi_route_decode(zclient->ibuf, &api) < 0) | |
130 | return -1; | |
131 | ||
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; | |
136 | ||
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, | |
145 | nh.ifindex); | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | void rip_redistribute_conf_update(int type) | |
151 | { | |
152 | zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, | |
153 | 0, VRF_DEFAULT); | |
154 | } | |
155 | ||
156 | void rip_redistribute_conf_delete(int type) | |
157 | { | |
158 | if (zclient->sock > 0) | |
159 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, | |
160 | AFI_IP, type, 0, VRF_DEFAULT); | |
161 | ||
162 | /* Remove the routes from RIP table. */ | |
163 | rip_redistribute_withdraw(type); | |
164 | } | |
165 | ||
166 | int rip_redistribute_check(int type) | |
167 | { | |
168 | return vrf_bitmap_check(zclient->redist[AFI_IP][type], VRF_DEFAULT); | |
169 | } | |
170 | ||
171 | void rip_redistribute_clean(void) | |
172 | { | |
173 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
174 | if (!vrf_bitmap_check(zclient->redist[AFI_IP][i], VRF_DEFAULT)) | |
175 | continue; | |
176 | ||
177 | if (zclient->sock > 0) | |
178 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, | |
179 | zclient, AFI_IP, i, 0, | |
180 | VRF_DEFAULT); | |
181 | ||
182 | vrf_bitmap_unset(zclient->redist[AFI_IP][i], VRF_DEFAULT); | |
183 | ||
184 | /* Remove the routes from RIP table. */ | |
185 | rip_redistribute_withdraw(i); | |
186 | } | |
187 | } | |
188 | ||
189 | void rip_show_redistribute_config(struct vty *vty) | |
190 | { | |
191 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
192 | if (i == zclient->redist_default | |
193 | || !vrf_bitmap_check(zclient->redist[AFI_IP][i], | |
194 | VRF_DEFAULT)) | |
195 | continue; | |
196 | ||
197 | vty_out(vty, " %s", zebra_route_string(i)); | |
198 | } | |
199 | } | |
200 | ||
201 | static void rip_zebra_connected(struct zclient *zclient) | |
202 | { | |
203 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
204 | } | |
205 | ||
206 | void rip_zclient_init(struct thread_master *master) | |
207 | { | |
208 | /* Set default value to the zebra client structure. */ | |
209 | zclient = zclient_new(master, &zclient_options_default); | |
210 | zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs); | |
211 | zclient->zebra_connected = rip_zebra_connected; | |
212 | zclient->interface_add = rip_interface_add; | |
213 | zclient->interface_delete = rip_interface_delete; | |
214 | zclient->interface_address_add = rip_interface_address_add; | |
215 | zclient->interface_address_delete = rip_interface_address_delete; | |
216 | zclient->interface_up = rip_interface_up; | |
217 | zclient->interface_down = rip_interface_down; | |
218 | zclient->redistribute_route_add = rip_zebra_read_route; | |
219 | zclient->redistribute_route_del = rip_zebra_read_route; | |
220 | } | |
221 | ||
222 | void rip_zclient_stop(void) | |
223 | { | |
224 | zclient_stop(zclient); | |
225 | zclient_free(zclient); | |
226 | } |