]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_shortcut.c
Merge pull request #8182 from mjstapp/topotest_start_tgen
[mirror_frr.git] / nhrpd / nhrp_shortcut.c
1 /* NHRP shortcut related 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 "thread.h"
18 #include "log.h"
19 #include "nhrp_protocol.h"
20
21 DEFINE_MTYPE_STATIC(NHRPD, NHRP_SHORTCUT, "NHRP shortcut");
22
23 static struct route_table *shortcut_rib[AFI_MAX];
24
25 static int nhrp_shortcut_do_purge(struct thread *t);
26 static void nhrp_shortcut_delete(struct nhrp_shortcut *s);
27 static void nhrp_shortcut_send_resolution_req(struct nhrp_shortcut *s);
28
29 static void nhrp_shortcut_check_use(struct nhrp_shortcut *s)
30 {
31 if (s->expiring && s->cache && s->cache->used) {
32 debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX used and expiring",
33 s->p);
34 nhrp_shortcut_send_resolution_req(s);
35 }
36 }
37
38 static int nhrp_shortcut_do_expire(struct thread *t)
39 {
40 struct nhrp_shortcut *s = THREAD_ARG(t);
41
42 s->t_timer = NULL;
43 thread_add_timer(master, nhrp_shortcut_do_purge, s, s->holding_time / 3,
44 &s->t_timer);
45 s->expiring = 1;
46 nhrp_shortcut_check_use(s);
47
48 return 0;
49 }
50
51 static void nhrp_shortcut_cache_notify(struct notifier_block *n,
52 unsigned long cmd)
53 {
54 char buf2[PREFIX_STRLEN];
55
56 struct nhrp_shortcut *s =
57 container_of(n, struct nhrp_shortcut, cache_notifier);
58 struct nhrp_cache *c = s->cache;
59
60 if (c)
61 sockunion2str(&c->remote_addr, buf2, sizeof(buf2));
62 else
63 snprintf(buf2, sizeof(buf2), "(unspec)");
64 switch (cmd) {
65 case NOTIFY_CACHE_UP:
66 if (!s->route_installed) {
67 debugf(NHRP_DEBUG_ROUTE,
68 "Shortcut: route install %pFX nh %s dev %s",
69 s->p, buf2, c && c->ifp ?
70 c->ifp->name : "<unk>");
71
72 nhrp_route_announce(1, s->type, s->p, c ? c->ifp : NULL,
73 c ? &c->remote_addr : NULL, 0);
74 s->route_installed = 1;
75 }
76 break;
77 case NOTIFY_CACHE_USED:
78 nhrp_shortcut_check_use(s);
79 break;
80 case NOTIFY_CACHE_DOWN:
81 case NOTIFY_CACHE_DELETE:
82 if (s->route_installed) {
83 nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL,
84 NULL, 0);
85 s->route_installed = 0;
86 }
87 if (cmd == NOTIFY_CACHE_DELETE)
88 nhrp_shortcut_delete(s);
89 break;
90 }
91 }
92
93 static void nhrp_shortcut_update_binding(struct nhrp_shortcut *s,
94 enum nhrp_cache_type type,
95 struct nhrp_cache *c, int holding_time)
96 {
97 s->type = type;
98 if (c != s->cache) {
99 if (s->cache) {
100 nhrp_cache_notify_del(s->cache, &s->cache_notifier);
101 s->cache = NULL;
102 }
103 s->cache = c;
104 if (s->cache) {
105 nhrp_cache_notify_add(s->cache, &s->cache_notifier,
106 nhrp_shortcut_cache_notify);
107 if (s->cache->route_installed) {
108 /* Force renewal of Zebra announce on prefix
109 * change */
110 s->route_installed = 0;
111 debugf(NHRP_DEBUG_ROUTE,
112 "Shortcut: forcing renewal of zebra announce on prefix change peer %pSU ht %u cur nbma %pSU dev %s",
113 &s->cache->remote_addr, holding_time,
114 &s->cache->cur.remote_nbma_natoa,
115 s->cache->ifp->name);
116 nhrp_shortcut_cache_notify(&s->cache_notifier,
117 NOTIFY_CACHE_UP);
118 }
119 }
120 if (!s->cache || !s->cache->route_installed) {
121 debugf(NHRP_DEBUG_ROUTE,
122 "Shortcut: notify cache down because cache?%s or ri?%s",
123 s->cache ? "yes" : "no",
124 s->cache ? (s->cache->route_installed ? "yes"
125 : "no")
126 : "n/a");
127 nhrp_shortcut_cache_notify(&s->cache_notifier,
128 NOTIFY_CACHE_DOWN);
129 }
130 }
131 if (s->type == NHRP_CACHE_NEGATIVE && !s->route_installed) {
132 nhrp_route_announce(1, s->type, s->p, NULL, NULL, 0);
133 s->route_installed = 1;
134 } else if (s->type == NHRP_CACHE_INVALID && s->route_installed) {
135 nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, NULL, 0);
136 s->route_installed = 0;
137 }
138
139 THREAD_OFF(s->t_timer);
140 if (holding_time) {
141 s->expiring = 0;
142 s->holding_time = holding_time;
143 thread_add_timer(master, nhrp_shortcut_do_expire, s,
144 2 * holding_time / 3, &s->t_timer);
145 }
146 }
147
148 static void nhrp_shortcut_delete(struct nhrp_shortcut *s)
149 {
150 struct route_node *rn;
151 afi_t afi = family2afi(PREFIX_FAMILY(s->p));
152
153 THREAD_OFF(s->t_timer);
154 nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid);
155
156 debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX purged", s->p);
157
158 nhrp_shortcut_update_binding(s, NHRP_CACHE_INVALID, NULL, 0);
159
160 /* Delete node */
161 rn = route_node_lookup(shortcut_rib[afi], s->p);
162 if (rn) {
163 XFREE(MTYPE_NHRP_SHORTCUT, rn->info);
164 rn->info = NULL;
165 route_unlock_node(rn);
166 route_unlock_node(rn);
167 }
168 }
169
170 static int nhrp_shortcut_do_purge(struct thread *t)
171 {
172 struct nhrp_shortcut *s = THREAD_ARG(t);
173 s->t_timer = NULL;
174 nhrp_shortcut_delete(s);
175 return 0;
176 }
177
178 static struct nhrp_shortcut *nhrp_shortcut_get(struct prefix *p)
179 {
180 struct nhrp_shortcut *s;
181 struct route_node *rn;
182 afi_t afi = family2afi(PREFIX_FAMILY(p));
183
184 if (!shortcut_rib[afi])
185 return 0;
186
187 rn = route_node_get(shortcut_rib[afi], p);
188 if (!rn->info) {
189 s = rn->info = XCALLOC(MTYPE_NHRP_SHORTCUT,
190 sizeof(struct nhrp_shortcut));
191 s->type = NHRP_CACHE_INVALID;
192 s->p = &rn->p;
193
194 debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX created", s->p);
195 } else {
196 s = rn->info;
197 route_unlock_node(rn);
198 }
199 return s;
200 }
201
202 static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid,
203 void *arg)
204 {
205 struct nhrp_packet_parser *pp = arg;
206 struct interface *ifp = pp->ifp;
207 struct nhrp_interface *nifp = ifp->info;
208 struct nhrp_shortcut *s =
209 container_of(reqid, struct nhrp_shortcut, reqid);
210 struct nhrp_shortcut *ps;
211 struct nhrp_extension_header *ext;
212 struct nhrp_cie_header *cie;
213 struct nhrp_cache *c = NULL;
214 struct nhrp_cache *c_dst = NULL;
215 union sockunion *proto, cie_proto, *nbma, cie_nbma, nat_nbma;
216 struct prefix prefix, route_prefix;
217 struct zbuf extpl;
218 int holding_time = pp->if_ad->holdtime;
219
220 nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid);
221 THREAD_OFF(s->t_timer);
222 thread_add_timer(master, nhrp_shortcut_do_purge, s, 1, &s->t_timer);
223
224 if (pp->hdr->type != NHRP_PACKET_RESOLUTION_REPLY) {
225 if (pp->hdr->type == NHRP_PACKET_ERROR_INDICATION
226 && pp->hdr->u.error.code
227 == NHRP_ERROR_PROTOCOL_ADDRESS_UNREACHABLE) {
228 debugf(NHRP_DEBUG_COMMON,
229 "Shortcut: Resolution: Protocol address unreachable");
230 nhrp_shortcut_update_binding(s, NHRP_CACHE_NEGATIVE,
231 NULL, holding_time);
232 } else {
233 debugf(NHRP_DEBUG_COMMON,
234 "Shortcut: Resolution failed");
235 }
236 return;
237 }
238
239 /* Minor sanity check */
240 prefix2sockunion(s->p, &cie_proto);
241 if (!sockunion_same(&cie_proto, &pp->dst_proto)) {
242 debugf(NHRP_DEBUG_COMMON,
243 "Shortcut: Warning dst_proto altered from %pSU to %pSU",
244 &cie_proto, &pp->dst_proto);
245 ;
246 }
247
248 /* One or more CIEs should be given as reply, we support only one */
249 cie = nhrp_cie_pull(&pp->payload, pp->hdr, &cie_nbma, &cie_proto);
250 if (!cie || cie->code != NHRP_CODE_SUCCESS) {
251 debugf(NHRP_DEBUG_COMMON, "Shortcut: CIE code %d",
252 cie ? cie->code : -1);
253 return;
254 }
255
256 proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto
257 : &pp->dst_proto;
258 if (cie->holding_time)
259 holding_time = htons(cie->holding_time);
260
261 prefix = *s->p;
262 prefix.prefixlen = cie->prefix_length;
263
264 /* Sanity check prefix length */
265 if (prefix.prefixlen >= 8 * prefix_blen(&prefix)
266 || prefix.prefixlen == 0) {
267 prefix.prefixlen = 8 * prefix_blen(&prefix);
268 } else if (nhrp_route_address(NULL, &pp->dst_proto, &route_prefix, NULL)
269 == NHRP_ROUTE_NBMA_NEXTHOP) {
270 if (prefix.prefixlen < route_prefix.prefixlen)
271 prefix.prefixlen = route_prefix.prefixlen;
272 }
273
274 /* Parse extensions */
275 memset(&nat_nbma, 0, sizeof(nat_nbma));
276 while ((ext = nhrp_ext_pull(&pp->extensions, &extpl)) != NULL) {
277 switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) {
278 case NHRP_EXTENSION_NAT_ADDRESS: {
279 struct nhrp_cie_header *cie_nat;
280
281 do {
282 union sockunion cie_nat_proto, cie_nat_nbma;
283
284 sockunion_family(&cie_nat_proto) = AF_UNSPEC;
285 sockunion_family(&cie_nat_nbma) = AF_UNSPEC;
286 cie_nat = nhrp_cie_pull(&extpl, pp->hdr,
287 &cie_nat_nbma,
288 &cie_nat_proto);
289 /* We are interested only in peer CIE */
290 if (cie_nat
291 && sockunion_same(&cie_nat_proto, proto)) {
292 nat_nbma = cie_nat_nbma;
293 }
294 } while (cie_nat);
295 } break;
296 default:
297 break;
298 }
299 }
300
301 /* Update cache entry for the protocol to nbma binding */
302 if (sockunion_family(&nat_nbma) != AF_UNSPEC) {
303 debugf(NHRP_DEBUG_COMMON,
304 "Shortcut: NAT detected (NAT extension) proto %pSU NBMA %pSU claimed-NBMA %pSU",
305 proto, &nat_nbma, &cie_nbma);
306 nbma = &nat_nbma;
307 }
308 /* For NHRP resolution reply the cie_nbma in mandatory part is the
309 * address of the actual address of the sender
310 */
311 else if (!sockunion_same(&cie_nbma, &pp->peer->vc->remote.nbma)
312 && !nhrp_nhs_match_ip(&pp->peer->vc->remote.nbma, nifp)) {
313 debugf(NHRP_DEBUG_COMMON,
314 "Shortcut: NAT detected (no NAT Extension) proto %pSU NBMA %pSU claimed-NBMA %pSU",
315 proto, &pp->peer->vc->remote.nbma, &cie_nbma);
316 nbma = &pp->peer->vc->remote.nbma;
317 nat_nbma = *nbma;
318 } else {
319 nbma = &cie_nbma;
320 }
321
322 debugf(NHRP_DEBUG_COMMON,
323 "Shortcut: %pFX is at proto %pSU dst_proto %pSU NBMA %pSU cie-holdtime %d",
324 &prefix, proto, &pp->dst_proto, nbma,
325 htons(cie->holding_time));
326
327 if (sockunion_family(nbma)) {
328 c = nhrp_cache_get(pp->ifp, proto, 1);
329 if (c) {
330 debugf(NHRP_DEBUG_COMMON,
331 "Shortcut: cache found, update binding");
332 nhrp_cache_update_binding(c, NHRP_CACHE_DYNAMIC,
333 holding_time,
334 nhrp_peer_get(pp->ifp, nbma),
335 htons(cie->mtu),
336 nbma,
337 &cie_nbma);
338 } else {
339 debugf(NHRP_DEBUG_COMMON,
340 "Shortcut: no cache for proto %pSU", proto);
341 }
342
343 /* Update cache binding for dst_proto as well */
344 if (sockunion_cmp(proto, &pp->dst_proto)) {
345 c_dst = nhrp_cache_get(pp->ifp, &pp->dst_proto, 1);
346 if (c_dst) {
347 debugf(NHRP_DEBUG_COMMON,
348 "Shortcut: cache found, update binding");
349 nhrp_cache_update_binding(c_dst,
350 NHRP_CACHE_DYNAMIC,
351 holding_time,
352 nhrp_peer_get(pp->ifp, nbma),
353 htons(cie->mtu),
354 nbma,
355 &cie_nbma);
356 } else {
357 debugf(NHRP_DEBUG_COMMON,
358 "Shortcut: no cache for proto %pSU",
359 &pp->dst_proto);
360 }
361 }
362 }
363
364 /* Update shortcut entry for subnet to protocol gw binding */
365 if (c) {
366 ps = nhrp_shortcut_get(&prefix);
367 if (ps) {
368 ps->addr = s->addr;
369 debugf(NHRP_DEBUG_COMMON,
370 "Shortcut: calling update_binding");
371 nhrp_shortcut_update_binding(ps, NHRP_CACHE_DYNAMIC, c,
372 holding_time);
373 } else {
374 debugf(NHRP_DEBUG_COMMON,
375 "Shortcut: proto diff but no ps");
376 }
377 } else {
378 debugf(NHRP_DEBUG_COMMON,
379 "NO Shortcut because c NULL?%s or same proto?%s",
380 c ? "no" : "yes",
381 proto && pp && sockunion_same(proto, &pp->dst_proto)
382 ? "yes"
383 : "no");
384 }
385
386 debugf(NHRP_DEBUG_COMMON, "Shortcut: Resolution reply handled");
387 }
388
389 static void nhrp_shortcut_send_resolution_req(struct nhrp_shortcut *s)
390 {
391 struct zbuf *zb;
392 struct nhrp_packet_header *hdr;
393 struct interface *ifp;
394 struct nhrp_interface *nifp;
395 struct nhrp_afi_data *if_ad;
396 struct nhrp_peer *peer;
397 struct nhrp_cie_header *cie;
398 struct nhrp_extension_header *ext;
399
400 if (nhrp_route_address(NULL, &s->addr, NULL, &peer)
401 != NHRP_ROUTE_NBMA_NEXTHOP)
402 return;
403
404 if (s->type == NHRP_CACHE_INVALID || s->type == NHRP_CACHE_NEGATIVE)
405 s->type = NHRP_CACHE_INCOMPLETE;
406
407 ifp = peer->ifp;
408 nifp = ifp->info;
409
410 /* Create request */
411 zb = zbuf_alloc(1500);
412 hdr = nhrp_packet_push(
413 zb, NHRP_PACKET_RESOLUTION_REQUEST, &nifp->nbma,
414 &nifp->afi[family2afi(sockunion_family(&s->addr))].addr,
415 &s->addr);
416 hdr->u.request_id =
417 htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &s->reqid,
418 nhrp_shortcut_recv_resolution_rep));
419 hdr->flags = htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER
420 | NHRP_FLAG_RESOLUTION_AUTHORATIVE
421 | NHRP_FLAG_RESOLUTION_SOURCE_STABLE);
422
423 /* RFC2332 - One or zero CIEs, if CIE is present contains:
424 * - Prefix length: widest acceptable prefix we accept (if U set, 0xff)
425 * - MTU: MTU of the source station
426 * - Holding Time: Max time to cache the source information
427 * */
428 /* FIXME: push CIE for each local protocol address */
429 cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, NULL, NULL);
430 cie->prefix_length = 0xff;
431 if_ad = &nifp->afi[family2afi(sockunion_family(&s->addr))];
432 cie->holding_time = htons(if_ad->holdtime);
433 cie->mtu = htons(if_ad->mtu);
434 debugf(NHRP_DEBUG_COMMON,
435 "Shortcut res_req: set cie ht to %u and mtu to %u. shortcut ht is %u",
436 ntohs(cie->holding_time), ntohs(cie->mtu), s->holding_time);
437
438 nhrp_ext_request(zb, hdr, ifp);
439
440 /* Cisco NAT detection extension */
441 hdr->flags |= htons(NHRP_FLAG_RESOLUTION_NAT);
442 ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS);
443 if (sockunion_family(&nifp->nat_nbma) != AF_UNSPEC) {
444 cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nat_nbma,
445 &if_ad->addr);
446 cie->prefix_length = 8 * sockunion_get_addrlen(&if_ad->addr);
447 cie->mtu = htons(if_ad->mtu);
448 nhrp_ext_complete(zb, ext);
449 }
450
451 nhrp_packet_complete(zb, hdr);
452
453 nhrp_peer_send(peer, zb);
454 nhrp_peer_unref(peer);
455 zbuf_free(zb);
456 }
457
458 void nhrp_shortcut_initiate(union sockunion *addr)
459 {
460 struct prefix p;
461 struct nhrp_shortcut *s;
462
463 if (!sockunion2hostprefix(addr, &p))
464 return;
465
466 s = nhrp_shortcut_get(&p);
467 if (s && s->type != NHRP_CACHE_INCOMPLETE) {
468 s->addr = *addr;
469 THREAD_OFF(s->t_timer);
470 thread_add_timer(master, nhrp_shortcut_do_purge, s, 30,
471 &s->t_timer);
472 nhrp_shortcut_send_resolution_req(s);
473 }
474 }
475
476 void nhrp_shortcut_init(void)
477 {
478 shortcut_rib[AFI_IP] = route_table_init();
479 shortcut_rib[AFI_IP6] = route_table_init();
480 }
481
482 void nhrp_shortcut_terminate(void)
483 {
484 route_table_finish(shortcut_rib[AFI_IP]);
485 route_table_finish(shortcut_rib[AFI_IP6]);
486 }
487
488 void nhrp_shortcut_foreach(afi_t afi,
489 void (*cb)(struct nhrp_shortcut *, void *),
490 void *ctx)
491 {
492 struct route_table *rt = shortcut_rib[afi];
493 struct route_node *rn;
494 route_table_iter_t iter;
495
496 if (!rt)
497 return;
498
499 route_table_iter_init(&iter, rt);
500 while ((rn = route_table_iter_next(&iter)) != NULL) {
501 if (rn->info)
502 cb(rn->info, ctx);
503 }
504 route_table_iter_cleanup(&iter);
505 }
506
507 struct purge_ctx {
508 const struct prefix *p;
509 int deleted;
510 };
511
512 void nhrp_shortcut_purge(struct nhrp_shortcut *s, int force)
513 {
514 THREAD_OFF(s->t_timer);
515 nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid);
516
517 if (force) {
518 /* Immediate purge on route with draw or pending shortcut */
519 thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 5,
520 &s->t_timer);
521 } else {
522 /* Soft expire - force immediate renewal, but purge
523 * in few seconds to make sure stale route is not
524 * used too long. In practice most purges are caused
525 * by hub bgp change, but target usually stays same.
526 * This allows to keep nhrp route up, and to not
527 * cause temporary rerouting via hubs causing latency
528 * jitter. */
529 thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 3000,
530 &s->t_timer);
531 s->expiring = 1;
532 nhrp_shortcut_check_use(s);
533 }
534 }
535
536 static void nhrp_shortcut_purge_prefix(struct nhrp_shortcut *s, void *ctx)
537 {
538 struct purge_ctx *pctx = ctx;
539
540 if (prefix_match(pctx->p, s->p))
541 nhrp_shortcut_purge(s, pctx->deleted || !s->cache);
542 }
543
544 void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted)
545 {
546 struct purge_ctx pctx = {
547 .p = p, .deleted = deleted,
548 };
549 nhrp_shortcut_foreach(family2afi(PREFIX_FAMILY(p)),
550 nhrp_shortcut_purge_prefix, &pctx);
551 }