]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_route.c
nhrpd: replace nhrp route nexthop with onlink route when prefix=nh
[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 union sockunion *nexthop_ref = (union sockunion *)nexthop;
102
103 if (zclient->sock < 0)
104 return;
105
106 memset(&api, 0, sizeof(api));
107 api.type = ZEBRA_ROUTE_NHRP;
108 api.safi = SAFI_UNICAST;
109 api.vrf_id = VRF_DEFAULT;
110 api.prefix = *p;
111
112 switch (type) {
113 case NHRP_CACHE_NEGATIVE:
114 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
115 ifp = NULL;
116 nexthop = NULL;
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:
125 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
126 break;
127 }
128 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
129
130 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
131 api.nexthop_num = 1;
132 api_nh = &api.nexthops[0];
133 api_nh->vrf_id = VRF_DEFAULT;
134
135 switch (api.prefix.family) {
136 case AF_INET:
137 if (api.prefix.prefixlen == IPV4_MAX_BITLEN &&
138 nexthop_ref &&
139 memcmp(&nexthop_ref->sin.sin_addr, &api.prefix.u.prefix4,
140 sizeof(struct in_addr)) == 0) {
141 nexthop_ref = NULL;
142 }
143 if (nexthop_ref) {
144 api_nh->gate.ipv4 = nexthop_ref->sin.sin_addr;
145 api_nh->type = NEXTHOP_TYPE_IPV4;
146 }
147 if (ifp) {
148 api_nh->ifindex = ifp->ifindex;
149 if (api_nh->type == NEXTHOP_TYPE_IPV4)
150 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
151 else
152 api_nh->type = NEXTHOP_TYPE_IFINDEX;
153 }
154 break;
155 case AF_INET6:
156 if (api.prefix.prefixlen == IPV6_MAX_BITLEN &&
157 nexthop_ref &&
158 memcmp(&nexthop_ref->sin6.sin6_addr, &api.prefix.u.prefix6,
159 sizeof(struct in6_addr)) == 0) {
160 nexthop_ref = NULL;
161 }
162 if (nexthop_ref) {
163 api_nh->gate.ipv6 = nexthop_ref->sin6.sin6_addr;
164 api_nh->type = NEXTHOP_TYPE_IPV6;
165 }
166 if (ifp) {
167 api_nh->ifindex = ifp->ifindex;
168 if (api_nh->type == NEXTHOP_TYPE_IPV6)
169 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
170 else
171 api_nh->type = NEXTHOP_TYPE_IFINDEX;
172 }
173 break;
174 }
175 if (mtu) {
176 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
177 api.mtu = mtu;
178 }
179
180 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
181 char buf[PREFIX_STRLEN];
182
183 zlog_debug(
184 "Zebra send: route %s %pFX nexthop %s metric %u count %d dev %s",
185 add ? "add" : "del", &api.prefix,
186 nexthop_ref ? inet_ntop(api.prefix.family,
187 &api_nh->gate,
188 buf, sizeof(buf))
189 : "<onlink>",
190 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
191 }
192
193 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
194 &api);
195 }
196
197 int nhrp_route_read(ZAPI_CALLBACK_ARGS)
198 {
199 struct zapi_route api;
200 struct zapi_nexthop *api_nh;
201 struct interface *ifp = NULL;
202 union sockunion nexthop_addr;
203 char buf[PREFIX_STRLEN];
204 int added;
205
206 if (zapi_route_decode(zclient->ibuf, &api) < 0)
207 return -1;
208
209 /* we completely ignore srcdest routes for now. */
210 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
211 return 0;
212
213 /* ignore our routes */
214 if (api.type == ZEBRA_ROUTE_NHRP)
215 return 0;
216
217 sockunion_family(&nexthop_addr) = AF_UNSPEC;
218 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
219 api_nh = &api.nexthops[0];
220
221 nexthop_addr.sa.sa_family = api.prefix.family;
222 switch (nexthop_addr.sa.sa_family) {
223 case AF_INET:
224 nexthop_addr.sin.sin_addr = api_nh->gate.ipv4;
225 break;
226 case AF_INET6:
227 nexthop_addr.sin6.sin6_addr = api_nh->gate.ipv6;
228 break;
229 }
230
231 if (api_nh->ifindex != IFINDEX_INTERNAL)
232 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
233 }
234
235 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
236 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %pFX via %s dev %s",
237 added ? "add" : "del", &api.prefix,
238 sockunion2str(&nexthop_addr, buf, sizeof(buf)),
239 ifp ? ifp->name : "(none)");
240
241 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, added ? ifp : NULL);
242 nhrp_shortcut_prefix_change(&api.prefix, !added);
243
244 return 0;
245 }
246
247 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
248 union sockunion *via, struct interface **ifp)
249 {
250 struct route_node *rn;
251 struct route_info *ri;
252 struct prefix lookup;
253 afi_t afi = family2afi(sockunion_family(addr));
254
255 sockunion2hostprefix(addr, &lookup);
256
257 rn = route_node_match(zebra_rib[afi], &lookup);
258 if (!rn)
259 return 0;
260
261 ri = rn->info;
262 if (ri->nhrp_ifp) {
263 debugf(NHRP_DEBUG_ROUTE, "lookup %pFX: nhrp_if=%s", &lookup,
264 ri->nhrp_ifp->name);
265
266 if (via)
267 sockunion_family(via) = AF_UNSPEC;
268 if (ifp)
269 *ifp = ri->nhrp_ifp;
270 } else {
271 debugf(NHRP_DEBUG_ROUTE, "lookup %pFX: zebra route dev %s",
272 &lookup, ri->ifp ? ri->ifp->name : "(none)");
273
274 if (via)
275 *via = ri->via;
276 if (ifp)
277 *ifp = ri->ifp;
278 }
279 if (p)
280 *p = rn->p;
281 route_unlock_node(rn);
282 return 1;
283 }
284
285 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
286 union sockunion *addr, struct prefix *p,
287 struct nhrp_peer **peer)
288 {
289 struct interface *ifp = in_ifp;
290 struct nhrp_interface *nifp;
291 struct nhrp_cache *c;
292 union sockunion via[4];
293 uint32_t network_id = 0;
294 afi_t afi = family2afi(sockunion_family(addr));
295 int i;
296
297 if (ifp) {
298 nifp = ifp->info;
299 network_id = nifp->afi[afi].network_id;
300
301 c = nhrp_cache_get(ifp, addr, 0);
302 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
303 if (p)
304 memset(p, 0, sizeof(*p));
305 return NHRP_ROUTE_LOCAL;
306 }
307 }
308
309 for (i = 0; i < 4; i++) {
310 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
311 return NHRP_ROUTE_BLACKHOLE;
312 if (ifp) {
313 /* Departing from nbma network? */
314 nifp = ifp->info;
315 if (network_id
316 && network_id != nifp->afi[afi].network_id)
317 return NHRP_ROUTE_OFF_NBMA;
318 }
319 if (sockunion_family(&via[i]) == AF_UNSPEC)
320 break;
321 /* Resolve via node, but return the prefix of first match */
322 addr = &via[i];
323 p = NULL;
324 }
325
326 if (ifp) {
327 c = nhrp_cache_get(ifp, addr, 0);
328 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
329 if (p)
330 memset(p, 0, sizeof(*p));
331 if (c->cur.type == NHRP_CACHE_LOCAL)
332 return NHRP_ROUTE_LOCAL;
333 if (peer)
334 *peer = nhrp_peer_ref(c->cur.peer);
335 return NHRP_ROUTE_NBMA_NEXTHOP;
336 }
337 }
338
339 return NHRP_ROUTE_BLACKHOLE;
340 }
341
342 static void nhrp_zebra_connected(struct zclient *zclient)
343 {
344 zclient_send_reg_requests(zclient, VRF_DEFAULT);
345 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
346 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
347 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
348 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
349 }
350
351 void nhrp_zebra_init(void)
352 {
353 zebra_rib[AFI_IP] = route_table_init();
354 zebra_rib[AFI_IP6] = route_table_init();
355
356 zclient = zclient_new(master, &zclient_options_default);
357 zclient->zebra_connected = nhrp_zebra_connected;
358 zclient->interface_address_add = nhrp_interface_address_add;
359 zclient->interface_address_delete = nhrp_interface_address_delete;
360 zclient->redistribute_route_add = nhrp_route_read;
361 zclient->redistribute_route_del = nhrp_route_read;
362
363 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
364 }
365
366 static void nhrp_table_node_cleanup(struct route_table *table,
367 struct route_node *node)
368 {
369 if (!node->info)
370 return;
371
372 XFREE(MTYPE_NHRP_ROUTE, node->info);
373 }
374
375 void nhrp_zebra_terminate(void)
376 {
377 zclient_stop(zclient);
378 zclient_free(zclient);
379
380 zebra_rib[AFI_IP]->cleanup = nhrp_table_node_cleanup;
381 zebra_rib[AFI_IP6]->cleanup = nhrp_table_node_cleanup;
382 route_table_finish(zebra_rib[AFI_IP]);
383 route_table_finish(zebra_rib[AFI_IP6]);
384 }