]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_cache.c
tools: enable stylechecker to handle new files
[mirror_frr.git] / nhrpd / nhrp_cache.c
1 /* NHRP cache
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 #include "zebra.h"
11 #include "memory.h"
12 #include "thread.h"
13 #include "hash.h"
14 #include "nhrpd.h"
15
16 #include "netlink.h"
17
18 DEFINE_MTYPE_STATIC(NHRPD, NHRP_CACHE, "NHRP cache entry")
19
20 unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];
21
22 const char * const nhrp_cache_type_str[] = {
23 [NHRP_CACHE_INVALID] = "invalid",
24 [NHRP_CACHE_INCOMPLETE] = "incomplete",
25 [NHRP_CACHE_NEGATIVE] = "negative",
26 [NHRP_CACHE_CACHED] = "cached",
27 [NHRP_CACHE_DYNAMIC] = "dynamic",
28 [NHRP_CACHE_NHS] = "nhs",
29 [NHRP_CACHE_STATIC] = "static",
30 [NHRP_CACHE_LOCAL] = "local",
31 };
32
33 static unsigned int nhrp_cache_protocol_key(void *peer_data)
34 {
35 struct nhrp_cache *p = peer_data;
36 return sockunion_hash(&p->remote_addr);
37 }
38
39 static int nhrp_cache_protocol_cmp(const void *cache_data, const void *key_data)
40 {
41 const struct nhrp_cache *a = cache_data;
42 const struct nhrp_cache *b = key_data;
43 return sockunion_same(&a->remote_addr, &b->remote_addr);
44 }
45
46 static void *nhrp_cache_alloc(void *data)
47 {
48 struct nhrp_cache *p, *key = data;
49
50 p = XMALLOC(MTYPE_NHRP_CACHE, sizeof(struct nhrp_cache));
51 if (p) {
52 *p = (struct nhrp_cache) {
53 .cur.type = NHRP_CACHE_INVALID,
54 .new.type = NHRP_CACHE_INVALID,
55 .remote_addr = key->remote_addr,
56 .ifp = key->ifp,
57 .notifier_list = NOTIFIER_LIST_INITIALIZER(&p->notifier_list),
58 };
59 nhrp_cache_counts[p->cur.type]++;
60 }
61
62 return p;
63 }
64
65 static void nhrp_cache_free(struct nhrp_cache *c)
66 {
67 struct nhrp_interface *nifp = c->ifp->info;
68
69 zassert(c->cur.type == NHRP_CACHE_INVALID && c->cur.peer == NULL);
70 zassert(c->new.type == NHRP_CACHE_INVALID && c->new.peer == NULL);
71 nhrp_cache_counts[c->cur.type]--;
72 notifier_call(&c->notifier_list, NOTIFY_CACHE_DELETE);
73 zassert(!notifier_active(&c->notifier_list));
74 hash_release(nifp->cache_hash, c);
75 XFREE(MTYPE_NHRP_CACHE, c);
76 }
77
78 struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote_addr, int create)
79 {
80 struct nhrp_interface *nifp = ifp->info;
81 struct nhrp_cache key;
82
83 if (!nifp->cache_hash) {
84 nifp->cache_hash = hash_create(nhrp_cache_protocol_key,
85 nhrp_cache_protocol_cmp,
86 "NHRP Cache");
87 if (!nifp->cache_hash)
88 return NULL;
89 }
90
91 key.remote_addr = *remote_addr;
92 key.ifp = ifp;
93
94 return hash_get(nifp->cache_hash, &key, create ? nhrp_cache_alloc : NULL);
95 }
96
97 static int nhrp_cache_do_free(struct thread *t)
98 {
99 struct nhrp_cache *c = THREAD_ARG(t);
100 c->t_timeout = NULL;
101 nhrp_cache_free(c);
102 return 0;
103 }
104
105 static int nhrp_cache_do_timeout(struct thread *t)
106 {
107 struct nhrp_cache *c = THREAD_ARG(t);
108 c->t_timeout = NULL;
109 if (c->cur.type != NHRP_CACHE_INVALID)
110 nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
111 return 0;
112 }
113
114 static void nhrp_cache_update_route(struct nhrp_cache *c)
115 {
116 struct prefix pfx;
117 struct nhrp_peer *p = c->cur.peer;
118
119 sockunion2hostprefix(&c->remote_addr, &pfx);
120
121 if (p && nhrp_peer_check(p, 1)) {
122 netlink_update_binding(p->ifp, &c->remote_addr, &p->vc->remote.nbma);
123 nhrp_route_announce(1, c->cur.type, &pfx, c->ifp, NULL, c->cur.mtu);
124 if (c->cur.type >= NHRP_CACHE_DYNAMIC) {
125 nhrp_route_update_nhrp(&pfx, c->ifp);
126 c->nhrp_route_installed = 1;
127 } else if (c->nhrp_route_installed) {
128 nhrp_route_update_nhrp(&pfx, NULL);
129 c->nhrp_route_installed = 0;
130 }
131 if (!c->route_installed) {
132 notifier_call(&c->notifier_list, NOTIFY_CACHE_UP);
133 c->route_installed = 1;
134 }
135 } else {
136 if (c->nhrp_route_installed) {
137 nhrp_route_update_nhrp(&pfx, NULL);
138 c->nhrp_route_installed = 0;
139 }
140 if (c->route_installed) {
141 sockunion2hostprefix(&c->remote_addr, &pfx);
142 notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
143 nhrp_route_announce(0, c->cur.type, &pfx, NULL, NULL, 0);
144 c->route_installed = 0;
145 }
146 }
147 }
148
149 static void nhrp_cache_peer_notifier(struct notifier_block *n, unsigned long cmd)
150 {
151 struct nhrp_cache *c = container_of(n, struct nhrp_cache, peer_notifier);
152
153 switch (cmd) {
154 case NOTIFY_PEER_UP:
155 nhrp_cache_update_route(c);
156 break;
157 case NOTIFY_PEER_DOWN:
158 case NOTIFY_PEER_IFCONFIG_CHANGED:
159 notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
160 nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
161 break;
162 case NOTIFY_PEER_NBMA_CHANGING:
163 if (c->cur.type == NHRP_CACHE_DYNAMIC)
164 c->cur.peer->vc->abort_migration = 1;
165 break;
166 }
167 }
168
169 static void nhrp_cache_reset_new(struct nhrp_cache *c)
170 {
171 THREAD_OFF(c->t_auth);
172 if (list_hashed(&c->newpeer_notifier.notifier_entry))
173 nhrp_peer_notify_del(c->new.peer, &c->newpeer_notifier);
174 nhrp_peer_unref(c->new.peer);
175 memset(&c->new, 0, sizeof(c->new));
176 c->new.type = NHRP_CACHE_INVALID;
177 }
178
179 static void nhrp_cache_update_timers(struct nhrp_cache *c)
180 {
181 THREAD_OFF(c->t_timeout);
182
183 switch (c->cur.type) {
184 case NHRP_CACHE_INVALID:
185 if (!c->t_auth)
186 thread_add_timer_msec(master, nhrp_cache_do_free, c,
187 10, &c->t_timeout);
188 break;
189 default:
190 if (c->cur.expires)
191 thread_add_timer(master, nhrp_cache_do_timeout, c,
192 c->cur.expires - monotime(NULL),
193 &c->t_timeout);
194 break;
195 }
196 }
197
198 static void nhrp_cache_authorize_binding(struct nhrp_reqid *r, void *arg)
199 {
200 struct nhrp_cache *c = container_of(r, struct nhrp_cache, eventid);
201 char buf[SU_ADDRSTRLEN];
202
203 debugf(NHRP_DEBUG_COMMON, "cache: %s %s: %s",
204 c->ifp->name, sockunion2str(&c->remote_addr, buf, sizeof buf),
205 (const char *) arg);
206
207 nhrp_reqid_free(&nhrp_event_reqid, r);
208
209 if (arg && strcmp(arg, "accept") == 0) {
210 if (c->cur.peer) {
211 netlink_update_binding(c->cur.peer->ifp, &c->remote_addr, NULL);
212 nhrp_peer_notify_del(c->cur.peer, &c->peer_notifier);
213 nhrp_peer_unref(c->cur.peer);
214 }
215 nhrp_cache_counts[c->cur.type]--;
216 nhrp_cache_counts[c->new.type]++;
217 c->cur = c->new;
218 c->cur.peer = nhrp_peer_ref(c->cur.peer);
219 nhrp_cache_reset_new(c);
220 if (c->cur.peer)
221 nhrp_peer_notify_add(c->cur.peer, &c->peer_notifier, nhrp_cache_peer_notifier);
222 nhrp_cache_update_route(c);
223 notifier_call(&c->notifier_list, NOTIFY_CACHE_BINDING_CHANGE);
224 } else {
225 nhrp_cache_reset_new(c);
226 }
227
228 nhrp_cache_update_timers(c);
229 }
230
231 static int nhrp_cache_do_auth_timeout(struct thread *t)
232 {
233 struct nhrp_cache *c = THREAD_ARG(t);
234 c->t_auth = NULL;
235 nhrp_cache_authorize_binding(&c->eventid, (void *) "timeout");
236 return 0;
237 }
238
239 static void nhrp_cache_newpeer_notifier(struct notifier_block *n, unsigned long cmd)
240 {
241 struct nhrp_cache *c = container_of(n, struct nhrp_cache, newpeer_notifier);
242
243 switch (cmd) {
244 case NOTIFY_PEER_UP:
245 if (nhrp_peer_check(c->new.peer, 1)) {
246 evmgr_notify("authorize-binding", c, nhrp_cache_authorize_binding);
247 thread_add_timer(master, nhrp_cache_do_auth_timeout,
248 c, 10, &c->t_auth);
249 }
250 break;
251 case NOTIFY_PEER_DOWN:
252 case NOTIFY_PEER_IFCONFIG_CHANGED:
253 nhrp_cache_reset_new(c);
254 break;
255 }
256 }
257
258 int nhrp_cache_update_binding(struct nhrp_cache *c, enum nhrp_cache_type type, int holding_time, struct nhrp_peer *p, uint32_t mtu, union sockunion *nbma_oa)
259 {
260 if (c->cur.type > type || c->new.type > type) {
261 nhrp_peer_unref(p);
262 return 0;
263 }
264
265 /* Sanitize MTU */
266 switch (sockunion_family(&c->remote_addr)) {
267 case AF_INET:
268 if (mtu < 576 || mtu >= 1500)
269 mtu = 0;
270 /* Opennhrp announces nbma mtu, but we use protocol mtu.
271 * This heuristic tries to fix up it. */
272 if (mtu > 1420) mtu = (mtu & -16) - 80;
273 break;
274 default:
275 mtu = 0;
276 break;
277 }
278
279 nhrp_cache_reset_new(c);
280 if (c->cur.type == type && c->cur.peer == p && c->cur.mtu == mtu) {
281 if (holding_time > 0) c->cur.expires = monotime(NULL) + holding_time;
282 if (nbma_oa) c->cur.remote_nbma_natoa = *nbma_oa;
283 else memset(&c->cur.remote_nbma_natoa, 0, sizeof c->cur.remote_nbma_natoa);
284 nhrp_peer_unref(p);
285 } else {
286 c->new.type = type;
287 c->new.peer = p;
288 c->new.mtu = mtu;
289 if (nbma_oa) c->new.remote_nbma_natoa = *nbma_oa;
290
291 if (holding_time > 0)
292 c->new.expires = monotime(NULL) + holding_time;
293 else if (holding_time < 0)
294 nhrp_cache_reset_new(c);
295
296 if (c->new.type == NHRP_CACHE_INVALID ||
297 c->new.type >= NHRP_CACHE_STATIC ||
298 c->map) {
299 nhrp_cache_authorize_binding(&c->eventid, (void *) "accept");
300 } else {
301 nhrp_peer_notify_add(c->new.peer, &c->newpeer_notifier, nhrp_cache_newpeer_notifier);
302 nhrp_cache_newpeer_notifier(&c->newpeer_notifier, NOTIFY_PEER_UP);
303 thread_add_timer(master, nhrp_cache_do_auth_timeout,
304 c, 60, &c->t_auth);
305 }
306 }
307 nhrp_cache_update_timers(c);
308
309 return 1;
310 }
311
312 void nhrp_cache_set_used(struct nhrp_cache *c, int used)
313 {
314 c->used = used;
315 if (c->used)
316 notifier_call(&c->notifier_list, NOTIFY_CACHE_USED);
317 }
318
319 struct nhrp_cache_iterator_ctx {
320 void (*cb)(struct nhrp_cache *, void *);
321 void *ctx;
322 };
323
324 static void nhrp_cache_iterator(struct hash_backet *b, void *ctx)
325 {
326 struct nhrp_cache_iterator_ctx *ic = ctx;
327 ic->cb(b->data, ic->ctx);
328 }
329
330 void nhrp_cache_foreach(struct interface *ifp, void (*cb)(struct nhrp_cache *, void *), void *ctx)
331 {
332 struct nhrp_interface *nifp = ifp->info;
333 struct nhrp_cache_iterator_ctx ic = {
334 .cb = cb,
335 .ctx = ctx,
336 };
337
338 if (nifp->cache_hash)
339 hash_iterate(nifp->cache_hash, nhrp_cache_iterator, &ic);
340 }
341
342 void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n, notifier_fn_t fn)
343 {
344 notifier_add(n, &c->notifier_list, fn);
345 }
346
347 void nhrp_cache_notify_del(struct nhrp_cache *c, struct notifier_block *n)
348 {
349 notifier_del(n);
350 }