]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_route.c
Merge pull request #5776 from volta-networks/fix_ldp_topol_test
[mirror_frr.git] / nhrpd / nhrp_route.c
CommitLineData
2fb975da
TT
1/* NHRP routing functions
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
b45ac5f5
DL
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
2fb975da
TT
14#include "nhrpd.h"
15#include "table.h"
16#include "memory.h"
17#include "stream.h"
18#include "log.h"
19#include "zclient.h"
20
819dc8bb
DL
21DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry")
22
2fb975da
TT
23static struct zclient *zclient;
24static struct route_table *zebra_rib[AFI_MAX];
25
26struct route_info {
27 union sockunion via;
28 struct interface *ifp;
29 struct interface *nhrp_ifp;
30};
31
996c9314
LB
32static struct route_node *nhrp_route_update_get(const struct prefix *p,
33 int create)
2fb975da
TT
34{
35 struct route_node *rn;
36 afi_t afi = family2afi(PREFIX_FAMILY(p));
37
38 if (!zebra_rib[afi])
39 return NULL;
40
41 if (create) {
42 rn = route_node_get(zebra_rib[afi], p);
43 if (!rn->info) {
996c9314
LB
44 rn->info = XCALLOC(MTYPE_NHRP_ROUTE,
45 sizeof(struct route_info));
2fb975da
TT
46 route_lock_node(rn);
47 }
48 return rn;
49 } else {
50 return route_node_lookup(zebra_rib[afi], p);
51 }
52}
53
54static void nhrp_route_update_put(struct route_node *rn)
55{
56 struct route_info *ri = rn->info;
57
996c9314
LB
58 if (!ri->ifp && !ri->nhrp_ifp
59 && sockunion_family(&ri->via) == AF_UNSPEC) {
2fb975da 60 XFREE(MTYPE_NHRP_ROUTE, rn->info);
2fb975da
TT
61 route_unlock_node(rn);
62 }
63 route_unlock_node(rn);
64}
65
996c9314
LB
66static void nhrp_route_update_zebra(const struct prefix *p,
67 union sockunion *nexthop,
68 struct interface *ifp)
2fb975da
TT
69{
70 struct route_node *rn;
71 struct route_info *ri;
72
996c9314
LB
73 rn = nhrp_route_update_get(
74 p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
2fb975da
TT
75 if (rn) {
76 ri = rn->info;
77 ri->via = *nexthop;
78 ri->ifp = ifp;
79 nhrp_route_update_put(rn);
80 }
81}
82
83void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp)
84{
85 struct route_node *rn;
86 struct route_info *ri;
87
88 rn = nhrp_route_update_get(p, ifp != NULL);
89 if (rn) {
90 ri = rn->info;
91 ri->nhrp_ifp = ifp;
92 nhrp_route_update_put(rn);
93 }
94}
95
996c9314
LB
96void nhrp_route_announce(int add, enum nhrp_cache_type type,
97 const struct prefix *p, struct interface *ifp,
98 const union sockunion *nexthop, uint32_t mtu)
2fb975da 99{
9350687d
RW
100 struct zapi_route api;
101 struct zapi_nexthop *api_nh;
2fb975da
TT
102
103 if (zclient->sock < 0)
104 return;
105
9350687d
RW
106 memset(&api, 0, sizeof(api));
107 api.type = ZEBRA_ROUTE_NHRP;
108 api.safi = SAFI_UNICAST;
d5b2119c 109 api.vrf_id = VRF_DEFAULT;
9350687d
RW
110 api.prefix = *p;
111
2fb975da
TT
112 switch (type) {
113 case NHRP_CACHE_NEGATIVE:
09a484dd
DL
114 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
115 ifp = NULL;
116 nexthop = NULL;
2fb975da
TT
117 break;
118 case NHRP_CACHE_DYNAMIC:
119 case NHRP_CACHE_NHS:
120 case NHRP_CACHE_STATIC:
121 /* Regular route, so these are announced
122 * to other routing daemons */
123 break;
124 default:
9350687d 125 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
2fb975da
TT
126 break;
127 }
4e8b02f4 128 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
2fb975da 129
9350687d
RW
130 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
131 api.nexthop_num = 1;
132 api_nh = &api.nexthops[0];
4a7371e9 133 api_nh->vrf_id = VRF_DEFAULT;
2fb975da 134
9350687d
RW
135 switch (api.prefix.family) {
136 case AF_INET:
2fb975da 137 if (nexthop) {
6f27a9f6
RW
138 api_nh->gate.ipv4 = nexthop->sin.sin_addr;
139 api_nh->type = NEXTHOP_TYPE_IPV4;
2fb975da
TT
140 }
141 if (ifp) {
6f27a9f6
RW
142 api_nh->ifindex = ifp->ifindex;
143 if (api_nh->type == NEXTHOP_TYPE_IPV4)
144 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
145 else
146 api_nh->type = NEXTHOP_TYPE_IFINDEX;
2fb975da 147 }
9350687d
RW
148 break;
149 case AF_INET6:
37dc8ab5 150 if (nexthop) {
6f27a9f6
RW
151 api_nh->gate.ipv6 = nexthop->sin6.sin6_addr;
152 api_nh->type = NEXTHOP_TYPE_IPV6;
37dc8ab5
TT
153 }
154 if (ifp) {
6f27a9f6
RW
155 api_nh->ifindex = ifp->ifindex;
156 if (api_nh->type == NEXTHOP_TYPE_IPV6)
157 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
158 else
159 api_nh->type = NEXTHOP_TYPE_IFINDEX;
37dc8ab5 160 }
9350687d
RW
161 break;
162 }
163 if (mtu) {
164 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
165 api.mtu = mtu;
166 }
37dc8ab5 167
9350687d
RW
168 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
169 char buf[2][PREFIX_STRLEN];
37dc8ab5 170
9350687d 171 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
996c9314
LB
172 zlog_debug(
173 "Zebra send: route %s %s nexthop %s metric %u"
9350687d
RW
174 " count %d dev %s",
175 add ? "add" : "del", buf[0],
996c9314
LB
176 nexthop ? inet_ntop(api.prefix.family, &api_nh->gate,
177 buf[1], sizeof(buf[1]))
178 : "<onlink>",
09a484dd 179 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
2fb975da 180 }
9350687d
RW
181
182 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
183 &api);
2fb975da
TT
184}
185
121f9dee 186int nhrp_route_read(ZAPI_CALLBACK_ARGS)
2fb975da 187{
74489921
RW
188 struct zapi_route api;
189 struct zapi_nexthop *api_nh;
2fb975da 190 struct interface *ifp = NULL;
2fb975da 191 union sockunion nexthop_addr;
2fb975da 192 char buf[2][PREFIX_STRLEN];
74489921 193 int added;
2fb975da 194
74489921 195 if (zapi_route_decode(zclient->ibuf, &api) < 0)
2fb975da 196 return -1;
74489921
RW
197
198 /* we completely ignore srcdest routes for now. */
199 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
c125d1d4
RW
200 return 0;
201
74489921
RW
202 sockunion_family(&nexthop_addr) = AF_UNSPEC;
203 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
204 api_nh = &api.nexthops[0];
205
206 nexthop_addr.sa.sa_family = api.prefix.family;
207 switch (nexthop_addr.sa.sa_family) {
208 case AF_INET:
209 nexthop_addr.sin.sin_addr = api_nh->gate.ipv4;
210 break;
211 case AF_INET6:
212 nexthop_addr.sin6.sin6_addr = api_nh->gate.ipv6;
213 break;
2fb975da 214 }
74489921
RW
215
216 if (api_nh->ifindex != IFINDEX_INTERNAL)
996c9314 217 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
2fb975da 218 }
2fb975da 219
74489921 220 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
2fb975da 221 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
996c9314
LB
222 added ? "add" : "del",
223 prefix2str(&api.prefix, buf[0], sizeof buf[0]),
224 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
225 ifp ? ifp->name : "(none)");
2fb975da 226
74489921
RW
227 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
228 nhrp_shortcut_prefix_change(&api.prefix, !added);
2fb975da
TT
229
230 return 0;
231}
232
996c9314
LB
233int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
234 union sockunion *via, struct interface **ifp)
2fb975da
TT
235{
236 struct route_node *rn;
237 struct route_info *ri;
238 struct prefix lookup;
239 afi_t afi = family2afi(sockunion_family(addr));
240 char buf[PREFIX_STRLEN];
241
242 sockunion2hostprefix(addr, &lookup);
243
244 rn = route_node_match(zebra_rib[afi], &lookup);
996c9314
LB
245 if (!rn)
246 return 0;
2fb975da
TT
247
248 ri = rn->info;
249 if (ri->nhrp_ifp) {
250 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
996c9314
LB
251 prefix2str(&lookup, buf, sizeof buf),
252 ri->nhrp_ifp->name);
2fb975da 253
996c9314
LB
254 if (via)
255 sockunion_family(via) = AF_UNSPEC;
256 if (ifp)
257 *ifp = ri->nhrp_ifp;
2fb975da
TT
258 } else {
259 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
996c9314
LB
260 prefix2str(&lookup, buf, sizeof buf),
261 ri->ifp ? ri->ifp->name : "(none)");
2fb975da 262
996c9314
LB
263 if (via)
264 *via = ri->via;
265 if (ifp)
266 *ifp = ri->ifp;
2fb975da 267 }
996c9314
LB
268 if (p)
269 *p = rn->p;
2fb975da
TT
270 route_unlock_node(rn);
271 return 1;
272}
273
996c9314
LB
274enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
275 union sockunion *addr, struct prefix *p,
276 struct nhrp_peer **peer)
2fb975da
TT
277{
278 struct interface *ifp = in_ifp;
279 struct nhrp_interface *nifp;
280 struct nhrp_cache *c;
281 union sockunion via[4];
282 uint32_t network_id = 0;
283 afi_t afi = family2afi(sockunion_family(addr));
284 int i;
285
286 if (ifp) {
287 nifp = ifp->info;
288 network_id = nifp->afi[afi].network_id;
289
290 c = nhrp_cache_get(ifp, addr, 0);
291 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
996c9314
LB
292 if (p)
293 memset(p, 0, sizeof(*p));
2fb975da
TT
294 return NHRP_ROUTE_LOCAL;
295 }
296 }
297
298 for (i = 0; i < 4; i++) {
299 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
300 return NHRP_ROUTE_BLACKHOLE;
301 if (ifp) {
302 /* Departing from nbma network? */
303 nifp = ifp->info;
996c9314
LB
304 if (network_id
305 && network_id != nifp->afi[afi].network_id)
2fb975da
TT
306 return NHRP_ROUTE_OFF_NBMA;
307 }
308 if (sockunion_family(&via[i]) == AF_UNSPEC)
309 break;
310 /* Resolve via node, but return the prefix of first match */
311 addr = &via[i];
312 p = NULL;
313 }
314
315 if (ifp) {
316 c = nhrp_cache_get(ifp, addr, 0);
317 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
996c9314
LB
318 if (p)
319 memset(p, 0, sizeof(*p));
2fb975da
TT
320 if (c->cur.type == NHRP_CACHE_LOCAL)
321 return NHRP_ROUTE_LOCAL;
996c9314
LB
322 if (peer)
323 *peer = nhrp_peer_ref(c->cur.peer);
2fb975da
TT
324 return NHRP_ROUTE_NBMA_NEXTHOP;
325 }
326 }
327
328 return NHRP_ROUTE_BLACKHOLE;
329}
330
996c9314 331static void nhrp_zebra_connected(struct zclient *zclient)
a1f1bab0
JB
332{
333 zclient_send_reg_requests(zclient, VRF_DEFAULT);
334 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
996c9314 335 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
a1f1bab0 336 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
996c9314 337 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
a1f1bab0
JB
338}
339
2fb975da
TT
340void nhrp_zebra_init(void)
341{
342 zebra_rib[AFI_IP] = route_table_init();
343 zebra_rib[AFI_IP6] = route_table_init();
344
26f63a1e 345 zclient = zclient_new(master, &zclient_options_default);
a1f1bab0 346 zclient->zebra_connected = nhrp_zebra_connected;
2fb975da
TT
347 zclient->interface_address_add = nhrp_interface_address_add;
348 zclient->interface_address_delete = nhrp_interface_address_delete;
74489921
RW
349 zclient->redistribute_route_add = nhrp_route_read;
350 zclient->redistribute_route_del = nhrp_route_read;
819dc8bb 351
342213ea 352 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
2fb975da
TT
353}
354
355void nhrp_zebra_terminate(void)
356{
357 zclient_stop(zclient);
20a11b25 358 zclient_free(zclient);
2fb975da
TT
359 route_table_finish(zebra_rib[AFI_IP]);
360 route_table_finish(zebra_rib[AFI_IP6]);
361}