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