summaryrefslogtreecommitdiffstats
path: root/nhrpd/nhrp_interface.c
blob: f6d89b141a2038857b555e783921c777a8ff57d4 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/* NHRP interface
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <net/if_arp.h>
#include "zebra.h"
#include "linklist.h"
#include "memory.h"
#include "thread.h"

#include "nhrpd.h"
#include "os.h"
#include "netlink.h"

DEFINE_MTYPE_STATIC(NHRPD, NHRP_IF, "NHRP interface");

static void nhrp_interface_update_cache_config(struct interface *ifp,
					       bool available,
					       uint8_t family);

static int nhrp_if_new_hook(struct interface *ifp)
{
	struct nhrp_interface *nifp;
	afi_t afi;

	nifp = XCALLOC(MTYPE_NHRP_IF, sizeof(struct nhrp_interface));

	ifp->info = nifp;
	nifp->ifp = ifp;

	notifier_init(&nifp->notifier_list);
	for (afi = 0; afi < AFI_MAX; afi++) {
		struct nhrp_afi_data *ad = &nifp->afi[afi];
		ad->holdtime = NHRPD_DEFAULT_HOLDTIME;
		list_init(&ad->nhslist_head);
	}

	return 0;
}

static int nhrp_if_delete_hook(struct interface *ifp)
{
	struct nhrp_interface *nifp = ifp->info;

	debugf(NHRP_DEBUG_IF, "Deleted interface (%s)", ifp->name);

	nhrp_cache_interface_del(ifp);
	nhrp_nhs_interface_del(ifp);
	nhrp_peer_interface_del(ifp);

	if (nifp->ipsec_profile)
		free(nifp->ipsec_profile);
	if (nifp->ipsec_fallback_profile)
		free(nifp->ipsec_fallback_profile);
	if (nifp->source)
		free(nifp->source);

	XFREE(MTYPE_NHRP_IF, ifp->info);
	return 0;
}

void nhrp_interface_init(void)
{
	hook_register_prio(if_add, 0, nhrp_if_new_hook);
	hook_register_prio(if_del, 0, nhrp_if_delete_hook);
}

void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi)
{
	struct nhrp_interface *nifp = ifp->info;
	struct nhrp_afi_data *if_ad = &nifp->afi[afi];
	unsigned short new_mtu;

	if (if_ad->configured_mtu < 0)
		new_mtu = nifp->nbmaifp ? nifp->nbmaifp->mtu : 0;
	else
		new_mtu = if_ad->configured_mtu;
	if (new_mtu >= 1500)
		new_mtu = 0;

	if (new_mtu != if_ad->mtu) {
		debugf(NHRP_DEBUG_IF, "%s: MTU changed to %d", ifp->name,
		       new_mtu);
		if_ad->mtu = new_mtu;
		notifier_call(&nifp->notifier_list,
			      NOTIFY_INTERFACE_MTU_CHANGED);
	}
}

static void nhrp_interface_update_source(struct interface *ifp)
{
	struct nhrp_interface *nifp = ifp->info;

	if (!nifp->source || !nifp->nbmaifp
	    || (ifindex_t)nifp->linkidx == nifp->nbmaifp->ifindex)
		return;

	nifp->linkidx = nifp->nbmaifp->ifindex;
	debugf(NHRP_DEBUG_IF, "%s: bound device index changed to %d", ifp->name,
	       nifp->linkidx);
	netlink_gre_set_link(ifp->ifindex, nifp->linkidx);
}

static void nhrp_interface_interface_notifier(struct notifier_block *n,
					      unsigned long cmd)
{
	struct nhrp_interface *nifp =
		container_of(n, struct nhrp_interface, nbmanifp_notifier);
	struct interface *nbmaifp = nifp->nbmaifp;
	struct nhrp_interface *nbmanifp = nbmaifp->info;

	switch (cmd) {
	case NOTIFY_INTERFACE_CHANGED:
		nhrp_interface_update_mtu(nifp->ifp, AFI_IP);
		nhrp_interface_update_source(nifp->ifp);
		break;
	case NOTIFY_INTERFACE_ADDRESS_CHANGED:
		nifp->nbma = nbmanifp->afi[AFI_IP].addr;
		nhrp_interface_update(nifp->ifp);
		notifier_call(&nifp->notifier_list,
			      NOTIFY_INTERFACE_NBMA_CHANGED);
		debugf(NHRP_DEBUG_IF, "%s: NBMA change: address %pSU",
		       nifp->ifp->name, &nifp->nbma);
		break;
	}
}

static void nhrp_interface_update_nbma(struct interface *ifp)
{
	struct nhrp_interface *nifp = ifp->info, *nbmanifp = NULL;
	struct interface *nbmaifp = NULL;
	union sockunion nbma;

	sockunion_family(&nbma) = AF_UNSPEC;

	if (nifp->source)
		nbmaifp = if_lookup_by_name(nifp->source, VRF_DEFAULT);

	switch (ifp->ll_type) {
	case ZEBRA_LLT_IPGRE: {
		struct in_addr saddr = {0};
		netlink_gre_get_info(ifp->ifindex, &nifp->grekey,
				     &nifp->linkidx, &saddr);
		debugf(NHRP_DEBUG_IF, "%s: GRE: %x %x %x", ifp->name,
		       nifp->grekey, nifp->linkidx, saddr.s_addr);
		if (saddr.s_addr != INADDR_ANY)
			sockunion_set(&nbma, AF_INET, (uint8_t *)&saddr.s_addr,
				      sizeof(saddr.s_addr));
		else if (!nbmaifp && nifp->linkidx != IFINDEX_INTERNAL)
			nbmaifp =
				if_lookup_by_index(nifp->linkidx, VRF_DEFAULT);
	} break;
	default:
		break;
	}

	if (nbmaifp)
		nbmanifp = nbmaifp->info;

	if (nbmaifp != nifp->nbmaifp) {
		if (nifp->nbmaifp)
			notifier_del(&nifp->nbmanifp_notifier);
		nifp->nbmaifp = nbmaifp;
		if (nbmaifp) {
			notifier_add(&nifp->nbmanifp_notifier,
				     &nbmanifp->notifier_list,
				     nhrp_interface_interface_notifier);
			debugf(NHRP_DEBUG_IF, "%s: bound to %s", ifp->name,
			       nbmaifp->name);
		}
	}

	if (nbmaifp) {
		if (sockunion_family(&nbma) == AF_UNSPEC)
			nbma = nbmanifp->afi[AFI_IP].addr;
		nhrp_interface_update_mtu(ifp, AFI_IP);
		nhrp_interface_update_source(ifp);
	}

	if (!sockunion_same(&nbma, &nifp->nbma)) {
		nifp->nbma = nbma;
		nhrp_interface_update(nifp->ifp);
		debugf(NHRP_DEBUG_IF, "%s: NBMA address changed", ifp->name);
		notifier_call(&nifp->notifier_list,
			      NOTIFY_INTERFACE_NBMA_CHANGED);
	}

	nhrp_interface_update(ifp);
}

static void nhrp_interface_update_address(struct interface *ifp, afi_t afi,
					  int force)
{
	const int family = afi2family(afi);
	struct nhrp_interface *nifp = ifp->info;
	struct nhrp_afi_data *if_ad = &nifp->afi[afi];
	struct nhrp_cache *nc;
	struct connected *c, *best;
	struct listnode *cnode;
	union sockunion addr;
	char buf[PREFIX_STRLEN];

	/* Select new best match preferring primary address */
	best = NULL;
	for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
		if (PREFIX_FAMILY(c->address) != family)
			continue;
		if (best == NULL) {
			best = c;
			continue;
		}
		if ((best->flags & ZEBRA_IFA_SECONDARY)
		    && !(c->flags & ZEBRA_IFA_SECONDARY)) {
			best = c;
			continue;
		}
		if (!(best->flags & ZEBRA_IFA_SECONDARY)
		    && (c->flags & ZEBRA_IFA_SECONDARY))
			continue;
		if (best->address->prefixlen > c->address->prefixlen) {
			best = c;
			continue;
		}
		if (best->address->prefixlen < c->address->prefixlen)
			continue;
	}

	/* On NHRP interfaces a host prefix is required */
	if (best && if_ad->configured
	    && best->address->prefixlen != 8 * prefix_blen(best->address)) {
		zlog_notice("%s: %pFX is not a host prefix", ifp->name,
			    best->address);
		best = NULL;
	}

	/* Update address if it changed */
	if (best)
		prefix2sockunion(best->address, &addr);
	else
		memset(&addr, 0, sizeof(addr));

	if (!force && sockunion_same(&if_ad->addr, &addr))
		return;

	if (sockunion_family(&if_ad->addr) != AF_UNSPEC) {
		nc = nhrp_cache_get(ifp, &if_ad->addr, 0);
		if (nc)
			nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1,
						  NULL, 0, NULL, NULL);
	}

	debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", ifp->name,
	       afi == AFI_IP ? 4 : 6,
	       best ? prefix2str(best->address, buf, sizeof(buf)) : "(none)");
	if_ad->addr = addr;

	if (if_ad->configured && sockunion_family(&if_ad->addr) != AF_UNSPEC) {
		nc = nhrp_cache_get(ifp, &addr, 1);
		if (nc)
			nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL,
						  0, NULL, NULL);
	}

	notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED);
}

void nhrp_interface_update(struct interface *ifp)
{
	struct nhrp_interface *nifp = ifp->info;
	struct nhrp_afi_data *if_ad;
	afi_t afi;
	int enabled = 0;

	notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_CHANGED);

	for (afi = 0; afi < AFI_MAX; afi++) {
		if_ad = &nifp->afi[afi];

		if (sockunion_family(&nifp->nbma) == AF_UNSPEC
		    || ifp->ifindex == IFINDEX_INTERNAL || !if_is_up(ifp)
		    || !if_ad->network_id) {
			if (if_ad->configured) {
				if_ad->configured = 0;
				nhrp_interface_update_address(ifp, afi, 1);
			}
			continue;
		}

		if (!if_ad->configured) {
			os_configure_dmvpn(ifp->ifindex, ifp->name,
					   afi2family(afi));
			nhrp_send_zebra_configure_arp(ifp, afi2family(afi));
			if_ad->configured = 1;
			nhrp_interface_update_address(ifp, afi, 1);
		}

		enabled = 1;
	}

	if (enabled != nifp->enabled) {
		nifp->enabled = enabled;
		notifier_call(&nifp->notifier_list,
			      enabled ? NOTIFY_INTERFACE_UP
				      : NOTIFY_INTERFACE_DOWN);
	}
}

int nhrp_ifp_create(struct interface *ifp)
{
	debugf(NHRP_DEBUG_IF, "if-add: %s, ifindex: %u, hw_type: %d %s",
	       ifp->name, ifp->ifindex, ifp->ll_type,
	       if_link_type_str(ifp->ll_type));

	nhrp_interface_update_nbma(ifp);

	return 0;
}

int nhrp_ifp_destroy(struct interface *ifp)
{
	debugf(NHRP_DEBUG_IF, "if-delete: %s", ifp->name);

	nhrp_interface_update_cache_config(ifp, false, AF_INET);
	nhrp_interface_update_cache_config(ifp, false, AF_INET6);
	nhrp_interface_update(ifp);

	return 0;
}

struct map_ctx {
	int family;
	bool enabled;
};

static void interface_config_update_nhrp_map(struct nhrp_cache_config *cc,
					     void *data)
{
	struct map_ctx *ctx = data;
	struct interface *ifp = cc->ifp;
	struct nhrp_cache *c;
	union sockunion nbma_addr;

	if (sockunion_family(&cc->remote_addr) != ctx->family)
		return;

	/* gre layer not ready */
	if (ifp->vrf_id == VRF_UNKNOWN)
		return;

	c = nhrp_cache_get(ifp, &cc->remote_addr, ctx->enabled ? 1 : 0);
	if (!c && !ctx->enabled)
		return;

	/* suppress */
	if (!ctx->enabled) {
		if (c && c->map) {
			nhrp_cache_update_binding(
				c, c->cur.type, -1,
				nhrp_peer_get(ifp, &nbma_addr), 0, NULL, NULL);
		}
		return;
	}

	/* Newly created */
	assert(c != NULL);

	c->map = 1;
	if (cc->type == NHRP_CACHE_LOCAL)
		nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
					  NULL, NULL);
	else {
		nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
					  nhrp_peer_get(ifp, &cc->nbma), 0,
					  NULL, NULL);
	}
}

static void nhrp_interface_update_cache_config(struct interface *ifp, bool available, uint8_t family)
{
	struct map_ctx mapctx;

	mapctx = (struct map_ctx){
		.family = family,
		.enabled = available
	};
	nhrp_cache_config_foreach(ifp, interface_config_update_nhrp_map,
				  &mapctx);

}

int nhrp_ifp_up(struct interface *ifp)
{
	debugf(NHRP_DEBUG_IF, "if-up: %s", ifp->name);
	nhrp_interface_update_nbma(ifp);

	return 0;
}

int nhrp_ifp_down(struct interface *ifp)
{
	debugf(NHRP_DEBUG_IF, "if-down: %s", ifp->name);
	nhrp_interface_update(ifp);

	return 0;
}

int nhrp_interface_address_add(ZAPI_CALLBACK_ARGS)
{
	struct connected *ifc;

	ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
	if (ifc == NULL)
		return 0;

	debugf(NHRP_DEBUG_IF, "if-addr-add: %s: %pFX", ifc->ifp->name,
	       ifc->address);

	nhrp_interface_update_address(
		ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
	nhrp_interface_update_cache_config(ifc->ifp, true, PREFIX_FAMILY(ifc->address));
	return 0;
}

int nhrp_interface_address_delete(ZAPI_CALLBACK_ARGS)
{
	struct connected *ifc;

	ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
	if (ifc == NULL)
		return 0;

	debugf(NHRP_DEBUG_IF, "if-addr-del: %s: %pFX", ifc->ifp->name,
	       ifc->address);

	nhrp_interface_update_address(
		ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
	connected_free(&ifc);

	return 0;
}

void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n,
			       notifier_fn_t fn)
{
	struct nhrp_interface *nifp = ifp->info;
	notifier_add(n, &nifp->notifier_list, fn);
}

void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n)
{
	notifier_del(n);
}

void nhrp_interface_set_protection(struct interface *ifp, const char *profile,
				   const char *fallback_profile)
{
	struct nhrp_interface *nifp = ifp->info;

	if (nifp->ipsec_profile) {
		vici_terminate_vc_by_profile_name(nifp->ipsec_profile);
		nhrp_vc_reset();
		free(nifp->ipsec_profile);
	}
	nifp->ipsec_profile = profile ? strdup(profile) : NULL;

	if (nifp->ipsec_fallback_profile) {
		vici_terminate_vc_by_profile_name(nifp->ipsec_fallback_profile);
		nhrp_vc_reset();
		free(nifp->ipsec_fallback_profile);
	}
	nifp->ipsec_fallback_profile =
		fallback_profile ? strdup(fallback_profile) : NULL;

	notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_IPSEC_CHANGED);
}

void nhrp_interface_set_source(struct interface *ifp, const char *ifname)
{
	struct nhrp_interface *nifp = ifp->info;

	if (nifp->source)
		free(nifp->source);
	nifp->source = ifname ? strdup(ifname) : NULL;

	nhrp_interface_update_nbma(ifp);
}