summaryrefslogtreecommitdiffstats
path: root/nhrpd/nhrp_cache.c
blob: ffc8b5a9bf80a7295e5415377dbf173d73e94d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* NHRP cache
 * Copyright (c) 2014-2015 Timo Teräs
 *
 * This file is free software: you may copy, redistribute and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 */

#include "zebra.h"
#include "memory.h"
#include "thread.h"
#include "hash.h"
#include "nhrpd.h"

#include "netlink.h"

DEFINE_MTYPE_STATIC(NHRPD, NHRP_CACHE, "NHRP cache entry")

unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];

const char *const nhrp_cache_type_str[] = {
		[NHRP_CACHE_INVALID] = "invalid",
		[NHRP_CACHE_INCOMPLETE] = "incomplete",
		[NHRP_CACHE_NEGATIVE] = "negative",
		[NHRP_CACHE_CACHED] = "cached",
		[NHRP_CACHE_DYNAMIC] = "dynamic",
		[NHRP_CACHE_NHS] = "nhs",
		[NHRP_CACHE_STATIC] = "static",
		[NHRP_CACHE_LOCAL] = "local",
};

static unsigned int nhrp_cache_protocol_key(void *peer_data)
{
	struct nhrp_cache *p = peer_data;
	return sockunion_hash(&p->remote_addr);
}

static int nhrp_cache_protocol_cmp(const void *cache_data, const void *key_data)
{
	const struct nhrp_cache *a = cache_data;
	const struct nhrp_cache *b = key_data;
	return sockunion_same(&a->remote_addr, &b->remote_addr);
}

static void *nhrp_cache_alloc(void *data)
{
	struct nhrp_cache *p, *key = data;

	p = XMALLOC(MTYPE_NHRP_CACHE, sizeof(struct nhrp_cache));
	if (p) {
		*p = (struct nhrp_cache){
			.cur.type = NHRP_CACHE_INVALID,
			.new.type = NHRP_CACHE_INVALID,
			.remote_addr = key->remote_addr,
			.ifp = key->ifp,
			.notifier_list =
				NOTIFIER_LIST_INITIALIZER(&p->notifier_list),
		};
		nhrp_cache_counts[p->cur.type]++;
	}

	return p;
}

static void nhrp_cache_free(struct nhrp_cache *c)
{
	struct nhrp_interface *nifp = c->ifp->info;

	zassert(c->cur.type == NHRP_CACHE_INVALID && c->cur.peer == NULL);
	zassert(c->new.type == NHRP_CACHE_INVALID && c->new.peer == NULL);
	nhrp_cache_counts[c->cur.type]--;
	notifier_call(&c->notifier_list, NOTIFY_CACHE_DELETE);
	zassert(!notifier_active(&c->notifier_list));
	hash_release(nifp->cache_hash, c);
	XFREE(MTYPE_NHRP_CACHE, c);
}

struct nhrp_cache *nhrp_cache_get(struct interface *ifp,
				  union sockunion *remote_addr, int create)
{
	struct nhrp_interface *nifp = ifp->info;
	struct nhrp_cache key;

	if (!nifp->cache_hash) {
		nifp->cache_hash =
			hash_create(nhrp_cache_protocol_key,
				    nhrp_cache_protocol_cmp, "NHRP Cache");
		if (!nifp->cache_hash)
			return NULL;
	}

	key.remote_addr = *remote_addr;
	key.ifp = ifp;

	return hash_get(nifp->cache_hash, &key,
			create ? nhrp_cache_alloc : NULL);
}

static int nhrp_cache_do_free(struct thread *t)
{
	struct nhrp_cache *c = THREAD_ARG(t);
	c->t_timeout = NULL;
	nhrp_cache_free(c);
	return 0;
}

static int nhrp_cache_do_timeout(struct thread *t)
{
	struct nhrp_cache *c = THREAD_ARG(t);
	c->t_timeout = NULL;
	if (c->cur.type != NHRP_CACHE_INVALID)
		nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
	return 0;
}

static void nhrp_cache_update_route(struct nhrp_cache *c)
{
	struct prefix pfx;
	struct nhrp_peer *p = c->cur.peer;

	sockunion2hostprefix(&c->remote_addr, &pfx);

	if (p && nhrp_peer_check(p, 1)) {
		netlink_update_binding(p->ifp, &c->remote_addr,
				       &p->vc->remote.nbma);
		nhrp_route_announce(1, c->cur.type, &pfx, c->ifp, NULL,
				    c->cur.mtu);
		if (c->cur.type >= NHRP_CACHE_DYNAMIC) {
			nhrp_route_update_nhrp(&pfx, c->ifp);
			c->nhrp_route_installed = 1;
		} else if (c->nhrp_route_installed) {
			nhrp_route_update_nhrp(&pfx, NULL);
			c->nhrp_route_installed = 0;
		}
		if (!c->route_installed) {
			notifier_call(&c->notifier_list, NOTIFY_CACHE_UP);
			c->route_installed = 1;
		}
	} else {
		if (c->nhrp_route_installed) {
			nhrp_route_update_nhrp(&pfx, NULL);
			c->nhrp_route_installed = 0;
		}
		if (c->route_installed) {
			sockunion2hostprefix(&c->remote_addr, &pfx);
			notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
			nhrp_route_announce(0, c->cur.type, &pfx, NULL, NULL,
					    0);
			c->route_installed = 0;
		}
	}
}

static void nhrp_cache_peer_notifier(struct notifier_block *n,
				     unsigned long cmd)
{
	struct nhrp_cache *c =
		container_of(n, struct nhrp_cache, peer_notifier);

	switch (cmd) {
	case NOTIFY_PEER_UP:
		nhrp_cache_update_route(c);
		break;
	case NOTIFY_PEER_DOWN:
	case NOTIFY_PEER_IFCONFIG_CHANGED:
		notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN);
		nhrp_cache_update_binding(c, c->cur.type, -1, NULL, 0, NULL);
		break;
	case NOTIFY_PEER_NBMA_CHANGING:
		if (c->cur.type == NHRP_CACHE_DYNAMIC)
			c->cur.peer->vc->abort_migration = 1;
		break;
	}
}

static void nhrp_cache_reset_new(struct nhrp_cache *c)
{
	THREAD_OFF(c->t_auth);
	if (list_hashed(&c->newpeer_notifier.notifier_entry))
		nhrp_peer_notify_del(c->new.peer, &c->newpeer_notifier);
	nhrp_peer_unref(c->new.peer);
	memset(&c->new, 0, sizeof(c->new));
	c->new.type = NHRP_CACHE_INVALID;
}

static void nhrp_cache_update_timers(struct nhrp_cache *c)
{
	THREAD_OFF(c->t_timeout);

	switch (c->cur.type) {
	case NHRP_CACHE_INVALID:
		if (!c->t_auth)
			thread_add_timer_msec(master, nhrp_cache_do_free, c, 10,
					      &c->t_timeout);
		break;
	default:
		if (c->cur.expires)
			thread_add_timer(master, nhrp_cache_do_timeout, c,
					 c->cur.expires - monotime(NULL),
					 &c->t_timeout);
		break;
	}
}

static void nhrp_cache_authorize_binding(struct nhrp_reqid *r, void *arg)
{
	struct nhrp_cache *c = container_of(r, struct nhrp_cache, eventid);
	char buf[SU_ADDRSTRLEN];

	debugf(NHRP_DEBUG_COMMON, "cache: %s %s: %s", c->ifp->name,
	       sockunion2str(&c->remote_addr, buf, sizeof buf),
	       (const char *)arg);

	nhrp_reqid_free(&nhrp_event_reqid, r);

	if (arg && strcmp(arg, "accept") == 0) {
		if (c->cur.peer) {
			netlink_update_binding(c->cur.peer->ifp,
					       &c->remote_addr, NULL);
			nhrp_peer_notify_del(c->cur.peer, &c->peer_notifier);
			nhrp_peer_unref(c->cur.peer);
		}
		nhrp_cache_counts[c->cur.type]--;
		nhrp_cache_counts[c->new.type]++;
		c->cur = c->new;
		c->cur.peer = nhrp_peer_ref(c->cur.peer);
		nhrp_cache_reset_new(c);
		if (c->cur.peer)
			nhrp_peer_notify_add(c->cur.peer, &c->peer_notifier,
					     nhrp_cache_peer_notifier);
		nhrp_cache_update_route(c);
		notifier_call(&c->notifier_list, NOTIFY_CACHE_BINDING_CHANGE);
	} else {
		nhrp_cache_reset_new(c);
	}

	nhrp_cache_update_timers(c);
}

static int nhrp_cache_do_auth_timeout(struct thread *t)
{
	struct nhrp_cache *c = THREAD_ARG(t);
	c->t_auth = NULL;
	nhrp_cache_authorize_binding(&c->eventid, (void *)"timeout");
	return 0;
}

static void nhrp_cache_newpeer_notifier(struct notifier_block *n,
					unsigned long cmd)
{
	struct nhrp_cache *c =
		container_of(n, struct nhrp_cache, newpeer_notifier);

	switch (cmd) {
	case NOTIFY_PEER_UP:
		if (nhrp_peer_check(c->new.peer, 1)) {
			evmgr_notify("authorize-binding", c,
				     nhrp_cache_authorize_binding);
			thread_add_timer(master, nhrp_cache_do_auth_timeout, c,
					 10, &c->t_auth);
		}
		break;
	case NOTIFY_PEER_DOWN:
	case NOTIFY_PEER_IFCONFIG_CHANGED:
		nhrp_cache_reset_new(c);
		break;
	}
}

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)
{
	if (c->cur.type > type || c->new.type > type) {
		nhrp_peer_unref(p);
		return 0;
	}

	/* Sanitize MTU */
	switch (sockunion_family(&c->remote_addr)) {
	case AF_INET:
		if (mtu < 576 || mtu >= 1500)
			mtu = 0;
		/* Opennhrp announces nbma mtu, but we use protocol mtu.
		 * This heuristic tries to fix up it. */
		if (mtu > 1420)
			mtu = (mtu & -16) - 80;
		break;
	default:
		mtu = 0;
		break;
	}

	nhrp_cache_reset_new(c);
	if (c->cur.type == type && c->cur.peer == p && c->cur.mtu == mtu) {
		if (holding_time > 0)
			c->cur.expires = monotime(NULL) + holding_time;
		if (nbma_oa)
			c->cur.remote_nbma_natoa = *nbma_oa;
		else
			memset(&c->cur.remote_nbma_natoa, 0,
			       sizeof c->cur.remote_nbma_natoa);
		nhrp_peer_unref(p);
	} else {
		c->new.type = type;
		c->new.peer = p;
		c->new.mtu = mtu;
		if (nbma_oa)
			c->new.remote_nbma_natoa = *nbma_oa;

		if (holding_time > 0)
			c->new.expires = monotime(NULL) + holding_time;
		else if (holding_time < 0)
			nhrp_cache_reset_new(c);

		if (c->new.type == NHRP_CACHE_INVALID
		    || c->new.type >= NHRP_CACHE_STATIC || c->map) {
			nhrp_cache_authorize_binding(&c->eventid,
						     (void *)"accept");
		} else {
			nhrp_peer_notify_add(c->new.peer, &c->newpeer_notifier,
					     nhrp_cache_newpeer_notifier);
			nhrp_cache_newpeer_notifier(&c->newpeer_notifier,
						    NOTIFY_PEER_UP);
			thread_add_timer(master, nhrp_cache_do_auth_timeout, c,
					 60, &c->t_auth);
		}
	}
	nhrp_cache_update_timers(c);

	return 1;
}

void nhrp_cache_set_used(struct nhrp_cache *c, int used)
{
	c->used = used;
	if (c->used)
		notifier_call(&c->notifier_list, NOTIFY_CACHE_USED);
}

struct nhrp_cache_iterator_ctx {
	void (*cb)(struct nhrp_cache *, void *);
	void *ctx;
};

static void nhrp_cache_iterator(struct hash_backet *b, void *ctx)
{
	struct nhrp_cache_iterator_ctx *ic = ctx;
	ic->cb(b->data, ic->ctx);
}

void nhrp_cache_foreach(struct interface *ifp,
			void (*cb)(struct nhrp_cache *, void *), void *ctx)
{
	struct nhrp_interface *nifp = ifp->info;
	struct nhrp_cache_iterator_ctx ic = {
		.cb = cb, .ctx = ctx,
	};

	if (nifp->cache_hash)
		hash_iterate(nifp->cache_hash, nhrp_cache_iterator, &ic);
}

void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n,
			   notifier_fn_t fn)
{
	notifier_add(n, &c->notifier_list, fn);
}

void nhrp_cache_notify_del(struct nhrp_cache *c, struct notifier_block *n)
{
	notifier_del(n);
}