]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* |
3 | * RIPngd and zebra interface. | |
4 | * Copyright (C) 1998, 1999 Kunihiro Ishiguro | |
718e3744 | 5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
9 | #include "command.h" | |
10 | #include "prefix.h" | |
fe08ba7e | 11 | #include "agg_table.h" |
718e3744 | 12 | #include "stream.h" |
c880b636 | 13 | #include "memory.h" |
718e3744 | 14 | #include "routemap.h" |
15 | #include "zclient.h" | |
16 | #include "log.h" | |
17 | ||
18 | #include "ripngd/ripngd.h" | |
c880b636 | 19 | #include "ripngd/ripng_debug.h" |
718e3744 | 20 | |
21 | /* All information about zebra. */ | |
22 | struct zclient *zclient = NULL; | |
23 | ||
c880b636 | 24 | /* Send ECMP routes to zebra. */ |
5c84b9a5 RW |
25 | static void ripng_zebra_ipv6_send(struct ripng *ripng, struct agg_node *rp, |
26 | uint8_t cmd) | |
718e3744 | 27 | { |
d62a17ae | 28 | struct list *list = (struct list *)rp->info; |
c9fb3e23 RW |
29 | struct zapi_route api; |
30 | struct zapi_nexthop *api_nh; | |
d62a17ae | 31 | struct listnode *listnode = NULL; |
32 | struct ripng_info *rinfo = NULL; | |
33 | int count = 0; | |
26a3ffd6 | 34 | const struct prefix *p = agg_node_get_prefix(rp); |
d62a17ae | 35 | |
c9fb3e23 | 36 | memset(&api, 0, sizeof(api)); |
dde7b15b | 37 | api.vrf_id = ripng->vrf->vrf_id; |
d00061ea | 38 | api.type = ZEBRA_ROUTE_RIPNG; |
d00061ea | 39 | api.safi = SAFI_UNICAST; |
26a3ffd6 | 40 | api.prefix = *p; |
d62a17ae | 41 | |
d00061ea | 42 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
d00061ea | 43 | for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) { |
a74e593b RW |
44 | if (count >= MULTIPATH_NUM) |
45 | break; | |
c9fb3e23 | 46 | api_nh = &api.nexthops[count]; |
dde7b15b | 47 | api_nh->vrf_id = ripng->vrf->vrf_id; |
c9fb3e23 RW |
48 | api_nh->gate.ipv6 = rinfo->nexthop; |
49 | api_nh->ifindex = rinfo->ifindex; | |
50 | api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
d00061ea | 51 | count++; |
c9fb3e23 | 52 | if (cmd == ZEBRA_ROUTE_ADD) |
d00061ea RW |
53 | SET_FLAG(rinfo->flags, RIPNG_RTF_FIB); |
54 | else | |
55 | UNSET_FLAG(rinfo->flags, RIPNG_RTF_FIB); | |
56 | } | |
d62a17ae | 57 | |
d00061ea | 58 | api.nexthop_num = count; |
d62a17ae | 59 | |
d00061ea | 60 | rinfo = listgetdata(listhead(list)); |
d62a17ae | 61 | |
d00061ea RW |
62 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); |
63 | api.metric = rinfo->metric; | |
d62a17ae | 64 | |
d00061ea RW |
65 | if (rinfo->tag) { |
66 | SET_FLAG(api.message, ZAPI_MESSAGE_TAG); | |
67 | api.tag = rinfo->tag; | |
68 | } | |
d62a17ae | 69 | |
c9fb3e23 | 70 | zclient_route_send(cmd, zclient, &api); |
d00061ea RW |
71 | |
72 | if (IS_RIPNG_DEBUG_ZEBRA) { | |
73 | if (ripng->ecmp) | |
26a3ffd6 | 74 | zlog_debug("%s: %pRN nexthops %d", |
c9fb3e23 | 75 | (cmd == ZEBRA_ROUTE_ADD) |
d00061ea RW |
76 | ? "Install into zebra" |
77 | : "Delete from zebra", | |
26a3ffd6 | 78 | rp, count); |
d00061ea | 79 | else |
26a3ffd6 DS |
80 | zlog_debug("%s: %pRN", |
81 | (cmd == ZEBRA_ROUTE_ADD) | |
82 | ? "Install into zebra" | |
83 | : "Delete from zebra", | |
84 | rp); | |
d62a17ae | 85 | } |
718e3744 | 86 | } |
87 | ||
c880b636 | 88 | /* Add/update ECMP routes to zebra. */ |
5c84b9a5 | 89 | void ripng_zebra_ipv6_add(struct ripng *ripng, struct agg_node *rp) |
718e3744 | 90 | { |
5c84b9a5 | 91 | ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_ADD); |
c880b636 | 92 | } |
718e3744 | 93 | |
c880b636 | 94 | /* Delete ECMP routes from zebra. */ |
5c84b9a5 | 95 | void ripng_zebra_ipv6_delete(struct ripng *ripng, struct agg_node *rp) |
c880b636 | 96 | { |
5c84b9a5 | 97 | ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_DELETE); |
718e3744 | 98 | } |
99 | ||
100 | /* Zebra route add and delete treatment. */ | |
121f9dee | 101 | static int ripng_zebra_read_route(ZAPI_CALLBACK_ARGS) |
718e3744 | 102 | { |
5c84b9a5 | 103 | struct ripng *ripng; |
74489921 | 104 | struct zapi_route api; |
d62a17ae | 105 | struct in6_addr nexthop; |
74489921 | 106 | unsigned long ifindex; |
d62a17ae | 107 | |
5c84b9a5 RW |
108 | ripng = ripng_lookup_by_vrf_id(vrf_id); |
109 | if (!ripng) | |
110 | return 0; | |
111 | ||
74489921 RW |
112 | if (zapi_route_decode(zclient->ibuf, &api) < 0) |
113 | return -1; | |
d62a17ae | 114 | |
74489921 RW |
115 | /* we completely ignore srcdest routes for now. */ |
116 | if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) | |
117 | return 0; | |
d62a17ae | 118 | |
9fb2b879 DS |
119 | if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6)) |
120 | return 0; | |
121 | ||
74489921 RW |
122 | nexthop = api.nexthops[0].gate.ipv6; |
123 | ifindex = api.nexthops[0].ifindex; | |
d62a17ae | 124 | |
121f9dee | 125 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) |
5c84b9a5 RW |
126 | ripng_redistribute_add(ripng, api.type, |
127 | RIPNG_ROUTE_REDISTRIBUTE, | |
74489921 | 128 | (struct prefix_ipv6 *)&api.prefix, |
d62a17ae | 129 | ifindex, &nexthop, api.tag); |
130 | else | |
5c84b9a5 RW |
131 | ripng_redistribute_delete( |
132 | ripng, api.type, RIPNG_ROUTE_REDISTRIBUTE, | |
133 | (struct prefix_ipv6 *)&api.prefix, ifindex); | |
d62a17ae | 134 | |
135 | return 0; | |
718e3744 | 136 | } |
137 | ||
5c84b9a5 | 138 | void ripng_redistribute_conf_update(struct ripng *ripng, int type) |
718e3744 | 139 | { |
2afebed2 DS |
140 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, |
141 | type, 0, ripng->vrf->vrf_id); | |
db2038c7 | 142 | } |
7076bb2f | 143 | |
5c84b9a5 | 144 | void ripng_redistribute_conf_delete(struct ripng *ripng, int type) |
db2038c7 | 145 | { |
d62a17ae | 146 | if (zclient->sock > 0) |
147 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, | |
dde7b15b | 148 | AFI_IP6, type, 0, ripng->vrf->vrf_id); |
718e3744 | 149 | |
5c84b9a5 | 150 | ripng_redistribute_withdraw(ripng, type); |
718e3744 | 151 | } |
152 | ||
5c84b9a5 | 153 | int ripng_redistribute_check(struct ripng *ripng, int type) |
a94434b6 | 154 | { |
f9120f71 | 155 | return ripng->redist[type].enabled; |
a94434b6 | 156 | } |
157 | ||
f9120f71 | 158 | void ripng_redistribute_enable(struct ripng *ripng) |
a94434b6 | 159 | { |
db2038c7 | 160 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { |
f9120f71 | 161 | if (!ripng_redistribute_check(ripng, i)) |
db2038c7 | 162 | continue; |
d62a17ae | 163 | |
f9120f71 RW |
164 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, |
165 | AFI_IP6, i, 0, ripng->vrf->vrf_id); | |
166 | } | |
167 | } | |
d62a17ae | 168 | |
f9120f71 RW |
169 | void ripng_redistribute_disable(struct ripng *ripng) |
170 | { | |
171 | for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
172 | if (!ripng_redistribute_check(ripng, i)) | |
173 | continue; | |
718e3744 | 174 | |
f9120f71 RW |
175 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient, |
176 | AFI_IP6, i, 0, ripng->vrf->vrf_id); | |
d62a17ae | 177 | } |
718e3744 | 178 | } |
179 | ||
5c84b9a5 | 180 | void ripng_redistribute_write(struct vty *vty, struct ripng *ripng) |
718e3744 | 181 | { |
d62a17ae | 182 | int i; |
183 | ||
d00061ea RW |
184 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { |
185 | if (i == zclient->redist_default | |
f9120f71 | 186 | || !ripng_redistribute_check(ripng, i)) |
d00061ea RW |
187 | continue; |
188 | ||
db2038c7 | 189 | vty_out(vty, " %s", zebra_route_string(i)); |
d00061ea | 190 | } |
718e3744 | 191 | } |
192 | ||
dde7b15b RW |
193 | void ripng_zebra_vrf_register(struct vrf *vrf) |
194 | { | |
195 | if (vrf->vrf_id == VRF_DEFAULT) | |
196 | return; | |
197 | ||
198 | if (IS_RIPNG_DEBUG_EVENT) | |
199 | zlog_debug("%s: register VRF %s(%u) to zebra", __func__, | |
200 | vrf->name, vrf->vrf_id); | |
201 | ||
202 | zclient_send_reg_requests(zclient, vrf->vrf_id); | |
203 | } | |
204 | ||
205 | void ripng_zebra_vrf_deregister(struct vrf *vrf) | |
206 | { | |
207 | if (vrf->vrf_id == VRF_DEFAULT) | |
208 | return; | |
209 | ||
210 | if (IS_RIPNG_DEBUG_EVENT) | |
211 | zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__, | |
212 | vrf->name, vrf->vrf_id); | |
213 | ||
214 | zclient_send_dereg_requests(zclient, vrf->vrf_id); | |
215 | } | |
216 | ||
d62a17ae | 217 | static void ripng_zebra_connected(struct zclient *zclient) |
7076bb2f | 218 | { |
d62a17ae | 219 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
7076bb2f FL |
220 | } |
221 | ||
a243d1db DL |
222 | static zclient_handler *const ripng_handlers[] = { |
223 | [ZEBRA_INTERFACE_ADDRESS_ADD] = ripng_interface_address_add, | |
224 | [ZEBRA_INTERFACE_ADDRESS_DELETE] = ripng_interface_address_delete, | |
225 | [ZEBRA_INTERFACE_VRF_UPDATE] = ripng_interface_vrf_update, | |
226 | [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ripng_zebra_read_route, | |
227 | [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ripng_zebra_read_route, | |
228 | }; | |
229 | ||
718e3744 | 230 | /* Initialize zebra structure and it's commands. */ |
cd9d0537 | 231 | void zebra_init(struct event_loop *master) |
718e3744 | 232 | { |
d62a17ae | 233 | /* Allocate zebra structure. */ |
a243d1db DL |
234 | zclient = zclient_new(master, &zclient_options_default, ripng_handlers, |
235 | array_size(ripng_handlers)); | |
342213ea | 236 | zclient_init(zclient, ZEBRA_ROUTE_RIPNG, 0, &ripngd_privs); |
d62a17ae | 237 | |
238 | zclient->zebra_connected = ripng_zebra_connected; | |
718e3744 | 239 | } |
7feb9237 | 240 | |
d62a17ae | 241 | void ripng_zebra_stop(void) |
7feb9237 | 242 | { |
d62a17ae | 243 | zclient_stop(zclient); |
244 | zclient_free(zclient); | |
7feb9237 | 245 | } |