]>
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 | ||
819dc8bb DL |
21 | DEFINE_MTYPE_STATIC(NHRPD, NHRP_SHORTCUT, "NHRP shortcut") |
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 | { | |
31 | char buf[PREFIX_STRLEN]; | |
32 | ||
33 | if (s->expiring && s->cache && s->cache->used) { | |
34 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %s used and expiring", | |
996c9314 | 35 | prefix2str(s->p, buf, sizeof buf)); |
2fb975da TT |
36 | nhrp_shortcut_send_resolution_req(s); |
37 | } | |
38 | } | |
39 | ||
40 | static int nhrp_shortcut_do_expire(struct thread *t) | |
41 | { | |
42 | struct nhrp_shortcut *s = THREAD_ARG(t); | |
43 | ||
44 | s->t_timer = NULL; | |
996c9314 LB |
45 | thread_add_timer(master, nhrp_shortcut_do_purge, s, s->holding_time / 3, |
46 | &s->t_timer); | |
2fb975da TT |
47 | s->expiring = 1; |
48 | nhrp_shortcut_check_use(s); | |
49 | ||
50 | return 0; | |
51 | } | |
52 | ||
996c9314 LB |
53 | static void nhrp_shortcut_cache_notify(struct notifier_block *n, |
54 | unsigned long cmd) | |
2fb975da | 55 | { |
996c9314 LB |
56 | struct nhrp_shortcut *s = |
57 | container_of(n, struct nhrp_shortcut, cache_notifier); | |
2fb975da TT |
58 | |
59 | switch (cmd) { | |
60 | case NOTIFY_CACHE_UP: | |
61 | if (!s->route_installed) { | |
996c9314 LB |
62 | nhrp_route_announce(1, s->type, s->p, NULL, |
63 | &s->cache->remote_addr, 0); | |
2fb975da TT |
64 | s->route_installed = 1; |
65 | } | |
66 | break; | |
67 | case NOTIFY_CACHE_USED: | |
68 | nhrp_shortcut_check_use(s); | |
69 | break; | |
70 | case NOTIFY_CACHE_DOWN: | |
71 | case NOTIFY_CACHE_DELETE: | |
72 | if (s->route_installed) { | |
996c9314 LB |
73 | nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, |
74 | NULL, 0); | |
2fb975da TT |
75 | s->route_installed = 0; |
76 | } | |
77 | if (cmd == NOTIFY_CACHE_DELETE) | |
78 | nhrp_shortcut_delete(s); | |
79 | break; | |
80 | } | |
81 | } | |
82 | ||
996c9314 LB |
83 | static void nhrp_shortcut_update_binding(struct nhrp_shortcut *s, |
84 | enum nhrp_cache_type type, | |
85 | struct nhrp_cache *c, int holding_time) | |
2fb975da TT |
86 | { |
87 | s->type = type; | |
88 | if (c != s->cache) { | |
89 | if (s->cache) { | |
90 | nhrp_cache_notify_del(s->cache, &s->cache_notifier); | |
91 | s->cache = NULL; | |
92 | } | |
93 | s->cache = c; | |
94 | if (s->cache) { | |
996c9314 LB |
95 | nhrp_cache_notify_add(s->cache, &s->cache_notifier, |
96 | nhrp_shortcut_cache_notify); | |
2fb975da | 97 | if (s->cache->route_installed) { |
996c9314 LB |
98 | /* Force renewal of Zebra announce on prefix |
99 | * change */ | |
2fb975da | 100 | s->route_installed = 0; |
996c9314 LB |
101 | nhrp_shortcut_cache_notify(&s->cache_notifier, |
102 | NOTIFY_CACHE_UP); | |
2fb975da TT |
103 | } |
104 | } | |
105 | if (!s->cache || !s->cache->route_installed) | |
996c9314 LB |
106 | nhrp_shortcut_cache_notify(&s->cache_notifier, |
107 | NOTIFY_CACHE_DOWN); | |
2fb975da TT |
108 | } |
109 | if (s->type == NHRP_CACHE_NEGATIVE && !s->route_installed) { | |
110 | nhrp_route_announce(1, s->type, s->p, NULL, NULL, 0); | |
111 | s->route_installed = 1; | |
112 | } else if (s->type == NHRP_CACHE_INVALID && s->route_installed) { | |
113 | nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, NULL, 0); | |
114 | s->route_installed = 0; | |
115 | } | |
116 | ||
117 | THREAD_OFF(s->t_timer); | |
118 | if (holding_time) { | |
119 | s->expiring = 0; | |
120 | s->holding_time = holding_time; | |
ffa2c898 QY |
121 | thread_add_timer(master, nhrp_shortcut_do_expire, s, |
122 | 2 * holding_time / 3, &s->t_timer); | |
2fb975da TT |
123 | } |
124 | } | |
125 | ||
126 | static void nhrp_shortcut_delete(struct nhrp_shortcut *s) | |
127 | { | |
128 | struct route_node *rn; | |
129 | afi_t afi = family2afi(PREFIX_FAMILY(s->p)); | |
130 | char buf[PREFIX_STRLEN]; | |
131 | ||
132 | THREAD_OFF(s->t_timer); | |
133 | nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); | |
134 | ||
135 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %s purged", | |
996c9314 | 136 | prefix2str(s->p, buf, sizeof buf)); |
2fb975da TT |
137 | |
138 | nhrp_shortcut_update_binding(s, NHRP_CACHE_INVALID, NULL, 0); | |
139 | ||
140 | /* Delete node */ | |
141 | rn = route_node_lookup(shortcut_rib[afi], s->p); | |
142 | if (rn) { | |
143 | XFREE(MTYPE_NHRP_SHORTCUT, rn->info); | |
2fb975da TT |
144 | route_unlock_node(rn); |
145 | route_unlock_node(rn); | |
146 | } | |
147 | } | |
148 | ||
149 | static int nhrp_shortcut_do_purge(struct thread *t) | |
150 | { | |
151 | struct nhrp_shortcut *s = THREAD_ARG(t); | |
152 | s->t_timer = NULL; | |
153 | nhrp_shortcut_delete(s); | |
154 | return 0; | |
155 | } | |
156 | ||
157 | static struct nhrp_shortcut *nhrp_shortcut_get(struct prefix *p) | |
158 | { | |
159 | struct nhrp_shortcut *s; | |
160 | struct route_node *rn; | |
161 | char buf[PREFIX_STRLEN]; | |
162 | afi_t afi = family2afi(PREFIX_FAMILY(p)); | |
163 | ||
164 | if (!shortcut_rib[afi]) | |
165 | return 0; | |
166 | ||
167 | rn = route_node_get(shortcut_rib[afi], p); | |
168 | if (!rn->info) { | |
996c9314 LB |
169 | s = rn->info = XCALLOC(MTYPE_NHRP_SHORTCUT, |
170 | sizeof(struct nhrp_shortcut)); | |
2fb975da TT |
171 | s->type = NHRP_CACHE_INVALID; |
172 | s->p = &rn->p; | |
173 | ||
174 | debugf(NHRP_DEBUG_ROUTE, "Shortcut %s created", | |
996c9314 | 175 | prefix2str(s->p, buf, sizeof buf)); |
2fb975da TT |
176 | } else { |
177 | s = rn->info; | |
178 | route_unlock_node(rn); | |
179 | } | |
180 | return s; | |
181 | } | |
182 | ||
996c9314 LB |
183 | static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, |
184 | void *arg) | |
2fb975da TT |
185 | { |
186 | struct nhrp_packet_parser *pp = arg; | |
996c9314 LB |
187 | struct nhrp_shortcut *s = |
188 | container_of(reqid, struct nhrp_shortcut, reqid); | |
2fb975da TT |
189 | struct nhrp_shortcut *ps; |
190 | struct nhrp_extension_header *ext; | |
191 | struct nhrp_cie_header *cie; | |
192 | struct nhrp_cache *c = NULL; | |
996c9314 LB |
193 | union sockunion *proto, cie_proto, *nbma, *nbma_natoa, cie_nbma, |
194 | nat_nbma; | |
2fb975da TT |
195 | struct prefix prefix, route_prefix; |
196 | struct zbuf extpl; | |
197 | char bufp[PREFIX_STRLEN], buf[3][SU_ADDRSTRLEN]; | |
198 | int holding_time = pp->if_ad->holdtime; | |
199 | ||
200 | nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); | |
201 | THREAD_OFF(s->t_timer); | |
ffa2c898 | 202 | thread_add_timer(master, nhrp_shortcut_do_purge, s, 1, &s->t_timer); |
2fb975da TT |
203 | |
204 | if (pp->hdr->type != NHRP_PACKET_RESOLUTION_REPLY) { | |
996c9314 LB |
205 | if (pp->hdr->type == NHRP_PACKET_ERROR_INDICATION |
206 | && pp->hdr->u.error.code | |
207 | == NHRP_ERROR_PROTOCOL_ADDRESS_UNREACHABLE) { | |
208 | debugf(NHRP_DEBUG_COMMON, | |
209 | "Shortcut: Resolution: Protocol address unreachable"); | |
210 | nhrp_shortcut_update_binding(s, NHRP_CACHE_NEGATIVE, | |
211 | NULL, holding_time); | |
2fb975da | 212 | } else { |
996c9314 LB |
213 | debugf(NHRP_DEBUG_COMMON, |
214 | "Shortcut: Resolution failed"); | |
2fb975da TT |
215 | } |
216 | return; | |
217 | } | |
218 | ||
219 | /* Parse extensions */ | |
220 | memset(&nat_nbma, 0, sizeof nat_nbma); | |
221 | while ((ext = nhrp_ext_pull(&pp->extensions, &extpl)) != NULL) { | |
222 | switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) { | |
223 | case NHRP_EXTENSION_NAT_ADDRESS: | |
224 | nhrp_cie_pull(&extpl, pp->hdr, &nat_nbma, &cie_proto); | |
225 | break; | |
226 | } | |
227 | } | |
228 | ||
229 | /* Minor sanity check */ | |
230 | prefix2sockunion(s->p, &cie_proto); | |
231 | if (!sockunion_same(&cie_proto, &pp->dst_proto)) { | |
996c9314 LB |
232 | debugf(NHRP_DEBUG_COMMON, |
233 | "Shortcut: Warning dst_proto altered from %s to %s", | |
234 | sockunion2str(&cie_proto, buf[0], sizeof buf[0]), | |
235 | sockunion2str(&pp->dst_proto, buf[1], sizeof buf[1])); | |
2fb975da TT |
236 | } |
237 | ||
238 | /* One or more CIEs should be given as reply, we support only one */ | |
239 | cie = nhrp_cie_pull(&pp->payload, pp->hdr, &cie_nbma, &cie_proto); | |
240 | if (!cie || cie->code != NHRP_CODE_SUCCESS) { | |
996c9314 LB |
241 | debugf(NHRP_DEBUG_COMMON, "Shortcut: CIE code %d", |
242 | cie ? cie->code : -1); | |
2fb975da TT |
243 | return; |
244 | } | |
245 | ||
996c9314 LB |
246 | proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto |
247 | : &pp->dst_proto; | |
2fb975da TT |
248 | if (cie->holding_time) |
249 | holding_time = htons(cie->holding_time); | |
250 | ||
251 | prefix = *s->p; | |
252 | prefix.prefixlen = cie->prefix_length; | |
253 | ||
254 | /* Sanity check prefix length */ | |
996c9314 LB |
255 | if (prefix.prefixlen >= 8 * prefix_blen(&prefix) |
256 | || prefix.prefixlen == 0) { | |
257 | prefix.prefixlen = 8 * prefix_blen(&prefix); | |
258 | } else if (nhrp_route_address(NULL, &pp->dst_proto, &route_prefix, NULL) | |
259 | == NHRP_ROUTE_NBMA_NEXTHOP) { | |
2fb975da TT |
260 | if (prefix.prefixlen < route_prefix.prefixlen) |
261 | prefix.prefixlen = route_prefix.prefixlen; | |
262 | } | |
263 | ||
996c9314 LB |
264 | debugf(NHRP_DEBUG_COMMON, |
265 | "Shortcut: %s is at proto %s cie-nbma %s nat-nbma %s cie-holdtime %d", | |
266 | prefix2str(&prefix, bufp, sizeof bufp), | |
267 | sockunion2str(proto, buf[0], sizeof buf[0]), | |
268 | sockunion2str(&cie_nbma, buf[1], sizeof buf[1]), | |
269 | sockunion2str(&nat_nbma, buf[2], sizeof buf[2]), | |
270 | htons(cie->holding_time)); | |
2fb975da TT |
271 | |
272 | /* Update cache entry for the protocol to nbma binding */ | |
273 | if (sockunion_family(&nat_nbma) != AF_UNSPEC) { | |
274 | nbma = &nat_nbma; | |
275 | nbma_natoa = &cie_nbma; | |
276 | } else { | |
277 | nbma = &cie_nbma; | |
278 | nbma_natoa = NULL; | |
279 | } | |
280 | if (sockunion_family(nbma)) { | |
281 | c = nhrp_cache_get(pp->ifp, proto, 1); | |
282 | if (c) { | |
996c9314 LB |
283 | nhrp_cache_update_binding(c, NHRP_CACHE_CACHED, |
284 | holding_time, | |
285 | nhrp_peer_get(pp->ifp, nbma), | |
286 | htons(cie->mtu), nbma_natoa); | |
2fb975da TT |
287 | } |
288 | } | |
289 | ||
290 | /* Update shortcut entry for subnet to protocol gw binding */ | |
291 | if (c && !sockunion_same(proto, &pp->dst_proto)) { | |
292 | ps = nhrp_shortcut_get(&prefix); | |
293 | if (ps) { | |
294 | ps->addr = s->addr; | |
996c9314 LB |
295 | nhrp_shortcut_update_binding(ps, NHRP_CACHE_CACHED, c, |
296 | holding_time); | |
2fb975da TT |
297 | } |
298 | } | |
299 | ||
300 | debugf(NHRP_DEBUG_COMMON, "Shortcut: Resolution reply handled"); | |
301 | } | |
302 | ||
303 | static void nhrp_shortcut_send_resolution_req(struct nhrp_shortcut *s) | |
304 | { | |
305 | struct zbuf *zb; | |
306 | struct nhrp_packet_header *hdr; | |
307 | struct interface *ifp; | |
308 | struct nhrp_interface *nifp; | |
47d40757 | 309 | struct nhrp_afi_data *if_ad; |
2fb975da | 310 | struct nhrp_peer *peer; |
47d40757 | 311 | struct nhrp_cie_header *cie; |
2fb975da | 312 | |
996c9314 LB |
313 | if (nhrp_route_address(NULL, &s->addr, NULL, &peer) |
314 | != NHRP_ROUTE_NBMA_NEXTHOP) | |
2fb975da TT |
315 | return; |
316 | ||
317 | if (s->type == NHRP_CACHE_INVALID || s->type == NHRP_CACHE_NEGATIVE) | |
318 | s->type = NHRP_CACHE_INCOMPLETE; | |
319 | ||
320 | ifp = peer->ifp; | |
321 | nifp = ifp->info; | |
322 | ||
323 | /* Create request */ | |
324 | zb = zbuf_alloc(1500); | |
996c9314 LB |
325 | hdr = nhrp_packet_push( |
326 | zb, NHRP_PACKET_RESOLUTION_REQUEST, &nifp->nbma, | |
327 | &nifp->afi[family2afi(sockunion_family(&s->addr))].addr, | |
328 | &s->addr); | |
329 | hdr->u.request_id = | |
330 | htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &s->reqid, | |
331 | nhrp_shortcut_recv_resolution_rep)); | |
332 | hdr->flags = htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER | |
333 | | NHRP_FLAG_RESOLUTION_AUTHORATIVE | |
334 | | NHRP_FLAG_RESOLUTION_SOURCE_STABLE); | |
2fb975da TT |
335 | |
336 | /* RFC2332 - One or zero CIEs, if CIE is present contains: | |
337 | * - Prefix length: widest acceptable prefix we accept (if U set, 0xff) | |
338 | * - MTU: MTU of the source station | |
339 | * - Holding Time: Max time to cache the source information | |
340 | * */ | |
47d40757 GN |
341 | /* FIXME: push CIE for each local protocol address */ |
342 | cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, NULL, NULL); | |
343 | cie->prefix_length = 0xff; | |
344 | if_ad = &nifp->afi[family2afi(sockunion_family(&s->addr))]; | |
345 | cie->holding_time = htons(if_ad->holdtime); | |
346 | cie->mtu = htons(if_ad->mtu); | |
347 | debugf(NHRP_DEBUG_COMMON, | |
348 | "Shortcut res_req: set cie holdtime to %u " | |
349 | "and mtu to %u. shortcut's holdtime is %u.", | |
350 | ntohs(cie->holding_time), ntohs(cie->mtu), s->holding_time); | |
2fb975da TT |
351 | |
352 | nhrp_ext_request(zb, hdr, ifp); | |
353 | ||
354 | /* Cisco NAT detection extension */ | |
355 | hdr->flags |= htons(NHRP_FLAG_RESOLUTION_NAT); | |
356 | nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS); | |
357 | ||
358 | nhrp_packet_complete(zb, hdr); | |
359 | ||
360 | nhrp_peer_send(peer, zb); | |
361 | nhrp_peer_unref(peer); | |
362 | zbuf_free(zb); | |
363 | } | |
364 | ||
365 | void nhrp_shortcut_initiate(union sockunion *addr) | |
366 | { | |
367 | struct prefix p; | |
368 | struct nhrp_shortcut *s; | |
369 | ||
370 | sockunion2hostprefix(addr, &p); | |
371 | s = nhrp_shortcut_get(&p); | |
372 | if (s && s->type != NHRP_CACHE_INCOMPLETE) { | |
373 | s->addr = *addr; | |
374 | THREAD_OFF(s->t_timer); | |
ffa2c898 QY |
375 | thread_add_timer(master, nhrp_shortcut_do_purge, s, 30, |
376 | &s->t_timer); | |
2fb975da TT |
377 | nhrp_shortcut_send_resolution_req(s); |
378 | } | |
379 | } | |
380 | ||
381 | void nhrp_shortcut_init(void) | |
382 | { | |
383 | shortcut_rib[AFI_IP] = route_table_init(); | |
384 | shortcut_rib[AFI_IP6] = route_table_init(); | |
385 | } | |
386 | ||
387 | void nhrp_shortcut_terminate(void) | |
388 | { | |
389 | route_table_finish(shortcut_rib[AFI_IP]); | |
390 | route_table_finish(shortcut_rib[AFI_IP6]); | |
391 | } | |
392 | ||
996c9314 LB |
393 | void nhrp_shortcut_foreach(afi_t afi, |
394 | void (*cb)(struct nhrp_shortcut *, void *), | |
395 | void *ctx) | |
2fb975da TT |
396 | { |
397 | struct route_table *rt = shortcut_rib[afi]; | |
398 | struct route_node *rn; | |
399 | route_table_iter_t iter; | |
400 | ||
996c9314 LB |
401 | if (!rt) |
402 | return; | |
2fb975da TT |
403 | |
404 | route_table_iter_init(&iter, rt); | |
405 | while ((rn = route_table_iter_next(&iter)) != NULL) { | |
996c9314 LB |
406 | if (rn->info) |
407 | cb(rn->info, ctx); | |
2fb975da TT |
408 | } |
409 | route_table_iter_cleanup(&iter); | |
410 | } | |
411 | ||
412 | struct purge_ctx { | |
413 | const struct prefix *p; | |
414 | int deleted; | |
415 | }; | |
416 | ||
417 | void nhrp_shortcut_purge(struct nhrp_shortcut *s, int force) | |
418 | { | |
419 | THREAD_OFF(s->t_timer); | |
420 | nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); | |
421 | ||
422 | if (force) { | |
423 | /* Immediate purge on route with draw or pending shortcut */ | |
ffa2c898 QY |
424 | thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 5, |
425 | &s->t_timer); | |
2fb975da TT |
426 | } else { |
427 | /* Soft expire - force immediate renewal, but purge | |
428 | * in few seconds to make sure stale route is not | |
429 | * used too long. In practice most purges are caused | |
430 | * by hub bgp change, but target usually stays same. | |
431 | * This allows to keep nhrp route up, and to not | |
432 | * cause temporary rerouting via hubs causing latency | |
433 | * jitter. */ | |
ffa2c898 QY |
434 | thread_add_timer_msec(master, nhrp_shortcut_do_purge, s, 3000, |
435 | &s->t_timer); | |
2fb975da TT |
436 | s->expiring = 1; |
437 | nhrp_shortcut_check_use(s); | |
438 | } | |
439 | } | |
440 | ||
441 | static void nhrp_shortcut_purge_prefix(struct nhrp_shortcut *s, void *ctx) | |
442 | { | |
443 | struct purge_ctx *pctx = ctx; | |
444 | ||
445 | if (prefix_match(pctx->p, s->p)) | |
446 | nhrp_shortcut_purge(s, pctx->deleted || !s->cache); | |
447 | } | |
448 | ||
449 | void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted) | |
450 | { | |
451 | struct purge_ctx pctx = { | |
996c9314 | 452 | .p = p, .deleted = deleted, |
2fb975da | 453 | }; |
996c9314 LB |
454 | nhrp_shortcut_foreach(family2afi(PREFIX_FAMILY(p)), |
455 | nhrp_shortcut_purge_prefix, &pctx); | |
2fb975da | 456 | } |