summaryrefslogtreecommitdiffstats
path: root/src/iftable.c
blob: b6a8e725813fadb9dec1faae2fa0ac1c1c712fb0 (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
/* iftable - table of network interfaces
 *
 * (C) 2004 by Astaro AG, written by Harald Welte <hwelte@astaro.com>
 * (C) 2008 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This software is Free Software and licensed under GNU GPLv2. 
 */

/* IFINDEX handling */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>

#include <linux/netdevice.h>

#include <libnfnetlink/libnfnetlink.h>
#include "rtnl.h"
#include "linux_list.h"

struct ifindex_node {
	struct list_head head;

	u_int32_t	index;
	u_int32_t	type;
	u_int32_t	alen;
	u_int32_t	flags;
	char		addr[8];
	char		name[16];
};

struct nlif_handle {
	struct list_head ifindex_hash[16];
	struct rtnl_handle *rtnl_handle;
	struct rtnl_handler ifadd_handler;
	struct rtnl_handler ifdel_handler;
};

/* iftable_add - Add/Update an entry to/in the interface table
 * @n:		netlink message header of a RTM_NEWLINK message
 * @arg:	not used
 *
 * This function adds/updates an entry in the intrface table.
 * Returns -1 on error, 1 on success.
 */
static int iftable_add(struct nlmsghdr *n, void *arg)
{
	unsigned int hash, found = 0;
	struct ifinfomsg *ifi_msg = NLMSG_DATA(n);
	struct ifindex_node *this;
	struct rtattr *cb[IFLA_MAX+1];
	struct nlif_handle *h = (struct nlif_handle *)arg;

	if (n->nlmsg_type != RTM_NEWLINK)
		return -1;

	if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi_msg)))
		return -1;

	rtnl_parse_rtattr(cb, IFLA_MAX, IFLA_RTA(ifi_msg), IFLA_PAYLOAD(n));

	if (!cb[IFLA_IFNAME])
		return -1;

	hash = ifi_msg->ifi_index & 0xF;
	list_for_each_entry(this, &h->ifindex_hash[hash], head) {
		if (this->index == ifi_msg->ifi_index) {
			found = 1;
			break;
		}
	}

	if (!found) {
		this = malloc(sizeof(*this));
		if (!this)
			return -1;

		this->index = ifi_msg->ifi_index;
	}

	this->type = ifi_msg->ifi_type;
	this->flags = ifi_msg->ifi_flags;
	if (cb[IFLA_ADDRESS]) {
		unsigned int alen;
		this->alen = alen = RTA_PAYLOAD(cb[IFLA_ADDRESS]);
		if (alen > sizeof(this->addr))
			alen = sizeof(this->addr);
		memcpy(this->addr, RTA_DATA(cb[IFLA_ADDRESS]), alen);
	} else {
		this->alen = 0;
		memset(this->addr, 0, sizeof(this->addr));
	}
	strcpy(this->name, RTA_DATA(cb[IFLA_IFNAME]));

	list_add(&this->head, &h->ifindex_hash[hash]);

	return 1;
}

/* iftable_del - Delete an entry from the interface table
 * @n:		netlink message header of a RTM_DELLINK nlmsg
 * @arg:	not used
 *
 * Delete an entry from the interface table.  
 * Returns -1 on error, 0 if no matching entry was found or 1 on success.
 */
static int iftable_del(struct nlmsghdr *n, void *arg)
{
	struct ifinfomsg *ifi_msg = NLMSG_DATA(n);
	struct rtattr *cb[IFLA_MAX+1];
	struct nlif_handle *h = (struct nlif_handle *)arg;
	struct ifindex_node *this, *tmp;
	unsigned int hash;

	if (n->nlmsg_type != RTM_DELLINK)
		return -1;

	if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi_msg)))
		return -1;

	rtnl_parse_rtattr(cb, IFLA_MAX, IFLA_RTA(ifi_msg), IFLA_PAYLOAD(n));

	hash = ifi_msg->ifi_index & 0xF;
	list_for_each_entry_safe(this, tmp, &h->ifindex_hash[hash], head) {
		if (this->index == ifi_msg->ifi_index) {
			list_del(&this->head);
			free(this);
			return 1;
		}
	}

	return 0;
}

/** Get the name for an ifindex
 *
 * \param nlif_handle A pointer to a ::nlif_handle created
 * \param index ifindex to be resolved
 * \param name interface name, pass a buffer of IFNAMSIZ size
 * \return -1 on error, 1 on success 
 */
int nlif_index2name(struct nlif_handle *h, 
		    unsigned int index,
		    char *name)
{
	unsigned int hash;
	struct ifindex_node *this;

	assert(h != NULL);
	assert(name != NULL);

	if (index == 0) {
		strcpy(name, "*");
		return 1;
	}

	hash = index & 0xF;
	list_for_each_entry(this, &h->ifindex_hash[hash], head) {
		if (this->index == index) {
			strcpy(name, this->name);
			return 1;
		}
	}

	errno = ENOENT;
	return -1;
}

static int iftable_up(struct nlif_handle *h, unsigned int index)
{
	unsigned int hash;
	struct ifindex_node *this;

	hash = index & 0xF;
	list_for_each_entry(this, &h->ifindex_hash[hash], head) {
		if (this->index == index) {
			if (this->flags & IFF_UP)
				return 1;
			else
				return 0;
		}
	}
	return -1;
}

/** Initialize interface table
 *
 * Initialize rtnl interface and interface table
 * Call this before any nlif_* function
 *
 * \return file descriptor to netlink socket
 */
struct nlif_handle *nlif_open(void)
{
	int i;
	struct nlif_handle *h;

	h = calloc(1,  sizeof(struct nlif_handle));
	if (h == NULL)
		goto err;

	for (i=0; i<16; i++)
		INIT_LIST_HEAD(&h->ifindex_hash[i]);

	h->ifadd_handler.nlmsg_type = RTM_NEWLINK;
	h->ifadd_handler.handlefn = iftable_add;
	h->ifadd_handler.arg = h;
	h->ifdel_handler.nlmsg_type = RTM_DELLINK;
	h->ifdel_handler.handlefn = iftable_del;
	h->ifdel_handler.arg = h;

	h->rtnl_handle = rtnl_open();
	if (h->rtnl_handle == NULL)
		goto err;

	if (rtnl_handler_register(h->rtnl_handle, &h->ifadd_handler) < 0)
		goto err_close;

	if (rtnl_handler_register(h->rtnl_handle, &h->ifdel_handler) < 0)
		goto err_unregister;

	return h;

err_unregister:
	rtnl_handler_unregister(h->rtnl_handle, &h->ifdel_handler);
err_close:
	rtnl_close(h->rtnl_handle);
	free(h);
err:
	return NULL;
}

/** Destructor of interface table
 *
 * \param nlif_handle A pointer to a ::nlif_handle created 
 * via nlif_open()
 */
void nlif_close(struct nlif_handle *h)
{
	int i;
	struct ifindex_node *this, *tmp;

	assert(h != NULL);

	rtnl_handler_unregister(h->rtnl_handle, &h->ifadd_handler);
	rtnl_handler_unregister(h->rtnl_handle, &h->ifdel_handler);
	rtnl_close(h->rtnl_handle);

	for (i=0; i<16; i++) {
		list_for_each_entry_safe(this, tmp, &h->ifindex_hash[i], head) {
			list_del(&this->head);
			free(this);
		}
	}

	free(h);
	h = NULL; /* bugtrap */
}

/** Receive message from netlink and update interface table
 *
 * \param nlif_handle A pointer to a ::nlif_handle created
 * \return 0 if OK
 */
int nlif_catch(struct nlif_handle *h)
{
	assert(h != NULL);

	if (h->rtnl_handle)
		return rtnl_receive(h->rtnl_handle);

	return -1;
}

/** 
 * nlif_query - request a dump of interfaces available in the system
 * @h: pointer to a valid nlif_handler
 */
int nlif_query(struct nlif_handle *h)
{
	assert(h != NULL);

	if (rtnl_dump_type(h->rtnl_handle, RTM_GETLINK) < 0)
		return -1;

	return nlif_catch(h);
}

/** Returns socket descriptor for the netlink socket
 *
 * \param nlif_handle A pointer to a ::nlif_handle created
 * \return The fd or -1 if there's an error
 */
int nlif_fd(struct nlif_handle *h)
{
	assert(h != NULL);

	if (h->rtnl_handle)
		return h->rtnl_handle->rtnl_fd;

	return -1;
}