]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "prefix.h" | |
bce8e868 | 25 | #include "table.h" |
718e3744 | 26 | #include "stream.h" |
bce8e868 | 27 | #include "memory.h" |
718e3744 | 28 | #include "zclient.h" |
29 | #include "log.h" | |
7076bb2f | 30 | #include "vrf.h" |
718e3744 | 31 | #include "ripd/ripd.h" |
32 | #include "ripd/rip_debug.h" | |
dc63bfd4 | 33 | #include "ripd/rip_interface.h" |
718e3744 | 34 | |
35 | /* All information about zebra. */ | |
36 | struct zclient *zclient = NULL; | |
6b0655a2 | 37 | |
bce8e868 | 38 | /* Send ECMP routes to zebra. */ |
d7c0a89a | 39 | static void rip_zebra_ipv4_send(struct route_node *rp, uint8_t cmd) |
718e3744 | 40 | { |
d62a17ae | 41 | struct list *list = (struct list *)rp->info; |
c9fb3e23 RW |
42 | struct zapi_route api; |
43 | struct zapi_nexthop *api_nh; | |
d62a17ae | 44 | struct listnode *listnode = NULL; |
45 | struct rip_info *rinfo = NULL; | |
46 | int count = 0; | |
47 | ||
c9fb3e23 | 48 | memset(&api, 0, sizeof(api)); |
d00061ea RW |
49 | api.vrf_id = VRF_DEFAULT; |
50 | api.type = ZEBRA_ROUTE_RIP; | |
d00061ea RW |
51 | api.safi = SAFI_UNICAST; |
52 | ||
d00061ea RW |
53 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
54 | for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) { | |
a74e593b RW |
55 | if (count >= MULTIPATH_NUM) |
56 | break; | |
c9fb3e23 | 57 | api_nh = &api.nexthops[count]; |
4a7371e9 | 58 | api_nh->vrf_id = VRF_DEFAULT; |
dd127197 | 59 | api_nh->gate = rinfo->nh.gate; |
c9fb3e23 RW |
60 | api_nh->type = NEXTHOP_TYPE_IPV4; |
61 | if (cmd == ZEBRA_ROUTE_ADD) | |
d00061ea RW |
62 | SET_FLAG(rinfo->flags, RIP_RTF_FIB); |
63 | else | |
64 | UNSET_FLAG(rinfo->flags, RIP_RTF_FIB); | |
c9fb3e23 | 65 | count++; |
d00061ea | 66 | } |
d62a17ae | 67 | |
c9fb3e23 | 68 | api.prefix = rp->p; |
d00061ea | 69 | api.nexthop_num = count; |
d62a17ae | 70 | |
d00061ea | 71 | rinfo = listgetdata(listhead(list)); |
d62a17ae | 72 | |
d00061ea RW |
73 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); |
74 | api.metric = rinfo->metric; | |
d62a17ae | 75 | |
d00061ea RW |
76 | if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) { |
77 | SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE); | |
78 | api.distance = rinfo->distance; | |
79 | } | |
d62a17ae | 80 | |
d00061ea RW |
81 | if (rinfo->tag) { |
82 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
83 | api.tag = rinfo->tag; | |
84 | } | |
d62a17ae | 85 | |
c9fb3e23 | 86 | zclient_route_send(cmd, zclient, &api); |
d00061ea RW |
87 | |
88 | if (IS_RIP_DEBUG_ZEBRA) { | |
89 | if (rip->ecmp) | |
90 | zlog_debug("%s: %s/%d nexthops %d", | |
c9fb3e23 | 91 | (cmd == ZEBRA_ROUTE_ADD) |
d00061ea RW |
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", | |
c9fb3e23 | 98 | (cmd == ZEBRA_ROUTE_ADD) |
d00061ea RW |
99 | ? "Install into zebra" |
100 | : "Delete from zebra", | |
101 | inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen); | |
d62a17ae | 102 | } |
d00061ea RW |
103 | |
104 | rip_global_route_changes++; | |
718e3744 | 105 | } |
106 | ||
bce8e868 | 107 | /* Add/update ECMP routes to zebra. */ |
d62a17ae | 108 | void rip_zebra_ipv4_add(struct route_node *rp) |
718e3744 | 109 | { |
c9fb3e23 | 110 | rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_ADD); |
bce8e868 | 111 | } |
718e3744 | 112 | |
bce8e868 | 113 | /* Delete ECMP routes from zebra. */ |
d62a17ae | 114 | void rip_zebra_ipv4_delete(struct route_node *rp) |
bce8e868 | 115 | { |
c9fb3e23 | 116 | rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_DELETE); |
718e3744 | 117 | } |
118 | ||
119 | /* Zebra route add and delete treatment. */ | |
74489921 RW |
120 | static int rip_zebra_read_route(int command, struct zclient *zclient, |
121 | zebra_size_t length, vrf_id_t vrf_id) | |
718e3744 | 122 | { |
74489921 | 123 | struct zapi_route api; |
3f5682c8 | 124 | struct nexthop nh; |
d62a17ae | 125 | |
126 | if (!rip) | |
127 | return 0; | |
128 | ||
74489921 RW |
129 | if (zapi_route_decode(zclient->ibuf, &api) < 0) |
130 | return -1; | |
131 | ||
3f5682c8 DS |
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; | |
d62a17ae | 136 | |
137 | /* Then fetch IPv4 prefixes. */ | |
74489921 RW |
138 | if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD) |
139 | rip_redistribute_add(api.type, RIP_ROUTE_REDISTRIBUTE, | |
3f5682c8 | 140 | (struct prefix_ipv4 *)&api.prefix, &nh, |
996c9314 | 141 | api.metric, api.distance, api.tag); |
74489921 RW |
142 | else if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) |
143 | rip_redistribute_delete(api.type, RIP_ROUTE_REDISTRIBUTE, | |
144 | (struct prefix_ipv4 *)&api.prefix, | |
3f5682c8 | 145 | nh.ifindex); |
d62a17ae | 146 | |
147 | return 0; | |
718e3744 | 148 | } |
149 | ||
908f0020 | 150 | void rip_redistribute_conf_update(int type) |
718e3744 | 151 | { |
908f0020 RW |
152 | zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, |
153 | 0, VRF_DEFAULT); | |
718e3744 | 154 | } |
6b0655a2 | 155 | |
908f0020 | 156 | void rip_redistribute_conf_delete(int type) |
718e3744 | 157 | { |
d62a17ae | 158 | if (zclient->sock > 0) |
159 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, | |
160 | AFI_IP, type, 0, VRF_DEFAULT); | |
718e3744 | 161 | |
d62a17ae | 162 | /* Remove the routes from RIP table. */ |
163 | rip_redistribute_withdraw(type); | |
718e3744 | 164 | } |
165 | ||
d62a17ae | 166 | int rip_redistribute_check(int type) |
718e3744 | 167 | { |
d62a17ae | 168 | return vrf_bitmap_check(zclient->redist[AFI_IP][type], VRF_DEFAULT); |
718e3744 | 169 | } |
170 | ||
d62a17ae | 171 | void rip_redistribute_clean(void) |
718e3744 | 172 | { |
908f0020 RW |
173 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { |
174 | if (!vrf_bitmap_check(zclient->redist[AFI_IP][i], VRF_DEFAULT)) | |
175 | continue; | |
d62a17ae | 176 | |
908f0020 RW |
177 | if (zclient->sock > 0) |
178 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, | |
179 | zclient, AFI_IP, i, 0, | |
180 | VRF_DEFAULT); | |
d62a17ae | 181 | |
908f0020 | 182 | vrf_bitmap_unset(zclient->redist[AFI_IP][i], VRF_DEFAULT); |
718e3744 | 183 | |
908f0020 RW |
184 | /* Remove the routes from RIP table. */ |
185 | rip_redistribute_withdraw(i); | |
d62a17ae | 186 | } |
16705130 | 187 | } |
188 | ||
908f0020 | 189 | void rip_show_redistribute_config(struct vty *vty) |
718e3744 | 190 | { |
908f0020 | 191 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { |
d00061ea RW |
192 | if (i == zclient->redist_default |
193 | || !vrf_bitmap_check(zclient->redist[AFI_IP][i], | |
194 | VRF_DEFAULT)) | |
195 | continue; | |
196 | ||
908f0020 | 197 | vty_out(vty, " %s", zebra_route_string(i)); |
d00061ea | 198 | } |
718e3744 | 199 | } |
200 | ||
d62a17ae | 201 | static void rip_zebra_connected(struct zclient *zclient) |
7076bb2f | 202 | { |
d62a17ae | 203 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
7076bb2f FL |
204 | } |
205 | ||
d62a17ae | 206 | void rip_zclient_init(struct thread_master *master) |
718e3744 | 207 | { |
d62a17ae | 208 | /* Set default value to the zebra client structure. */ |
26f63a1e | 209 | zclient = zclient_new(master, &zclient_options_default); |
342213ea | 210 | zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs); |
d62a17ae | 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; | |
74489921 RW |
218 | zclient->redistribute_route_add = rip_zebra_read_route; |
219 | zclient->redistribute_route_del = rip_zebra_read_route; | |
718e3744 | 220 | } |
a2f9eb82 | 221 | |
d62a17ae | 222 | void rip_zclient_stop(void) |
a2f9eb82 | 223 | { |
d62a17ae | 224 | zclient_stop(zclient); |
225 | zclient_free(zclient); | |
a2f9eb82 | 226 | } |