]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_zebra.c
Merge pull request #7540 from ton31337/fix/typo
[mirror_frr.git] / ripngd / ripng_zebra.c
CommitLineData
718e3744 1/*
2 * RIPngd and zebra interface.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
24#include "command.h"
25#include "prefix.h"
fe08ba7e 26#include "agg_table.h"
718e3744 27#include "stream.h"
c880b636 28#include "memory.h"
718e3744 29#include "routemap.h"
30#include "zclient.h"
31#include "log.h"
32
33#include "ripngd/ripngd.h"
c880b636 34#include "ripngd/ripng_debug.h"
718e3744 35
36/* All information about zebra. */
37struct zclient *zclient = NULL;
38
c880b636 39/* Send ECMP routes to zebra. */
5c84b9a5
RW
40static void ripng_zebra_ipv6_send(struct ripng *ripng, struct agg_node *rp,
41 uint8_t cmd)
718e3744 42{
d62a17ae 43 struct list *list = (struct list *)rp->info;
c9fb3e23
RW
44 struct zapi_route api;
45 struct zapi_nexthop *api_nh;
d62a17ae 46 struct listnode *listnode = NULL;
47 struct ripng_info *rinfo = NULL;
48 int count = 0;
26a3ffd6 49 const struct prefix *p = agg_node_get_prefix(rp);
d62a17ae 50
c9fb3e23 51 memset(&api, 0, sizeof(api));
dde7b15b 52 api.vrf_id = ripng->vrf->vrf_id;
d00061ea 53 api.type = ZEBRA_ROUTE_RIPNG;
d00061ea 54 api.safi = SAFI_UNICAST;
26a3ffd6 55 api.prefix = *p;
d62a17ae 56
d00061ea 57 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
d00061ea 58 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
a74e593b
RW
59 if (count >= MULTIPATH_NUM)
60 break;
c9fb3e23 61 api_nh = &api.nexthops[count];
dde7b15b 62 api_nh->vrf_id = ripng->vrf->vrf_id;
c9fb3e23
RW
63 api_nh->gate.ipv6 = rinfo->nexthop;
64 api_nh->ifindex = rinfo->ifindex;
65 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
d00061ea 66 count++;
c9fb3e23 67 if (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
68 SET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
69 else
70 UNSET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
71 }
d62a17ae 72
d00061ea 73 api.nexthop_num = count;
d62a17ae 74
d00061ea 75 rinfo = listgetdata(listhead(list));
d62a17ae 76
d00061ea
RW
77 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
78 api.metric = rinfo->metric;
d62a17ae 79
d00061ea
RW
80 if (rinfo->tag) {
81 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
82 api.tag = rinfo->tag;
83 }
d62a17ae 84
c9fb3e23 85 zclient_route_send(cmd, zclient, &api);
d00061ea
RW
86
87 if (IS_RIPNG_DEBUG_ZEBRA) {
88 if (ripng->ecmp)
26a3ffd6 89 zlog_debug("%s: %pRN nexthops %d",
c9fb3e23 90 (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
91 ? "Install into zebra"
92 : "Delete from zebra",
26a3ffd6 93 rp, count);
d00061ea 94 else
26a3ffd6
DS
95 zlog_debug("%s: %pRN",
96 (cmd == ZEBRA_ROUTE_ADD)
97 ? "Install into zebra"
98 : "Delete from zebra",
99 rp);
d62a17ae 100 }
718e3744 101}
102
c880b636 103/* Add/update ECMP routes to zebra. */
5c84b9a5 104void ripng_zebra_ipv6_add(struct ripng *ripng, struct agg_node *rp)
718e3744 105{
5c84b9a5 106 ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_ADD);
c880b636 107}
718e3744 108
c880b636 109/* Delete ECMP routes from zebra. */
5c84b9a5 110void ripng_zebra_ipv6_delete(struct ripng *ripng, struct agg_node *rp)
c880b636 111{
5c84b9a5 112 ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_DELETE);
718e3744 113}
114
115/* Zebra route add and delete treatment. */
121f9dee 116static int ripng_zebra_read_route(ZAPI_CALLBACK_ARGS)
718e3744 117{
5c84b9a5 118 struct ripng *ripng;
74489921 119 struct zapi_route api;
d62a17ae 120 struct in6_addr nexthop;
74489921 121 unsigned long ifindex;
d62a17ae 122
5c84b9a5
RW
123 ripng = ripng_lookup_by_vrf_id(vrf_id);
124 if (!ripng)
125 return 0;
126
74489921
RW
127 if (zapi_route_decode(zclient->ibuf, &api) < 0)
128 return -1;
d62a17ae 129
74489921
RW
130 /* we completely ignore srcdest routes for now. */
131 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
132 return 0;
d62a17ae 133
9fb2b879
DS
134 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
135 return 0;
136
74489921
RW
137 nexthop = api.nexthops[0].gate.ipv6;
138 ifindex = api.nexthops[0].ifindex;
d62a17ae 139
121f9dee 140 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
5c84b9a5
RW
141 ripng_redistribute_add(ripng, api.type,
142 RIPNG_ROUTE_REDISTRIBUTE,
74489921 143 (struct prefix_ipv6 *)&api.prefix,
d62a17ae 144 ifindex, &nexthop, api.tag);
145 else
5c84b9a5
RW
146 ripng_redistribute_delete(
147 ripng, api.type, RIPNG_ROUTE_REDISTRIBUTE,
148 (struct prefix_ipv6 *)&api.prefix, ifindex);
d62a17ae 149
150 return 0;
718e3744 151}
152
5c84b9a5 153void ripng_redistribute_conf_update(struct ripng *ripng, int type)
718e3744 154{
2afebed2
DS
155 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
156 type, 0, ripng->vrf->vrf_id);
db2038c7 157}
7076bb2f 158
5c84b9a5 159void ripng_redistribute_conf_delete(struct ripng *ripng, int type)
db2038c7 160{
d62a17ae 161 if (zclient->sock > 0)
162 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
dde7b15b 163 AFI_IP6, type, 0, ripng->vrf->vrf_id);
718e3744 164
5c84b9a5 165 ripng_redistribute_withdraw(ripng, type);
718e3744 166}
167
5c84b9a5 168int ripng_redistribute_check(struct ripng *ripng, int type)
a94434b6 169{
f9120f71 170 return ripng->redist[type].enabled;
a94434b6 171}
172
f9120f71 173void ripng_redistribute_enable(struct ripng *ripng)
a94434b6 174{
db2038c7 175 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
f9120f71 176 if (!ripng_redistribute_check(ripng, i))
db2038c7 177 continue;
d62a17ae 178
f9120f71
RW
179 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
180 AFI_IP6, i, 0, ripng->vrf->vrf_id);
181 }
182}
d62a17ae 183
f9120f71
RW
184void ripng_redistribute_disable(struct ripng *ripng)
185{
186 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
187 if (!ripng_redistribute_check(ripng, i))
188 continue;
718e3744 189
f9120f71
RW
190 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
191 AFI_IP6, i, 0, ripng->vrf->vrf_id);
d62a17ae 192 }
718e3744 193}
194
5c84b9a5 195void ripng_redistribute_write(struct vty *vty, struct ripng *ripng)
718e3744 196{
d62a17ae 197 int i;
198
d00061ea
RW
199 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
200 if (i == zclient->redist_default
f9120f71 201 || !ripng_redistribute_check(ripng, i))
d00061ea
RW
202 continue;
203
db2038c7 204 vty_out(vty, " %s", zebra_route_string(i));
d00061ea 205 }
718e3744 206}
207
dde7b15b
RW
208void ripng_zebra_vrf_register(struct vrf *vrf)
209{
210 if (vrf->vrf_id == VRF_DEFAULT)
211 return;
212
213 if (IS_RIPNG_DEBUG_EVENT)
214 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
215 vrf->name, vrf->vrf_id);
216
217 zclient_send_reg_requests(zclient, vrf->vrf_id);
218}
219
220void ripng_zebra_vrf_deregister(struct vrf *vrf)
221{
222 if (vrf->vrf_id == VRF_DEFAULT)
223 return;
224
225 if (IS_RIPNG_DEBUG_EVENT)
226 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
227 vrf->name, vrf->vrf_id);
228
229 zclient_send_dereg_requests(zclient, vrf->vrf_id);
230}
231
d62a17ae 232static void ripng_zebra_connected(struct zclient *zclient)
7076bb2f 233{
d62a17ae 234 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
235}
236
718e3744 237/* Initialize zebra structure and it's commands. */
d62a17ae 238void zebra_init(struct thread_master *master)
718e3744 239{
d62a17ae 240 /* Allocate zebra structure. */
26f63a1e 241 zclient = zclient_new(master, &zclient_options_default);
342213ea 242 zclient_init(zclient, ZEBRA_ROUTE_RIPNG, 0, &ripngd_privs);
d62a17ae 243
244 zclient->zebra_connected = ripng_zebra_connected;
d62a17ae 245 zclient->interface_address_add = ripng_interface_address_add;
246 zclient->interface_address_delete = ripng_interface_address_delete;
dde7b15b 247 zclient->interface_vrf_update = ripng_interface_vrf_update;
74489921
RW
248 zclient->redistribute_route_add = ripng_zebra_read_route;
249 zclient->redistribute_route_del = ripng_zebra_read_route;
718e3744 250}
7feb9237 251
d62a17ae 252void ripng_zebra_stop(void)
7feb9237 253{
d62a17ae 254 zclient_stop(zclient);
255 zclient_free(zclient);
7feb9237 256}