]>
Commit | Line | Data |
---|---|---|
2fb975da TT |
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 | ||
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 "thread.h" | |
18 | #include "log.h" | |
19 | #include "nhrp_protocol.h" | |
20 | ||
bf8d3d6a | 21 | DEFINE_MTYPE_STATIC(NHRPD, NHRP_SHORTCUT, "NHRP shortcut"); |
819dc8bb | 22 | |
2fb975da TT |
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 | { | |
2fb975da | 31 | if (s->expiring && s->cache && s->cache->used) { |
2dbe669b DA |
32 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX used and expiring", |
33 | s->p); | |
2fb975da TT |
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; | |
996c9314 LB |
43 | thread_add_timer(master, nhrp_shortcut_do_purge, s, s->holding_time / 3, |
44 | &s->t_timer); | |
2fb975da TT |
45 | s->expiring = 1; |
46 | nhrp_shortcut_check_use(s); | |
47 | ||
48 | return 0; | |
49 | } | |
50 | ||
996c9314 LB |
51 | static void nhrp_shortcut_cache_notify(struct notifier_block *n, |
52 | unsigned long cmd) | |
2fb975da | 53 | { |
ef91ff04 PG |
54 | char buf2[PREFIX_STRLEN]; |
55 | ||
996c9314 LB |
56 | struct nhrp_shortcut *s = |
57 | container_of(n, struct nhrp_shortcut, cache_notifier); | |
ef91ff04 | 58 | struct nhrp_cache *c = s->cache; |
2fb975da | 59 | |
ef91ff04 PG |
60 | if (c) |
61 | sockunion2str(&c->remote_addr, buf2, sizeof(buf2)); | |
62 | else | |
63 | snprintf(buf2, sizeof(buf2), "(unspec)"); | |
2fb975da TT |
64 | switch (cmd) { |
65 | case NOTIFY_CACHE_UP: | |
66 | if (!s->route_installed) { | |
912c556b | 67 | debugf(NHRP_DEBUG_ROUTE, |
ef91ff04 PG |
68 | "Shortcut: route install %pFX nh %s dev %s", |
69 | s->p, buf2, c && c->ifp ? | |
70 | c->ifp->name : "<unk>"); | |
912c556b | 71 | |
ef91ff04 PG |
72 | nhrp_route_announce(1, s->type, s->p, c ? c->ifp : NULL, |
73 | c ? &c->remote_addr : NULL, 0); | |
2fb975da TT |
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) { | |
996c9314 LB |
83 | nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, |
84 | NULL, 0); | |
2fb975da TT |
85 | s->route_installed = 0; |
86 | } | |
87 | if (cmd == NOTIFY_CACHE_DELETE) | |
88 | nhrp_shortcut_delete(s); | |
89 | break; | |
90 | } | |
91 | } | |
92 | ||
996c9314 LB |
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) | |
2fb975da TT |
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) { | |
996c9314 LB |
105 | nhrp_cache_notify_add(s->cache, &s->cache_notifier, |
106 | nhrp_shortcut_cache_notify); | |
2fb975da | 107 | if (s->cache->route_installed) { |
996c9314 LB |
108 | /* Force renewal of Zebra announce on prefix |
109 | * change */ | |
2fb975da | 110 | s->route_installed = 0; |
912c556b | 111 | debugf(NHRP_DEBUG_ROUTE, |
b6c48481 DS |
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, | |
912c556b | 115 | s->cache->ifp->name); |
996c9314 LB |
116 | nhrp_shortcut_cache_notify(&s->cache_notifier, |
117 | NOTIFY_CACHE_UP); | |
2fb975da TT |
118 | } |
119 | } | |
912c556b GN |
120 | if (!s->cache || !s->cache->route_installed) { |
121 | debugf(NHRP_DEBUG_ROUTE, | |
5f36c26c | 122 | "Shortcut: notify cache down because cache?%s or ri?%s", |
912c556b GN |
123 | s->cache ? "yes" : "no", |
124 | s->cache ? (s->cache->route_installed ? "yes" | |
125 | : "no") | |
126 | : "n/a"); | |
996c9314 LB |
127 | nhrp_shortcut_cache_notify(&s->cache_notifier, |
128 | NOTIFY_CACHE_DOWN); | |
912c556b | 129 | } |
2fb975da TT |
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; | |
ffa2c898 QY |
143 | thread_add_timer(master, nhrp_shortcut_do_expire, s, |
144 | 2 * holding_time / 3, &s->t_timer); | |
2fb975da TT |
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)); | |
2fb975da TT |
152 | |
153 | THREAD_OFF(s->t_timer); | |
154 | nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); | |
155 | ||
2dbe669b | 156 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX purged", s->p); |
2fb975da TT |
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); | |
912c556b | 164 | rn->info = NULL; |
2fb975da TT |
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; | |
2fb975da TT |
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) { | |
996c9314 LB |
189 | s = rn->info = XCALLOC(MTYPE_NHRP_SHORTCUT, |
190 | sizeof(struct nhrp_shortcut)); | |
2fb975da TT |
191 | s->type = NHRP_CACHE_INVALID; |
192 | s->p = &rn->p; | |
193 | ||
2dbe669b | 194 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %pFX created", s->p); |
2fb975da TT |
195 | } else { |
196 | s = rn->info; | |
197 | route_unlock_node(rn); | |
198 | } | |
199 | return s; | |
200 | } | |
201 | ||
996c9314 LB |
202 | static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, |
203 | void *arg) | |
2fb975da TT |
204 | { |
205 | struct nhrp_packet_parser *pp = arg; | |
bb58f442 GG |
206 | struct interface *ifp = pp->ifp; |
207 | struct nhrp_interface *nifp = ifp->info; | |
996c9314 LB |
208 | struct nhrp_shortcut *s = |
209 | container_of(reqid, struct nhrp_shortcut, reqid); | |
2fb975da TT |
210 | struct nhrp_shortcut *ps; |
211 | struct nhrp_extension_header *ext; | |
212 | struct nhrp_cie_header *cie; | |
213 | struct nhrp_cache *c = NULL; | |
00683a14 | 214 | struct nhrp_cache *c_dst = NULL; |
912c556b | 215 | union sockunion *proto, cie_proto, *nbma, cie_nbma, nat_nbma; |
2fb975da TT |
216 | struct prefix prefix, route_prefix; |
217 | struct zbuf extpl; | |
2fb975da TT |
218 | int holding_time = pp->if_ad->holdtime; |
219 | ||
220 | nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); | |
221 | THREAD_OFF(s->t_timer); | |
ffa2c898 | 222 | thread_add_timer(master, nhrp_shortcut_do_purge, s, 1, &s->t_timer); |
2fb975da TT |
223 | |
224 | if (pp->hdr->type != NHRP_PACKET_RESOLUTION_REPLY) { | |
996c9314 LB |
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); | |
2fb975da | 232 | } else { |
996c9314 LB |
233 | debugf(NHRP_DEBUG_COMMON, |
234 | "Shortcut: Resolution failed"); | |
2fb975da TT |
235 | } |
236 | return; | |
237 | } | |
238 | ||
2fb975da TT |
239 | /* Minor sanity check */ |
240 | prefix2sockunion(s->p, &cie_proto); | |
241 | if (!sockunion_same(&cie_proto, &pp->dst_proto)) { | |
996c9314 | 242 | debugf(NHRP_DEBUG_COMMON, |
b6c48481 DS |
243 | "Shortcut: Warning dst_proto altered from %pSU to %pSU", |
244 | &cie_proto, &pp->dst_proto); | |
245 | ; | |
2fb975da TT |
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) { | |
996c9314 LB |
251 | debugf(NHRP_DEBUG_COMMON, "Shortcut: CIE code %d", |
252 | cie ? cie->code : -1); | |
2fb975da TT |
253 | return; |
254 | } | |
255 | ||
996c9314 LB |
256 | proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto |
257 | : &pp->dst_proto; | |
2fb975da TT |
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 */ | |
996c9314 LB |
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) { | |
2fb975da TT |
270 | if (prefix.prefixlen < route_prefix.prefixlen) |
271 | prefix.prefixlen = route_prefix.prefixlen; | |
272 | } | |
273 | ||
9c292647 AL |
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; | |
00683a14 | 280 | |
9c292647 AL |
281 | do { |
282 | union sockunion cie_nat_proto, cie_nat_nbma; | |
00683a14 | 283 | |
9c292647 AL |
284 | sockunion_family(&cie_nat_proto) = AF_UNSPEC; |
285 | sockunion_family(&cie_nat_nbma) = AF_UNSPEC; | |
611915ae AL |
286 | cie_nat = nhrp_cie_pull(&extpl, pp->hdr, |
287 | &cie_nat_nbma, | |
288 | &cie_nat_proto); | |
9c292647 | 289 | /* We are interested only in peer CIE */ |
611915ae AL |
290 | if (cie_nat |
291 | && sockunion_same(&cie_nat_proto, proto)) { | |
9c292647 AL |
292 | nat_nbma = cie_nat_nbma; |
293 | } | |
294 | } while (cie_nat); | |
295 | } break; | |
296 | default: | |
297 | break; | |
298 | } | |
299 | } | |
2fb975da TT |
300 | |
301 | /* Update cache entry for the protocol to nbma binding */ | |
bb58f442 | 302 | if (sockunion_family(&nat_nbma) != AF_UNSPEC) { |
611915ae | 303 | debugf(NHRP_DEBUG_COMMON, |
0551aead | 304 | "Shortcut: NAT detected (NAT extension) proto %pSU NBMA %pSU claimed-NBMA %pSU", |
4ddc702a | 305 | proto, &nat_nbma, &cie_nbma); |
2fb975da | 306 | nbma = &nat_nbma; |
611915ae AL |
307 | } |
308 | /* For NHRP resolution reply the cie_nbma in mandatory part is the | |
00683a14 RD |
309 | * address of the actual address of the sender |
310 | */ | |
611915ae AL |
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, | |
0551aead | 314 | "Shortcut: NAT detected (no NAT Extension) proto %pSU NBMA %pSU claimed-NBMA %pSU", |
4ddc702a | 315 | proto, &pp->peer->vc->remote.nbma, &cie_nbma); |
bb58f442 GG |
316 | nbma = &pp->peer->vc->remote.nbma; |
317 | nat_nbma = *nbma; | |
318 | } else { | |
2fb975da | 319 | nbma = &cie_nbma; |
bb58f442 GG |
320 | } |
321 | ||
996c9314 | 322 | debugf(NHRP_DEBUG_COMMON, |
4ddc702a RD |
323 | "Shortcut: %pFX is at proto %pSU dst_proto %pSU NBMA %pSU cie-holdtime %d", |
324 | &prefix, proto, &pp->dst_proto, nbma, | |
996c9314 | 325 | htons(cie->holding_time)); |
912c556b | 326 | |
2fb975da TT |
327 | if (sockunion_family(nbma)) { |
328 | c = nhrp_cache_get(pp->ifp, proto, 1); | |
329 | if (c) { | |
912c556b GN |
330 | debugf(NHRP_DEBUG_COMMON, |
331 | "Shortcut: cache found, update binding"); | |
332 | nhrp_cache_update_binding(c, NHRP_CACHE_DYNAMIC, | |
996c9314 LB |
333 | holding_time, |
334 | nhrp_peer_get(pp->ifp, nbma), | |
00683a14 RD |
335 | htons(cie->mtu), |
336 | nbma, | |
337 | &cie_nbma); | |
912c556b GN |
338 | } else { |
339 | debugf(NHRP_DEBUG_COMMON, | |
4ddc702a | 340 | "Shortcut: no cache for proto %pSU", proto); |
2fb975da | 341 | } |
ac95bcef GG |
342 | |
343 | /* Update cache binding for dst_proto as well */ | |
bb58f442 | 344 | if (sockunion_cmp(proto, &pp->dst_proto)) { |
00683a14 RD |
345 | c_dst = nhrp_cache_get(pp->ifp, &pp->dst_proto, 1); |
346 | if (c_dst) { | |
ac95bcef | 347 | debugf(NHRP_DEBUG_COMMON, |
00683a14 RD |
348 | "Shortcut: cache found, update binding"); |
349 | nhrp_cache_update_binding(c_dst, | |
350 | NHRP_CACHE_DYNAMIC, | |
ac95bcef GG |
351 | holding_time, |
352 | nhrp_peer_get(pp->ifp, nbma), | |
00683a14 RD |
353 | htons(cie->mtu), |
354 | nbma, | |
355 | &cie_nbma); | |
ac95bcef GG |
356 | } else { |
357 | debugf(NHRP_DEBUG_COMMON, | |
4ddc702a RD |
358 | "Shortcut: no cache for proto %pSU", |
359 | &pp->dst_proto); | |
ac95bcef GG |
360 | } |
361 | } | |
2fb975da TT |
362 | } |
363 | ||
364 | /* Update shortcut entry for subnet to protocol gw binding */ | |
912c556b | 365 | if (c) { |
2fb975da TT |
366 | ps = nhrp_shortcut_get(&prefix); |
367 | if (ps) { | |
368 | ps->addr = s->addr; | |
912c556b | 369 | debugf(NHRP_DEBUG_COMMON, |
5f36c26c | 370 | "Shortcut: calling update_binding"); |
912c556b | 371 | nhrp_shortcut_update_binding(ps, NHRP_CACHE_DYNAMIC, c, |
996c9314 | 372 | holding_time); |
912c556b GN |
373 | } else { |
374 | debugf(NHRP_DEBUG_COMMON, | |
375 | "Shortcut: proto diff but no ps"); | |
2fb975da | 376 | } |
912c556b GN |
377 | } else { |
378 | debugf(NHRP_DEBUG_COMMON, | |
5f36c26c | 379 | "NO Shortcut because c NULL?%s or same proto?%s", |
912c556b GN |
380 | c ? "no" : "yes", |
381 | proto && pp && sockunion_same(proto, &pp->dst_proto) | |
382 | ? "yes" | |
383 | : "no"); | |
2fb975da TT |
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; | |
47d40757 | 395 | struct nhrp_afi_data *if_ad; |
2fb975da | 396 | struct nhrp_peer *peer; |
47d40757 | 397 | struct nhrp_cie_header *cie; |
c2cffffb | 398 | struct nhrp_extension_header *ext; |
2fb975da | 399 | |
996c9314 LB |
400 | if (nhrp_route_address(NULL, &s->addr, NULL, &peer) |
401 | != NHRP_ROUTE_NBMA_NEXTHOP) | |
2fb975da TT |
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); | |
996c9314 LB |
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); | |
2fb975da TT |
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 | * */ | |
47d40757 GN |
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, | |
5f36c26c | 435 | "Shortcut res_req: set cie ht to %u and mtu to %u. shortcut ht is %u", |
47d40757 | 436 | ntohs(cie->holding_time), ntohs(cie->mtu), s->holding_time); |
2fb975da TT |
437 | |
438 | nhrp_ext_request(zb, hdr, ifp); | |
439 | ||
440 | /* Cisco NAT detection extension */ | |
441 | hdr->flags |= htons(NHRP_FLAG_RESOLUTION_NAT); | |
c2cffffb | 442 | ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS); |
611915ae AL |
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); | |
999a07f0 | 446 | cie->prefix_length = 8 * sockunion_get_addrlen(&if_ad->addr); |
1e52c954 | 447 | cie->mtu = htons(if_ad->mtu); |
c2cffffb GG |
448 | nhrp_ext_complete(zb, ext); |
449 | } | |
2fb975da TT |
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 | ||
0154d8ce DS |
463 | if (!sockunion2hostprefix(addr, &p)) |
464 | return; | |
465 | ||
2fb975da TT |
466 | s = nhrp_shortcut_get(&p); |
467 | if (s && s->type != NHRP_CACHE_INCOMPLETE) { | |
468 | s->addr = *addr; | |
469 | THREAD_OFF(s->t_timer); | |
ffa2c898 QY |
470 | thread_add_timer(master, nhrp_shortcut_do_purge, s, 30, |
471 | &s->t_timer); | |
2fb975da TT |
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 | ||
996c9314 LB |
488 | void nhrp_shortcut_foreach(afi_t afi, |
489 | void (*cb)(struct nhrp_shortcut *, void *), | |
490 | void *ctx) | |
2fb975da TT |
491 | { |
492 | struct route_table *rt = shortcut_rib[afi]; | |
493 | struct route_node *rn; | |
494 | route_table_iter_t iter; | |
495 | ||
996c9314 LB |
496 | if (!rt) |
497 | return; | |
2fb975da TT |
498 | |
499 | route_table_iter_init(&iter, rt); | |
500 | while ((rn = route_table_iter_next(&iter)) != NULL) { | |
996c9314 LB |
501 | if (rn->info) |
502 | cb(rn->info, ctx); | |
2fb975da TT |
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 */ | |
ffa2c898 QY |
519 | thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 5, |
520 | &s->t_timer); | |
2fb975da TT |
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. */ | |
ffa2c898 QY |
529 | thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 3000, |
530 | &s->t_timer); | |
2fb975da TT |
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 = { | |
996c9314 | 547 | .p = p, .deleted = deleted, |
2fb975da | 548 | }; |
996c9314 LB |
549 | nhrp_shortcut_foreach(family2afi(PREFIX_FAMILY(p)), |
550 | nhrp_shortcut_purge_prefix, &pctx); | |
2fb975da | 551 | } |