9f002a429627f9d7e00757f2d58958ccb1022430
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <net/if.h>
27
28 #ifndef IFF_LOWER_UP
29 #define IFF_LOWER_UP    0x10000
30 #endif
31
32 #include <gdbus.h>
33
34 #include "connman.h"
35
36 struct connman_ipaddress {
37         unsigned char prefixlen;
38         char *address;
39 };
40
41 struct connman_ipconfig {
42         gint refcount;
43         int index;
44         char *interface;
45         unsigned short type;
46         unsigned int flags;
47         enum connman_ipconfig_method method;
48         GSList *address_list;
49 };
50
51 static void free_address_list(struct connman_ipconfig *ipconfig)
52 {
53         GSList *list;
54
55         for (list = ipconfig->address_list; list; list = list->next) {
56                 struct connman_ipaddress *ipaddress = list->data;
57
58                 g_free(ipaddress->address);
59                 g_free(ipaddress);
60         }
61
62         g_slist_free(ipconfig->address_list);
63         ipconfig->address_list = NULL;
64 }
65
66 static struct connman_ipaddress *find_ipaddress(struct connman_ipconfig *ipconfig,
67                                 unsigned char prefixlen, const char *address)
68 {
69         GSList *list;
70
71         for (list = ipconfig->address_list; list; list = list->next) {
72                 struct connman_ipaddress *ipaddress = list->data;
73
74                 if (g_strcmp0(ipaddress->address, address) == 0 &&
75                                         ipaddress->prefixlen == prefixlen)
76                         return ipaddress;
77         }
78
79         return NULL;
80 }
81
82 /**
83  * connman_ipconfig_create:
84  *
85  * Allocate a new ipconfig structure.
86  *
87  * Returns: a newly-allocated #connman_ipconfig structure
88  */
89 struct connman_ipconfig *connman_ipconfig_create(int index)
90 {
91         struct connman_ipconfig *ipconfig;
92
93         DBG("");
94
95         ipconfig = g_try_new0(struct connman_ipconfig, 1);
96         if (ipconfig == NULL)
97                 return NULL;
98
99         ipconfig->refcount = 1;
100
101         ipconfig->index = index;
102         ipconfig->interface = connman_inet_ifname(index);
103
104         DBG("ipconfig %p", ipconfig);
105
106         connman_info("%s {create} index %d", ipconfig->interface,
107                                                         ipconfig->index);
108
109         return ipconfig;
110 }
111
112 /**
113  * connman_ipconfig_ref:
114  * @ipconfig: ipconfig structure
115  *
116  * Increase reference counter of ipconfig
117  */
118 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
119 {
120         g_atomic_int_inc(&ipconfig->refcount);
121
122         return ipconfig;
123 }
124
125 /**
126  * connman_ipconfig_unref:
127  * @ipconfig: ipconfig structure
128  *
129  * Decrease reference counter of ipconfig
130  */
131 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
132 {
133         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
134                 connman_info("%s {remove} index %d", ipconfig->interface,
135                                                         ipconfig->index);
136
137                 free_address_list(ipconfig);
138
139                 g_free(ipconfig->interface);
140                 g_free(ipconfig);
141         }
142 }
143
144 /**
145  * connman_ipconfig_set_method:
146  * @ipconfig: ipconfig structure
147  * @method: configuration method
148  *
149  * Set the configuration method
150  */
151 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
152                                         enum connman_ipconfig_method method)
153 {
154         ipconfig->method = method;
155
156         return 0;
157 }
158
159 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
160 {
161         return ipconfig->index;
162 }
163
164 unsigned short __connman_ipconfig_get_type(struct connman_ipconfig *ipconfig)
165 {
166         return ipconfig->type;
167 }
168
169 unsigned int __connman_ipconfig_get_flags(struct connman_ipconfig *ipconfig)
170 {
171         return ipconfig->flags;
172 }
173
174 void __connman_ipconfig_update_link(struct connman_ipconfig *ipconfig,
175                                         unsigned flags, unsigned change)
176 {
177         GString *str;
178
179         if (flags == ipconfig->flags)
180                 return;
181
182         ipconfig->flags = flags;
183
184         str = g_string_new(NULL);
185         if (str == NULL)
186                 return;
187
188         if (flags & IFF_UP)
189                 g_string_append(str, "UP");
190         else
191                 g_string_append(str, "DOWN");
192
193         if (flags & IFF_RUNNING)
194                 g_string_append(str, ",RUNNING");
195
196         if (flags & IFF_LOWER_UP)
197                 g_string_append(str, ",LOWER_UP");
198
199         connman_info("%s {update} flags %u change %u <%s>",
200                                 ipconfig->interface, flags, change, str->str);
201
202         g_string_free(str, TRUE);
203 }
204
205 void __connman_ipconfig_add_address(struct connman_ipconfig *ipconfig,
206                                 const char *label, unsigned char prefixlen,
207                                 const char *address, const char *broadcast)
208 {
209         struct connman_ipaddress *ipaddress;
210
211         ipaddress = g_try_new0(struct connman_ipaddress, 1);
212         if (ipaddress == NULL)
213                 return;
214
215         ipaddress->prefixlen = prefixlen;
216         ipaddress->address = g_strdup(address);
217
218         ipconfig->address_list = g_slist_append(ipconfig->address_list,
219                                                                 ipaddress);
220
221         connman_info("%s {add} address %s/%u label %s", ipconfig->interface,
222                                                 address, prefixlen, label);
223 }
224
225 void __connman_ipconfig_del_address(struct connman_ipconfig *ipconfig,
226                                 const char *label, unsigned char prefixlen,
227                                 const char *address, const char *broadcast)
228 {
229         struct connman_ipaddress *ipaddress;
230
231         ipaddress = find_ipaddress(ipconfig, prefixlen, address);
232         if (ipaddress == NULL)
233                 return;
234
235         ipconfig->address_list = g_slist_remove(ipconfig->address_list,
236                                                                 ipaddress);
237
238         g_free(ipaddress->address);
239         g_free(ipaddress);
240
241         connman_info("%s {del} address %s/%u label %s", ipconfig->interface,
242                                                 address, prefixlen, label);
243 }
244
245 static const char *scope2str(unsigned char scope)
246 {
247         switch (scope) {
248         case 0:
249                 return "UNIVERSE";
250         case 253:
251                 return "LINK";
252         }
253
254         return "";
255 }
256
257 void __connman_ipconfig_add_route(struct connman_ipconfig *ipconfig,
258                                 unsigned char scope, const char *destination,
259                                                         const char *gateway)
260 {
261         connman_info("%s {add} route %s gw %s scope %u <%s>",
262                                         ipconfig->interface, destination,
263                                         gateway, scope, scope2str(scope));
264 }
265
266 void __connman_ipconfig_del_route(struct connman_ipconfig *ipconfig,
267                                 unsigned char scope, const char *destination,
268                                                         const char *gateway)
269 {
270         connman_info("%s {del} route %s gw %s scope %u <%s>",
271                                         ipconfig->interface, destination,
272                                         gateway, scope, scope2str(scope));
273 }
274
275 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
276 {
277         switch (method) {
278         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
279                 break;
280         case CONNMAN_IPCONFIG_METHOD_OFF:
281                 return "off";
282         case CONNMAN_IPCONFIG_METHOD_STATIC:
283                 return "static";
284         case CONNMAN_IPCONFIG_METHOD_DHCP:
285                 return "dhcp";
286         }
287
288         return NULL;
289 }
290
291 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
292 {
293         if (g_strcmp0(method, "off") == 0)
294                 return CONNMAN_IPCONFIG_METHOD_OFF;
295         else if (g_strcmp0(method, "static") == 0)
296                 return CONNMAN_IPCONFIG_METHOD_STATIC;
297         else if (g_strcmp0(method, "dhcp") == 0)
298                 return CONNMAN_IPCONFIG_METHOD_DHCP;
299         else
300                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
301 }
302
303 static void append_variant(DBusMessageIter *iter, const char *prefix,
304                                         const char *key, int type, void *val)
305 {
306         char *str;
307
308         if (prefix == NULL) {
309                 connman_dbus_dict_append_variant(iter, key, type, val);
310                 return;
311         }
312
313         str = g_strdup_printf("%s%s", prefix, key);
314         if (str != NULL)
315                 connman_dbus_dict_append_variant(iter, str, type, val);
316
317         g_free(str);
318 }
319
320 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
321                                 DBusMessageIter *iter, const char *prefix)
322 {
323         const char *str;
324
325         str = __connman_ipconfig_method2string(ipconfig->method);
326         if (str == NULL)
327                 return;
328
329         append_variant(iter, prefix, "Method", DBUS_TYPE_STRING, &str);
330 }
331
332 int __connman_ipconfig_set_ipv4(struct connman_ipconfig *ipconfig,
333                                 const char *key, DBusMessageIter *value)
334 {
335         int type = dbus_message_iter_get_arg_type(value);
336
337         DBG("ipconfig %p key %s type %d", ipconfig, key, type);
338
339         if (g_strcmp0(key, "Method") == 0) {
340                 const char *method;
341
342                 if (type != DBUS_TYPE_STRING)
343                         return -EINVAL;
344
345                 dbus_message_iter_get_basic(value, &method);
346
347                 ipconfig->method = __connman_ipconfig_string2method(method);
348         } else
349                 return -EINVAL;
350
351         return 0;
352 }
353
354 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
355                 GKeyFile *keyfile, const char *identifier, const char *prefix)
356 {
357         DBG("ipconfig %p identifier %s", ipconfig, identifier);
358
359         return 0;
360 }
361
362 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
363                 GKeyFile *keyfile, const char *identifier, const char *prefix)
364 {
365         DBG("ipconfig %p identifier %s", ipconfig, identifier);
366
367         return 0;
368 }
369
370 static GSList *driver_list = NULL;
371
372 static gint compare_priority(gconstpointer a, gconstpointer b)
373 {
374         const struct connman_ipconfig_driver *driver1 = a;
375         const struct connman_ipconfig_driver *driver2 = b;
376
377         return driver2->priority - driver1->priority;
378 }
379
380 /**
381  * connman_ipconfig_driver_register:
382  * @driver: IP configuration driver
383  *
384  * Register a new IP configuration driver
385  *
386  * Returns: %0 on success
387  */
388 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
389 {
390         DBG("driver %p name %s", driver, driver->name);
391
392         driver_list = g_slist_insert_sorted(driver_list, driver,
393                                                         compare_priority);
394
395         return 0;
396 }
397
398 /**
399  * connman_ipconfig_driver_unregister:
400  * @driver: IP configuration driver
401  *
402  * Remove a previously registered IP configuration driver.
403  */
404 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
405 {
406         DBG("driver %p name %s", driver, driver->name);
407
408         driver_list = g_slist_remove(driver_list, driver);
409 }