summaryrefslogtreecommitdiff
path: root/src/ipconfig.c
blob: 013a14f05db40a8cdc1a2fb0fd5efefacea3268f (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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <net/if.h>

#ifndef IFF_LOWER_UP
#define IFF_LOWER_UP	0x10000
#endif

#include <gdbus.h>

#include "connman.h"

struct connman_ipaddress {
	unsigned char prefixlen;
	char *address;
};

struct connman_ipconfig {
	gint refcount;
	int index;
	char *interface;
	unsigned short type;
	unsigned int flags;
	enum connman_ipconfig_method method;
	GSList *address_list;
	char *gateway;
};

static void free_address_list(struct connman_ipconfig *ipconfig)
{
	GSList *list;

	for (list = ipconfig->address_list; list; list = list->next) {
		struct connman_ipaddress *ipaddress = list->data;

		g_free(ipaddress->address);
		g_free(ipaddress);
		list->data = NULL;
	}

	g_slist_free(ipconfig->address_list);
	ipconfig->address_list = NULL;
}

static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
				unsigned char prefixlen, const char *address)
{
	GSList *list;

	for (list = ipconfig->address_list; list; list = list->next) {
		struct connman_ipaddress *ipaddress = list->data;

		if (g_strcmp0(ipaddress->address, address) == 0 &&
					ipaddress->prefixlen == prefixlen)
			return ipaddress;
	}

	return NULL;
}

/**
 * connman_ipconfig_create:
 *
 * Allocate a new ipconfig structure.
 *
 * Returns: a newly-allocated #connman_ipconfig structure
 */
struct connman_ipconfig *connman_ipconfig_create(int index)
{
	struct connman_ipconfig *ipconfig;

	DBG("");

	ipconfig = g_try_new0(struct connman_ipconfig, 1);
	if (ipconfig == NULL)
		return NULL;

	ipconfig->refcount = 1;

	ipconfig->index = index;
	ipconfig->interface = connman_inet_ifname(index);

	DBG("ipconfig %p", ipconfig);

	connman_info("%s {create} index %d", ipconfig->interface,
							ipconfig->index);

	return ipconfig;
}

/**
 * connman_ipconfig_ref:
 * @ipconfig: ipconfig structure
 *
 * Increase reference counter of ipconfig
 */
struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
{
	g_atomic_int_inc(&ipconfig->refcount);

	return ipconfig;
}

/**
 * connman_ipconfig_unref:
 * @ipconfig: ipconfig structure
 *
 * Decrease reference counter of ipconfig
 */
void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
{
	if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
		connman_info("%s {remove} index %d", ipconfig->interface,
							ipconfig->index);

		g_free(ipconfig->gateway);

		free_address_list(ipconfig);

		g_free(ipconfig->interface);
		g_free(ipconfig);
	}
}

/**
 * connman_ipconfig_set_method:
 * @ipconfig: ipconfig structure
 * @method: configuration method
 *
 * Set the configuration method
 */
int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
					enum connman_ipconfig_method method)
{
	ipconfig->method = method;

	return 0;
}

int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
{
	return ipconfig->index;
}

unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
{
	return ipconfig->type;
}

unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
{
	return ipconfig->flags;
}

const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
{
	return ipconfig->gateway;
}

void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
					unsigned flags, unsigned change)
{
	GString *str;

	if (flags == ipconfig->flags)
		return;

	ipconfig->flags = flags;

	str = g_string_new(NULL);
	if (str == NULL)
		return;

	if (flags & IFF_UP)
		g_string_append(str, "UP");
	else
		g_string_append(str, "DOWN");

	if (flags & IFF_RUNNING)
		g_string_append(str, ",RUNNING");

	if (flags & IFF_LOWER_UP)
		g_string_append(str, ",LOWER_UP");

	connman_info("%s {update} flags %u change %u <%s>",
				ipconfig->interface, flags, change, str->str);

	g_string_free(str, TRUE);
}

void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
				const char *label, unsigned char prefixlen,
				const char *address, const char *broadcast)
{
	struct connman_ipaddress *ipaddress;

	ipaddress = g_try_new0(struct connman_ipaddress, 1);
	if (ipaddress == NULL)
		return;

	ipaddress->prefixlen = prefixlen;
	ipaddress->address = g_strdup(address);

	ipconfig->address_list = g_slist_append(ipconfig->address_list,
								ipaddress);

	connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
						address, prefixlen, label);
}

void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
				const char *label, unsigned char prefixlen,
				const char *address, const char *broadcast)
{
	struct connman_ipaddress *ipaddress;

	ipaddress = find_ipaddress(ipconfig, prefixlen, address);
	if (ipaddress == NULL)
		return;

	ipconfig->address_list = g_slist_remove(ipconfig->address_list,
								ipaddress);

	g_free(ipaddress->address);
	g_free(ipaddress);

	connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
						address, prefixlen, label);
}

static const char *scope2str(unsigned char scope)
{
	switch (scope) {
	case 0:
		return "UNIVERSE";
	case 253:
		return "LINK";
	}

	return "";
}

void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
				unsigned char scope, const char *destination,
							const char *gateway)
{
	if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
		g_free(ipconfig->gateway);
		ipconfig->gateway = g_strdup(gateway);
	}

	connman_info("%s {add} route %s gw %s scope %u <%s>",
					ipconfig->interface, destination,
					gateway, scope, scope2str(scope));
}

void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
				unsigned char scope, const char *destination,
							const char *gateway)
{
	if (scope == 0 && g_strcmp0(destination, "0.0.0.0") == 0) {
		g_free(ipconfig->gateway);
		ipconfig->gateway = NULL;
	}

	connman_info("%s {del} route %s gw %s scope %u <%s>",
					ipconfig->interface, destination,
					gateway, scope, scope2str(scope));
}

const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
{
	switch (method) {
	case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
		break;
	case CONNMAN_IPCONFIG_METHOD_OFF:
		return "off";
	case CONNMAN_IPCONFIG_METHOD_STATIC:
		return "static";
	case CONNMAN_IPCONFIG_METHOD_DHCP:
		return "dhcp";
	}

	return NULL;
}

enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
{
	if (g_strcmp0(method, "off") == 0)
		return CONNMAN_IPCONFIG_METHOD_OFF;
	else if (g_strcmp0(method, "static") == 0)
		return CONNMAN_IPCONFIG_METHOD_STATIC;
	else if (g_strcmp0(method, "dhcp") == 0)
		return CONNMAN_IPCONFIG_METHOD_DHCP;
	else
		return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
}

static void append_variant(DBusMessageIter *iter, const char *prefix,
					const char *key, int type, void *val)
{
	char *str;

	if (prefix == NULL) {
		connman_dbus_dict_append_variant(iter, key, type, val);
		return;
	}

	str = g_strdup_printf("%s%s", prefix, key);
	if (str != NULL)
		connman_dbus_dict_append_variant(iter, str, type, val);

	g_free(str);
}

void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
				DBusMessageIter *iter, const char *prefix)
{
	const char *str;

	str = __connman_ipconfig_method2string(ipconfig->method);
	if (str == NULL)
		return;

	append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
}

int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
				const char *key, DBusMessageIter *value)
{
	int type = dbus_message_iter_get_arg_type(value);

	DBG("ipconfig %p key %s type %d", ipconfig, key, type);

	if (g_strcmp0(key, "Method") == 0) {
		const char *method;

		if (type != DBUS_TYPE_STRING)
			return -EINVAL;

		dbus_message_iter_get_basic(value, &method);

		ipconfig->method = __connman_ipconfig_string2method(method);
	} else
		return -EINVAL;

	return 0;
}

int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
		GKeyFile *keyfile, const char *identifier, const char *prefix)
{
	DBG("ipconfig %p identifier %s", ipconfig, identifier);

	return 0;
}

int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
		GKeyFile *keyfile, const char *identifier, const char *prefix)
{
	DBG("ipconfig %p identifier %s", ipconfig, identifier);

	return 0;
}

static GSList *driver_list = NULL;

static gint compare_priority(gconstpointer a, gconstpointer b)
{
	const struct connman_ipconfig_driver *driver1 = a;
	const struct connman_ipconfig_driver *driver2 = b;

	return driver2->priority - driver1->priority;
}

/**
 * connman_ipconfig_driver_register:
 * @driver: IP configuration driver
 *
 * Register a new IP configuration driver
 *
 * Returns: %0 on success
 */
int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
{
	DBG("driver %p name %s", driver, driver->name);

	driver_list = g_slist_insert_sorted(driver_list, driver,
							compare_priority);

	return 0;
}

/**
 * connman_ipconfig_driver_unregister:
 * @driver: IP configuration driver
 *
 * Remove a previously registered IP configuration driver.
 */
void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
{
	DBG("driver %p name %s", driver, driver->name);

	driver_list = g_slist_remove(driver_list, driver);
}