aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins/kernel_iph/kernel_iph_net.c
blob: 56c458fdb3b127e6b0221110842d6d4eec3a9803 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
 * Copyright (C) 2013 Martin Willi
 * Copyright (C) 2013 revosec AG
 *
 * This program is free software; you can redistribute it 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/* Windows 7, for some iphlpapi.h functionality */
#define _WIN32_WINNT 0x0601
#include <winsock2.h>
#include <ws2ipdef.h>
#include <windows.h>
#include <ntddndis.h>
#include <naptypes.h>
#include <iphlpapi.h>

#include "kernel_iph_net.h"

#include <hydra.h>
#include <threading/mutex.h>
#include <collections/linked_list.h>


typedef struct private_kernel_iph_net_t private_kernel_iph_net_t;

/**
 * Private data of kernel_iph_net implementation.
 */
struct private_kernel_iph_net_t {

	/**
	 * Public interface.
	 */
	kernel_iph_net_t public;

	/**
	 * NotifyIpInterfaceChange() handle
	 */
	HANDLE changes;

	/**
	 * Mutex to access interface list
	 */
	mutex_t *mutex;

	/**
	 * Known interfaces, as iface_t
	 */
	linked_list_t *ifaces;
};

/**
 * Interface entry
 */
typedef struct  {
	/** interface index */
	DWORD ifindex;
	/** interface name */
	char *ifname;
	/** interface description */
	char *ifdesc;
	/** type of interface */
	DWORD iftype;
	/** interface status */
	IF_OPER_STATUS status;
	/** list of known addresses, as host_t */
	linked_list_t *addrs;
} iface_t;

/**
 * Clean up an iface_t
 */
static void iface_destroy(iface_t *this)
{
	this->addrs->destroy_offset(this->addrs, offsetof(host_t, destroy));
	free(this->ifname);
	free(this->ifdesc);
	free(this);
}

/**
 * Enum names for Windows IF_OPER_STATUS
 */
ENUM(if_oper_names, IfOperStatusUp, IfOperStatusLowerLayerDown,
	"Up",
	"Down",
	"Testing",
	"Unknown",
	"Dormant",
	"NotPresent",
	"LowerLayerDown",
);

/**
 * Update addresses for an iface entry
 */
static void update_addrs(private_kernel_iph_net_t *this, iface_t *entry,
						 IP_ADAPTER_ADDRESSES *addr, bool log)
{
	IP_ADAPTER_UNICAST_ADDRESS *current;
	enumerator_t *enumerator;
	linked_list_t *list;
	host_t *host, *old;

	list = entry->addrs;
	entry->addrs = linked_list_create();

	for (current = addr->FirstUnicastAddress; current; current = current->Next)
	{
		if (current->Address.lpSockaddr->sa_family == AF_INET6)
		{
			struct sockaddr_in6 *sin;

			sin = (struct sockaddr_in6*)current->Address.lpSockaddr;
			if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr))
			{
				continue;
			}
		}

		host = host_create_from_sockaddr(current->Address.lpSockaddr);
		if (host)
		{
			bool found = FALSE;

			enumerator = list->create_enumerator(list);
			while (enumerator->enumerate(enumerator, &old))
			{
				if (host->ip_equals(host, old))
				{
					list->remove_at(list, enumerator);
					old->destroy(old);
					found = TRUE;
				}
			}
			enumerator->destroy(enumerator);

			entry->addrs->insert_last(entry->addrs, host);

			if (!found && log)
			{
				DBG1(DBG_KNL, "%H appeared on interface %u '%s'",
					 host, entry->ifindex, entry->ifdesc);
			}
		}
	}

	while (list->remove_first(list, (void**)&old) == SUCCESS)
	{
		if (log)
		{
			DBG1(DBG_KNL, "%H disappeared from interface %u '%s'",
				 old, entry->ifindex, entry->ifdesc);
		}
		old->destroy(old);
	}
	list->destroy(list);
}

/**
 * Add an interface entry
 */
static void add_interface(private_kernel_iph_net_t *this,
						  IP_ADAPTER_ADDRESSES *addr, bool log)
{
	enumerator_t *enumerator;
	iface_t *entry;
	bool exists = FALSE;

	this->mutex->lock(this->mutex);
	enumerator = this->ifaces->create_enumerator(this->ifaces);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (entry->ifindex == addr->IfIndex)
		{
			exists = TRUE;
			break;
		}
	}
	enumerator->destroy(enumerator);
	this->mutex->unlock(this->mutex);

	if (!exists)
	{
		char desc[128] = "";

		wcstombs(desc, addr->Description, sizeof(desc));

		INIT(entry,
			.ifindex = addr->IfIndex,
			.ifname = strdup(addr->AdapterName),
			.ifdesc = strdup(desc),
			.iftype = addr->IfType,
			.status = addr->OperStatus,
			.addrs = linked_list_create(),
		);

		if (log)
		{
			DBG1(DBG_KNL, "interface %u '%s' appeared",
				 entry->ifindex, entry->ifdesc);
		}

		this->mutex->lock(this->mutex);
		update_addrs(this, entry, addr, log);
		this->ifaces->insert_last(this->ifaces, entry);
		this->mutex->unlock(this->mutex);
	}
}

/**
 * Remove an interface entry that is gone
 */
static void remove_interface(private_kernel_iph_net_t *this, NET_IFINDEX index)
{
	enumerator_t *enumerator;
	iface_t *entry;

	this->mutex->lock(this->mutex);
	enumerator = this->ifaces->create_enumerator(this->ifaces);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (entry->ifindex == index)
		{
			this->ifaces->remove_at(this->ifaces, enumerator);
			DBG1(DBG_KNL, "interface %u '%s' disappeared",
				 entry->ifindex, entry->ifdesc);
			iface_destroy(entry);
		}
	}
	enumerator->destroy(enumerator);
	this->mutex->unlock(this->mutex);
}

/**
 * Update an interface entry changed
 */
static void update_interface(private_kernel_iph_net_t *this,
							 IP_ADAPTER_ADDRESSES *addr)
{
	enumerator_t *enumerator;
	iface_t *entry;

	this->mutex->lock(this->mutex);
	enumerator = this->ifaces->create_enumerator(this->ifaces);
	while (enumerator->enumerate(enumerator, &entry))
	{
		if (entry->ifindex == addr->IfIndex)
		{
			if (entry->status != addr->OperStatus)
			{
				DBG1(DBG_KNL, "interface %u '%s' changed state from %N to %N",
					 entry->ifindex, entry->ifdesc, if_oper_names,
					 entry->status, if_oper_names, addr->OperStatus);
				entry->status = addr->OperStatus;
			}
			update_addrs(this, entry, addr, TRUE);
		}
	}
	enumerator->destroy(enumerator);
	this->mutex->unlock(this->mutex);
}

/**
 * MinGW gets MIB_IPINTERFACE_ROW wrong, as it packs InterfaceLuid just after
 * Family. Fix that with our own version of the struct header.
 */
typedef struct {
	ADDRESS_FAMILY Family;
	union {
		ULONG64 Value;
		struct {
			ULONG64 Reserved :24;
			ULONG64 NetLuidIndex :24;
			ULONG64 IfType :16;
		} Info;
	} InterfaceLuid;
	NET_IFINDEX InterfaceIndex;
	/* more would go here if needed */
} MIB_IPINTERFACE_ROW_FIXUP;

/**
 * NotifyIpInterfaceChange() callback
 */
static void change_interface(private_kernel_iph_net_t *this,
					MIB_IPINTERFACE_ROW_FIXUP *row, MIB_NOTIFICATION_TYPE type)
{
	IP_ADAPTER_ADDRESSES addrs[64], *current;
	ULONG res, size = sizeof(addrs);

	if (row && type == MibDeleteInstance)
	{
		remove_interface(this, row->InterfaceIndex);
	}
	else
	{
		res = GetAdaptersAddresses(AF_UNSPEC,
						GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
						GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
						NULL, addrs, &size);
		if (res == NO_ERROR)
		{
			current = addrs;
			while (current)
			{
				/* row is NULL only on MibInitialNotification */
				if (!row || row->InterfaceIndex == current->IfIndex)
				{
					switch (type)
					{
						case MibParameterNotification:
							update_interface(this, current);
							break;
						case MibInitialNotification:
							add_interface(this, current, FALSE);
							break;
						case MibAddInstance:
							add_interface(this, current, TRUE);
							break;
						default:
							break;
					}
				}
				current = current->Next;
			}
		}
		else
		{
			DBG1(DBG_KNL, "getting IPH adapter addresses failed: 0x%08lx", res);
		}
	}
}

/**
 * Get an iface entry for a local address, does no locking
 */
static iface_t* address2entry(private_kernel_iph_net_t *this, host_t *ip)
{
	enumerator_t *ifaces, *addrs;
	iface_t *entry, *found = NULL;
	host_t *host;

	ifaces = this->ifaces->create_enumerator(this->ifaces);
	while (!found && ifaces->enumerate(ifaces, &entry))
	{
		addrs = entry->addrs->create_enumerator(entry->addrs);
		while (!found && addrs->enumerate(addrs, &host))
		{
			if (host->ip_equals(host, ip))
			{
				found = entry;
			}
		}
		addrs->destroy(addrs);
	}
	ifaces->destroy(ifaces);

	return found;
}

METHOD(kernel_net_t, get_interface_name, bool,
	private_kernel_iph_net_t *this, host_t* ip, char **name)
{
	iface_t *entry;

	this->mutex->lock(this->mutex);
	entry = address2entry(this, ip);
	if (entry && name)
	{
		*name = strdup(entry->ifname);
	}
	this->mutex->unlock(this->mutex);

	return entry != NULL;
}

/**
 * Address enumerator
 */
typedef struct {
	/** implements enumerator_t */
	enumerator_t public;
	/** what kind of address should we enumerate? */
	kernel_address_type_t which;
	/** enumerator over interfaces */
	enumerator_t *ifaces;
	/** current enumerator over addresses, or NULL */
	enumerator_t *addrs;
	/** mutex to unlock on destruction */
	mutex_t *mutex;
} addr_enumerator_t;

METHOD(enumerator_t, addr_enumerate, bool,
	addr_enumerator_t *this, host_t **host)
{
	iface_t *entry;

	while (TRUE)
	{
		while (!this->addrs)
		{
			if (!this->ifaces->enumerate(this->ifaces, &entry))
			{
				return FALSE;
			}
			if (entry->iftype == IF_TYPE_SOFTWARE_LOOPBACK &&
				!(this->which & ADDR_TYPE_LOOPBACK))
			{
				continue;
			}
			if (entry->status != IfOperStatusUp &&
				!(this->which & ADDR_TYPE_DOWN))
			{
				continue;
			}
			this->addrs = entry->addrs->create_enumerator(entry->addrs);
		}
		if (this->addrs->enumerate(this->addrs, host))
		{
			return TRUE;
		}
		this->addrs->destroy(this->addrs);
		this->addrs = NULL;
	}
}

METHOD(enumerator_t, addr_destroy, void,
	addr_enumerator_t *this)
{
	DESTROY_IF(this->addrs);
	this->ifaces->destroy(this->ifaces);
	this->mutex->unlock(this->mutex);
	free(this);
}

METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
	private_kernel_iph_net_t *this, kernel_address_type_t which)
{
	addr_enumerator_t *enumerator;

	if (!(which & ADDR_TYPE_REGULAR))
	{
		/* we currently have no virtual, but regular IPs only */
		return enumerator_create_empty();
	}

	this->mutex->lock(this->mutex);

	INIT(enumerator,
		.public = {
			.enumerate = (void*)_addr_enumerate,
			.destroy = _addr_destroy,
		},
		.which = which,
		.ifaces = this->ifaces->create_enumerator(this->ifaces),
		.mutex = this->mutex,
	);
	return &enumerator->public;
}

METHOD(kernel_net_t, get_source_addr, host_t*,
	private_kernel_iph_net_t *this, host_t *dest, host_t *src)
{
	MIB_IPFORWARD_ROW2 route;
	SOCKADDR_INET best, *sai_dst, *sai_src = NULL;
	DWORD res, index = 0;

	res = GetBestInterfaceEx(dest->get_sockaddr(dest), &index);
	if (res != NO_ERROR)
	{
		DBG1(DBG_KNL, "getting interface to %H failed: 0x%08x", dest, res);
		return NULL;
	}

	sai_dst = (SOCKADDR_INET*)dest->get_sockaddr(dest);
	if (src)
	{
		sai_src = (SOCKADDR_INET*)src->get_sockaddr(src);
	}
	res = GetBestRoute2(0, index, sai_src, sai_dst, 0, &route, &best);
	if (res != NO_ERROR)
	{
		DBG2(DBG_KNL, "getting src address to %H failed: 0x%08x", dest, res);
		return NULL;
	}
	return host_create_from_sockaddr((struct sockaddr*)&best);
}

METHOD(kernel_net_t, get_nexthop, host_t*,
	private_kernel_iph_net_t *this, host_t *dest, host_t *src)
{
	return NULL;
}

METHOD(kernel_net_t, add_ip, status_t,
	private_kernel_iph_net_t *this, host_t *virtual_ip, int prefix,
	char *iface_name)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_net_t, del_ip, status_t,
	private_kernel_iph_net_t *this, host_t *virtual_ip, int prefix,
	bool wait)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_net_t, add_route, status_t,
	private_kernel_iph_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
	host_t *gateway, host_t *src_ip, char *if_name)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_net_t, del_route, status_t,
	private_kernel_iph_net_t *this, chunk_t dst_net, u_int8_t prefixlen,
	host_t *gateway, host_t *src_ip, char *if_name)
{
	return NOT_SUPPORTED;
}

METHOD(kernel_net_t, destroy, void,
	private_kernel_iph_net_t *this)
{
	if (this->changes)
	{
		CancelMibChangeNotify2(this->changes);
	}
	this->mutex->destroy(this->mutex);
	this->ifaces->destroy_function(this->ifaces, (void*)iface_destroy);
	free(this);
}

/*
 * Described in header.
 */
kernel_iph_net_t *kernel_iph_net_create()
{
	private_kernel_iph_net_t *this;
	ULONG res;

	INIT(this,
		.public = {
			.interface = {
				.get_interface = _get_interface_name,
				.create_address_enumerator = _create_address_enumerator,
				.get_source_addr = _get_source_addr,
				.get_nexthop = _get_nexthop,
				.add_ip = _add_ip,
				.del_ip = _del_ip,
				.add_route = _add_route,
				.del_route = _del_route,
				.destroy = _destroy,
			},
		},
		.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
		.ifaces = linked_list_create(),
	);

	res = NotifyIpInterfaceChange(AF_UNSPEC, (void*)change_interface,
								  this, TRUE, &this->changes);
	if (res != NO_ERROR)
	{
		DBG1(DBG_KNL, "registering for IPH interface changes failed: 0x%08lx",
			 res);
		destroy(this);
		return NULL;
	}

	return &this->public;
}