2 * Copyright (c) 2014-2015 Timo Teräs
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.
18 unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];
20 const char * const nhrp_cache_type_str[] = {
21 [NHRP_CACHE_INVALID] = "invalid",
22 [NHRP_CACHE_INCOMPLETE] = "incomplete",
23 [NHRP_CACHE_NEGATIVE] = "negative",
24 [NHRP_CACHE_CACHED] = "cached",
25 [NHRP_CACHE_DYNAMIC] = "dynamic",
26 [NHRP_CACHE_NHS] = "nhs",
27 [NHRP_CACHE_STATIC] = "static",
28 [NHRP_CACHE_LOCAL] = "local",
31 static unsigned int nhrp_cache_protocol_key(void *peer_data)
33 struct nhrp_cache *p = peer_data;
34 return sockunion_hash(&p->remote_addr);
37 static int nhrp_cache_protocol_cmp(const void *cache_data, const void *key_data)
39 const struct nhrp_cache *a = cache_data;
40 const struct nhrp_cache *b = key_data;
41 return sockunion_same(&a->remote_addr, &b->remote_addr);
44 static void *nhrp_cache_alloc(void *data)
46 struct nhrp_cache *p, *key = data;
48 p = XMALLOC(MTYPE_NHRP_CACHE, sizeof(struct nhrp_cache));
50 *p = (struct nhrp_cache) {
51 .cur.type = NHRP_CACHE_INVALID,
52 .new.type = NHRP_CACHE_INVALID,
53 .remote_addr = key->remote_addr,
55 .notifier_list = NOTIFIER_LIST_INITIALIZER(&p->notifier_list),
57 nhrp_cache_counts[p->cur.type]++;
63 static void nhrp_cache_free(struct nhrp_cache *c)
65 struct nhrp_interface *nifp = c->ifp->info;
67 zassert(c->cur.type == NHRP_CACHE_INVALID && c->cur.peer == NULL);
68 zassert(c->new.type == NHRP_CACHE_INVALID && c->new.peer == NULL);
69 nhrp_cache_counts[c->cur.type]--;
70 notifier_call(&c->notifier_list, NOTIFY_CACHE_DELETE);
71 zassert(!notifier_active(&c->notifier_list));
72 hash_release(nifp->cache_hash, c);
73 XFREE(MTYPE_NHRP_CACHE, c);
76 struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote_addr, int create)
78 struct nhrp_interface *nifp = ifp->info;
79 struct nhrp_cache key;
81 if (!nifp->cache_hash) {
82 nifp->cache_hash = hash_create(nhrp_cache_protocol_key, nhrp_cache_protocol_cmp);
83 if (!nifp->cache_hash)
87 key.remote_addr = *remote_addr;
90 return hash_get(nifp->cache_hash, &key, create ? nhrp_cache_alloc : NULL);
93 static int nhrp_cache_do_free(struct thread *t)
95 struct nhrp_cache *c = THREAD_ARG(t);
101 static int nhrp_cache_do_timeout(struct thread *t)
103 struct nhrp_cache *c = THREAD_ARG(t);
105 if (c->cur.type != NHRP_CACHE_INVALID)
106 nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
110 static void nhrp_cache_update_route(struct nhrp_cache *c)
113 struct nhrp_peer *p = c->cur.peer;
115 sockunion2hostprefix(&c->remote_addr, &pfx);
117 if (p && nhrp_peer_check(p, 1)) {
118 netlink_update_binding(p->ifp, &c->remote_addr, &p->vc->remote.nbma);
119 nhrp_route_announce(1, c->cur.type, &pfx, c->ifp, NULL, c->cur.mtu);
120 if (c->cur.type >= NHRP_CACHE_DYNAMIC) {
121 nhrp_route_update_nhrp(&pfx, c->ifp);
122 c->nhrp_route_installed = 1;
123 } else if (c->nhrp_route_installed) {
124 nhrp_route_update_nhrp(&pfx, NULL);
125 c->nhrp_route_installed = 0;
127 if (!c->route_installed) {
128 notifier_call(&c->notifier_list, NOTIFY_CACHE_UP);
129 c->route_installed = 1;
132 if (c->nhrp_route_installed) {
133 nhrp_route_update_nhrp(&pfx, NULL);
134 c->nhrp_route_installed = 0;
136 if (c->route_installed) {
137 sockunion2hostprefix(&c->remote_addr, &pfx);
138 notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
139 nhrp_route_announce(0, c->cur.type, &pfx, NULL, NULL, 0);
140 c->route_installed = 0;
145 static void nhrp_cache_peer_notifier(struct notifier_block *n, unsigned long cmd)
147 struct nhrp_cache *c = container_of(n, struct nhrp_cache, peer_notifier);
151 nhrp_cache_update_route(c);
153 case NOTIFY_PEER_DOWN:
154 case NOTIFY_PEER_IFCONFIG_CHANGED:
155 notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
156 nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
158 case NOTIFY_PEER_NBMA_CHANGING:
159 if (c->cur.type == NHRP_CACHE_DYNAMIC)
160 c->cur.peer->vc->abort_migration = 1;
165 static void nhrp_cache_reset_new(struct nhrp_cache *c)
167 THREAD_OFF(c->t_auth);
168 if (list_hashed(&c->newpeer_notifier.notifier_entry))
169 nhrp_peer_notify_del(c->new.peer, &c->newpeer_notifier);
170 nhrp_peer_unref(c->new.peer);
171 memset(&c->new, 0, sizeof(c->new));
172 c->new.type = NHRP_CACHE_INVALID;
175 static void nhrp_cache_update_timers(struct nhrp_cache *c)
177 THREAD_OFF(c->t_timeout);
179 switch (c->cur.type) {
180 case NHRP_CACHE_INVALID:
182 THREAD_TIMER_MSEC_ON(master, c->t_timeout, nhrp_cache_do_free, c, 10);
186 THREAD_TIMER_ON(master, c->t_timeout, nhrp_cache_do_timeout, c, c->cur.expires - recent_relative_time().tv_sec);
191 static void nhrp_cache_authorize_binding(struct nhrp_reqid *r, void *arg)
193 struct nhrp_cache *c = container_of(r, struct nhrp_cache, eventid);
194 char buf[SU_ADDRSTRLEN];
196 debugf(NHRP_DEBUG_COMMON, "cache: %s %s: %s",
197 c->ifp->name, sockunion2str(&c->remote_addr, buf, sizeof buf),
200 nhrp_reqid_free(&nhrp_event_reqid, r);
202 if (arg && strcmp(arg, "accept") == 0) {
204 netlink_update_binding(c->cur.peer->ifp, &c->remote_addr, NULL);
205 nhrp_peer_notify_del(c->cur.peer, &c->peer_notifier);
206 nhrp_peer_unref(c->cur.peer);
208 nhrp_cache_counts[c->cur.type]--;
209 nhrp_cache_counts[c->new.type]++;
211 c->cur.peer = nhrp_peer_ref(c->cur.peer);
212 nhrp_cache_reset_new(c);
214 nhrp_peer_notify_add(c->cur.peer, &c->peer_notifier, nhrp_cache_peer_notifier);
215 nhrp_cache_update_route(c);
216 notifier_call(&c->notifier_list, NOTIFY_CACHE_BINDING_CHANGE);
218 nhrp_cache_reset_new(c);
221 nhrp_cache_update_timers(c);
224 static int nhrp_cache_do_auth_timeout(struct thread *t)
226 struct nhrp_cache *c = THREAD_ARG(t);
228 nhrp_cache_authorize_binding(&c->eventid, (void *) "timeout");
232 static void nhrp_cache_newpeer_notifier(struct notifier_block *n, unsigned long cmd)
234 struct nhrp_cache *c = container_of(n, struct nhrp_cache, newpeer_notifier);
238 if (nhrp_peer_check(c->new.peer, 1)) {
239 evmgr_notify("authorize-binding", c, nhrp_cache_authorize_binding);
240 THREAD_TIMER_ON(master, c->t_auth, nhrp_cache_do_auth_timeout, c, 10);
243 case NOTIFY_PEER_DOWN:
244 case NOTIFY_PEER_IFCONFIG_CHANGED:
245 nhrp_cache_reset_new(c);
250 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)
252 if (c->cur.type > type || c->new.type > type) {
258 switch (sockunion_family(&c->remote_addr)) {
260 if (mtu < 576 || mtu >= 1500)
262 /* Opennhrp announces nbma mtu, but we use protocol mtu.
263 * This heuristic tries to fix up it. */
264 if (mtu > 1420) mtu = (mtu & -16) - 80;
271 nhrp_cache_reset_new(c);
272 if (c->cur.type == type && c->cur.peer == p && c->cur.mtu == mtu) {
273 if (holding_time > 0) c->cur.expires = recent_relative_time().tv_sec + holding_time;
274 if (nbma_oa) c->cur.remote_nbma_natoa = *nbma_oa;
275 else memset(&c->cur.remote_nbma_natoa, 0, sizeof c->cur.remote_nbma_natoa);
281 if (nbma_oa) c->new.remote_nbma_natoa = *nbma_oa;
283 if (holding_time > 0)
284 c->new.expires = recent_relative_time().tv_sec + holding_time;
285 else if (holding_time < 0)
286 c->new.type = NHRP_CACHE_INVALID;
288 if (c->new.type == NHRP_CACHE_INVALID ||
289 c->new.type >= NHRP_CACHE_STATIC ||
291 nhrp_cache_authorize_binding(&c->eventid, (void *) "accept");
293 nhrp_peer_notify_add(c->new.peer, &c->newpeer_notifier, nhrp_cache_newpeer_notifier);
294 nhrp_cache_newpeer_notifier(&c->newpeer_notifier, NOTIFY_PEER_UP);
295 THREAD_TIMER_ON(master, c->t_auth, nhrp_cache_do_auth_timeout, c, 60);
298 nhrp_cache_update_timers(c);
303 void nhrp_cache_set_used(struct nhrp_cache *c, int used)
307 notifier_call(&c->notifier_list, NOTIFY_CACHE_USED);
310 struct nhrp_cache_iterator_ctx {
311 void (*cb)(struct nhrp_cache *, void *);
315 static void nhrp_cache_iterator(struct hash_backet *b, void *ctx)
317 struct nhrp_cache_iterator_ctx *ic = ctx;
318 ic->cb(b->data, ic->ctx);
321 void nhrp_cache_foreach(struct interface *ifp, void (*cb)(struct nhrp_cache *, void *), void *ctx)
323 struct nhrp_interface *nifp = ifp->info;
324 struct nhrp_cache_iterator_ctx ic = {
329 if (nifp->cache_hash)
330 hash_iterate(nifp->cache_hash, nhrp_cache_iterator, &ic);
333 void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n, notifier_fn_t fn)
335 notifier_add(n, &c->notifier_list, fn);
338 void nhrp_cache_notify_del(struct nhrp_cache *c, struct notifier_block *n)