Fix issues with hashing of RTNL interfaces and their flags
[framework/connectivity/connman.git] / src / rtnl.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 <unistd.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30
31 #include <linux/if.h>
32 #include <linux/if_arp.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
35
36 #include <glib.h>
37
38 #include "connman.h"
39
40 struct watch_data {
41         unsigned int id;
42         int index;
43         connman_rtnl_link_cb_t newlink;
44         void *user_data;
45 };
46
47 static GSList *watch_list = NULL;
48 static unsigned int watch_id = 0;
49
50 /**
51  * connman_rtnl_add_newlink_watch:
52  * @index: network device index
53  * @callback: callback function
54  * @user_data: callback data;
55  *
56  * Add a new RTNL watch for newlink events
57  *
58  * Returns: %0 on failure and a unique id on success
59  */
60 unsigned int connman_rtnl_add_newlink_watch(int index,
61                         connman_rtnl_link_cb_t callback, void *user_data)
62 {
63         struct watch_data *watch;
64
65         watch = g_try_new0(struct watch_data, 1);
66         if (watch == NULL)
67                 return 0;
68
69         watch->id = ++watch_id;
70         watch->index = index;
71
72         watch->newlink = callback;
73         watch->user_data = user_data;
74
75         watch_list = g_slist_prepend(watch_list, watch);
76
77         DBG("id %d", watch->id);
78
79         return watch->id;
80 }
81
82 /**
83  * connman_rtnl_remove_watch:
84  * @id: watch identifier
85  *
86  * Remove the RTNL watch for the identifier
87  */
88 void connman_rtnl_remove_watch(unsigned int id)
89 {
90         GSList *list;
91
92         DBG("id %d", id);
93
94         if (id == 0)
95                 return;
96
97         for (list = watch_list; list; list = list->next) {
98                 struct watch_data *watch = list->data;
99
100                 if (watch->id  == id) {
101                         watch_list = g_slist_remove(watch_list, watch);
102                         g_free(watch);
103                         break;
104                 }
105         }
106 }
107
108 static GSList *rtnl_list = NULL;
109
110 static gint compare_priority(gconstpointer a, gconstpointer b)
111 {
112         const struct connman_rtnl *rtnl1 = a;
113         const struct connman_rtnl *rtnl2 = b;
114
115         return rtnl2->priority - rtnl1->priority;
116 }
117
118 /**
119  * connman_rtnl_register:
120  * @rtnl: RTNL module
121  *
122  * Register a new RTNL module
123  *
124  * Returns: %0 on success
125  */
126 int connman_rtnl_register(struct connman_rtnl *rtnl)
127 {
128         DBG("rtnl %p name %s", rtnl, rtnl->name);
129
130         rtnl_list = g_slist_insert_sorted(rtnl_list, rtnl,
131                                                         compare_priority);
132
133         return 0;
134 }
135
136 /**
137  * connman_rtnl_unregister:
138  * @rtnl: RTNL module
139  *
140  * Remove a previously registered RTNL module
141  */
142 void connman_rtnl_unregister(struct connman_rtnl *rtnl)
143 {
144         DBG("rtnl %p name %s", rtnl, rtnl->name);
145
146         rtnl_list = g_slist_remove(rtnl_list, rtnl);
147 }
148
149 static GHashTable *ipconfig_hash = NULL;
150
151 static void free_ipconfig(gpointer data)
152 {
153         struct connman_ipconfig *ipconfig = data;
154
155         __connman_rtnl_unregister_ipconfig(ipconfig);
156
157         connman_ipconfig_unref(ipconfig);
158 }
159
160 static void process_newlink(unsigned short type, int index,
161                                         unsigned flags, unsigned change)
162 {
163         struct connman_ipconfig *ipconfig;
164         GSList *list;
165
166         switch (type) {
167         case ARPHRD_ETHER:
168         case ARPHRD_LOOPBACK:
169         case ARPHRD_NONE:
170                 ipconfig = g_hash_table_lookup(ipconfig_hash,
171                                                 GINT_TO_POINTER(index));
172                 if (ipconfig == NULL) {
173                         ipconfig = connman_ipconfig_create(index);
174                         if (ipconfig != NULL) {
175                                 g_hash_table_insert(ipconfig_hash,
176                                         GINT_TO_POINTER(index), ipconfig);
177
178                                 __connman_rtnl_register_ipconfig(ipconfig);
179                         }
180                 }
181
182                 if (ipconfig != NULL)
183                         __connman_ipconfig_update_link(ipconfig,
184                                                         flags, change);
185                 break;
186         }
187
188         for (list = rtnl_list; list; list = list->next) {
189                 struct connman_rtnl *rtnl = list->data;
190
191                 if (rtnl->newlink)
192                         rtnl->newlink(type, index, flags, change);
193         }
194
195         for (list = watch_list; list; list = list->next) {
196                 struct watch_data *watch = list->data;
197
198                 if (watch->index != index)
199                         continue;
200
201                 if (watch->newlink)
202                         watch->newlink(flags, change, watch->user_data);
203         }
204 }
205
206 static void process_dellink(unsigned short type, int index,
207                                         unsigned flags, unsigned change)
208 {
209         GSList *list;
210
211         for (list = rtnl_list; list; list = list->next) {
212                 struct connman_rtnl *rtnl = list->data;
213
214                 if (rtnl->dellink)
215                         rtnl->dellink(type, index, flags, change);
216         }
217
218         switch (type) {
219         case ARPHRD_ETHER:
220         case ARPHRD_LOOPBACK:
221         case ARPHRD_NONE:
222                 g_hash_table_remove(ipconfig_hash, GINT_TO_POINTER(index));
223                 break;
224         }
225 }
226
227 static char *extract_gateway(struct rtmsg *msg, int bytes, int *index)
228 {
229         char *gateway = NULL;
230         struct in_addr addr;
231         struct rtattr *attr;
232
233         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
234                                         attr = RTA_NEXT(attr, bytes)) {
235                 switch (attr->rta_type) {
236                 case RTA_GATEWAY:
237                         addr = *((struct in_addr *) RTA_DATA(attr));
238                         g_free(gateway);
239                         gateway = g_strdup(inet_ntoa(addr));
240                         break;
241                 case RTA_OIF:
242                         *index = *((int *) RTA_DATA(attr));
243                         break;
244                 }
245         }
246
247         return gateway;
248 }
249
250 static void process_newgateway(struct rtmsg *msg, int bytes)
251 {
252         int index = -1;
253         char *gateway;
254         GSList *list;
255
256         gateway = extract_gateway(msg, bytes, &index);
257         if (gateway == NULL || index < 0)
258                 return;
259
260         for (list = rtnl_list; list; list = list->next) {
261                 struct connman_rtnl *rtnl = list->data;
262
263                 if (rtnl->newgateway)
264                         rtnl->newgateway(index, gateway);
265         }
266
267         g_free(gateway);
268 }
269
270 static void process_delgateway(struct rtmsg *msg, int bytes)
271 {
272         int index = -1;
273         char *gateway;
274         GSList *list;
275
276         gateway = extract_gateway(msg, bytes, &index);
277         if (gateway == NULL || index < 0)
278                 return;
279
280         for (list = rtnl_list; list; list = list->next) {
281                 struct connman_rtnl *rtnl = list->data;
282
283                 if (rtnl->delgateway)
284                         rtnl->delgateway(index, gateway);
285         }
286
287         g_free(gateway);
288 }
289
290 static void extract_addr(struct ifaddrmsg *msg, int bytes,
291                                                 const char **label,
292                                                 struct in_addr *local,
293                                                 struct in_addr *address,
294                                                 struct in_addr *broadcast)
295 {
296         struct rtattr *attr;
297
298         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
299                                         attr = RTA_NEXT(attr, bytes)) {
300                 switch (attr->rta_type) {
301                 case IFA_ADDRESS:
302                         if (address != NULL)
303                                 *address = *((struct in_addr *) RTA_DATA(attr));
304                         break;
305                 case IFA_LOCAL:
306                         if (local != NULL)
307                                 *local = *((struct in_addr *) RTA_DATA(attr));
308                         break;
309                 case IFA_BROADCAST:
310                         if (broadcast != NULL)
311                                 *broadcast = *((struct in_addr *) RTA_DATA(attr));
312                         break;
313                 case IFA_LABEL:
314                         if (label != NULL)
315                                 *label = RTA_DATA(attr);
316                         break;
317                 }
318         }
319 }
320
321 static GSList *ipconfig_list = NULL;
322
323 static void process_newaddr(int family, int prefixlen, int index,
324                                         struct ifaddrmsg *msg, int bytes)
325 {
326         GSList *list;
327         const char *label;
328         struct in_addr address;
329
330         if (family != AF_INET)
331                 return;
332
333         for (list = ipconfig_list; list; list = list->next) {
334                 struct connman_ipconfig *ipconfig = list->data;
335
336                 if (__connman_ipconfig_get_index(ipconfig) != index)
337                         continue;
338
339                 extract_addr(msg, bytes, &label, &address, NULL, NULL);
340                 __connman_ipconfig_add_address(ipconfig, label, prefixlen,
341                                                 inet_ntoa(address), NULL);
342         }
343 }
344
345 static void process_deladdr(int family, int prefixlen, int index,
346                                         struct ifaddrmsg *msg, int bytes)
347 {
348         GSList *list;
349         const char *label;
350         struct in_addr address;
351
352         if (family != AF_INET)
353                 return;
354
355         for (list = ipconfig_list; list; list = list->next) {
356                 struct connman_ipconfig *ipconfig = list->data;
357
358                 if (__connman_ipconfig_get_index(ipconfig) != index)
359                         continue;
360
361                 extract_addr(msg, bytes, &label, &address, NULL, NULL);
362                 __connman_ipconfig_del_address(ipconfig, label, prefixlen,
363                                                 inet_ntoa(address), NULL);
364         }
365 }
366
367 int __connman_rtnl_register_ipconfig(struct connman_ipconfig *ipconfig)
368 {
369         DBG("ipconfig %p", ipconfig);
370
371         ipconfig_list = g_slist_append(ipconfig_list, ipconfig);
372
373         return 0;
374 }
375
376 void __connman_rtnl_unregister_ipconfig(struct connman_ipconfig *ipconfig)
377 {
378         DBG("ipconfig %p", ipconfig);
379
380         ipconfig_list = g_slist_remove(ipconfig_list, ipconfig);
381 }
382
383 static inline void print_inet(struct rtattr *attr, const char *name, int family)
384 {
385         if (family == AF_INET) {
386                 struct in_addr addr;
387                 addr = *((struct in_addr *) RTA_DATA(attr));
388                 DBG("  attr %s (len %d) %s\n", name,
389                                 (int) RTA_PAYLOAD(attr), inet_ntoa(addr));
390         } else
391                 DBG("  attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
392 }
393
394 static inline void print_char(struct rtattr *attr, const char *name)
395 {
396         DBG("  attr %s (len %d) %s\n", name, (int) RTA_PAYLOAD(attr),
397                                                 (char *) RTA_DATA(attr));
398 }
399
400 static inline void print_byte(struct rtattr *attr, const char *name)
401 {
402         DBG("  attr %s (len %d) 0x%02x\n", name, (int) RTA_PAYLOAD(attr),
403                                         *((unsigned char *) RTA_DATA(attr)));
404 }
405
406 static inline void print_attr(struct rtattr *attr, const char *name)
407 {
408         if (name)
409                 DBG("  attr %s (len %d)\n", name, (int) RTA_PAYLOAD(attr));
410         else
411                 DBG("  attr %d (len %d)\n",
412                                 attr->rta_type, (int) RTA_PAYLOAD(attr));
413 }
414
415 static void rtnl_link(struct nlmsghdr *hdr)
416 {
417 #if 0
418         struct ifinfomsg *msg;
419         struct rtattr *attr;
420         int bytes;
421
422         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
423         bytes = IFLA_PAYLOAD(hdr);
424
425         DBG("ifi_index %d ifi_flags 0x%04x", msg->ifi_index, msg->ifi_flags);
426
427         for (attr = IFLA_RTA(msg); RTA_OK(attr, bytes);
428                                         attr = RTA_NEXT(attr, bytes)) {
429                 switch (attr->rta_type) {
430                 case IFLA_ADDRESS:
431                         print_attr(attr, "address");
432                         break;
433                 case IFLA_BROADCAST:
434                         print_attr(attr, "broadcast");
435                         break;
436                 case IFLA_IFNAME:
437                         print_char(attr, "ifname");
438                         break;
439                 case IFLA_MTU:
440                         print_attr(attr, "mtu");
441                         break;
442                 case IFLA_LINK:
443                         print_attr(attr, "link");
444                         break;
445                 case IFLA_QDISC:
446                         print_attr(attr, "qdisc");
447                         break;
448                 case IFLA_STATS:
449                         print_attr(attr, "stats");
450                         break;
451                 case IFLA_COST:
452                         print_attr(attr, "cost");
453                         break;
454                 case IFLA_PRIORITY:
455                         print_attr(attr, "priority");
456                         break;
457                 case IFLA_MASTER:
458                         print_attr(attr, "master");
459                         break;
460                 case IFLA_WIRELESS:
461                         print_attr(attr, "wireless");
462                         break;
463                 case IFLA_PROTINFO:
464                         print_attr(attr, "protinfo");
465                         break;
466                 case IFLA_TXQLEN:
467                         print_attr(attr, "txqlen");
468                         break;
469                 case IFLA_MAP:
470                         print_attr(attr, "map");
471                         break;
472                 case IFLA_WEIGHT:
473                         print_attr(attr, "weight");
474                         break;
475                 case IFLA_OPERSTATE:
476                         print_byte(attr, "operstate");
477                         break;
478                 case IFLA_LINKMODE:
479                         print_byte(attr, "linkmode");
480                         break;
481                 default:
482                         print_attr(attr, NULL);
483                         break;
484                 }
485         }
486 #endif
487 }
488
489 static void rtnl_newlink(struct nlmsghdr *hdr)
490 {
491         struct ifinfomsg *msg;
492
493         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
494
495         DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
496                                         msg->ifi_type, msg->ifi_index,
497                                         msg->ifi_flags, msg->ifi_change);
498
499         process_newlink(msg->ifi_type, msg->ifi_index,
500                                         msg->ifi_flags, msg->ifi_change);
501
502         rtnl_link(hdr);
503 }
504
505 static void rtnl_dellink(struct nlmsghdr *hdr)
506 {
507         struct ifinfomsg *msg;
508
509         msg = (struct ifinfomsg *) NLMSG_DATA(hdr);
510
511         DBG("ifi_type %d ifi_index %d ifi_flags 0x%04x ifi_change 0x%04x",
512                                         msg->ifi_type, msg->ifi_index,
513                                         msg->ifi_flags, msg->ifi_change);
514
515         process_dellink(msg->ifi_type, msg->ifi_index,
516                                         msg->ifi_flags, msg->ifi_change);
517
518         rtnl_link(hdr);
519 }
520
521 static void rtnl_addr(struct nlmsghdr *hdr)
522 {
523 #if 0
524         struct ifaddrmsg *msg;
525         struct rtattr *attr;
526         int bytes;
527
528         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
529         bytes = IFA_PAYLOAD(hdr);
530
531         DBG("ifa_family %d ifa_index %d", msg->ifa_family, msg->ifa_index);
532
533         for (attr = IFA_RTA(msg); RTA_OK(attr, bytes);
534                                         attr = RTA_NEXT(attr, bytes)) {
535                 switch (attr->rta_type) {
536                 case IFA_ADDRESS:
537                         print_inet(attr, "address", msg->ifa_family);
538                         break;
539                 case IFA_LOCAL:
540                         print_inet(attr, "local", msg->ifa_family);
541                         break;
542                 case IFA_LABEL:
543                         print_char(attr, "label");
544                         break;
545                 case IFA_BROADCAST:
546                         print_inet(attr, "broadcast", msg->ifa_family);
547                         break;
548                 case IFA_ANYCAST:
549                         print_attr(attr, "anycast");
550                         break;
551                 case IFA_CACHEINFO:
552                         print_attr(attr, "cacheinfo");
553                         break;
554                 case IFA_MULTICAST:
555                         print_attr(attr, "multicast");
556                         break;
557                 default:
558                         print_attr(attr, NULL);
559                         break;
560                 }
561         }
562 #endif
563 }
564
565 static void rtnl_newaddr(struct nlmsghdr *hdr)
566 {
567         struct ifaddrmsg *msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
568
569         DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
570                         msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
571
572         process_newaddr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
573                                                 msg, IFA_PAYLOAD(hdr));
574
575         rtnl_addr(hdr);
576 }
577
578 static void rtnl_deladdr(struct nlmsghdr *hdr)
579 {
580         struct ifaddrmsg *msg;
581
582         msg = (struct ifaddrmsg *) NLMSG_DATA(hdr);
583
584         DBG("ifa_family %d ifa_prefixlen %d ifa_index %d",
585                         msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index);
586
587         process_deladdr(msg->ifa_family, msg->ifa_prefixlen, msg->ifa_index,
588                                                 msg, IFA_PAYLOAD(hdr));
589
590         rtnl_addr(hdr);
591 }
592
593 static void rtnl_route(struct nlmsghdr *hdr)
594 {
595 #if 0
596         struct rtmsg *msg;
597         struct rtattr *attr;
598         int bytes;
599
600         msg = (struct rtmsg *) NLMSG_DATA(hdr);
601         bytes = RTM_PAYLOAD(hdr);
602
603         DBG("rtm_family %d rtm_flags 0x%04x", msg->rtm_family, msg->rtm_flags);
604
605         for (attr = RTM_RTA(msg); RTA_OK(attr, bytes);
606                                         attr = RTA_NEXT(attr, bytes)) {
607                 switch (attr->rta_type) {
608                 case RTA_DST:
609                         print_inet(attr, "dst", msg->rtm_family);
610                         break;
611                 case RTA_SRC:
612                         print_inet(attr, "src", msg->rtm_family);
613                         break;
614                 case RTA_IIF:
615                         print_char(attr, "iif");
616                         break;
617                 case RTA_OIF:
618                         print_attr(attr, "oif");
619                         break;
620                 case RTA_GATEWAY:
621                         print_inet(attr, "gateway", msg->rtm_family);
622                         break;
623                 case RTA_PRIORITY:
624                         print_attr(attr, "priority");
625                         break;
626                 case RTA_PREFSRC:
627                         print_inet(attr, "prefsrc", msg->rtm_family);
628                         break;
629                 case RTA_METRICS:
630                         print_attr(attr, "metrics");
631                         break;
632                 case RTA_TABLE:
633                         print_attr(attr, "table");
634                         break;
635                 default:
636                         print_attr(attr, NULL);
637                         break;
638                 }
639         }
640 #endif
641 }
642
643 static void rtnl_newroute(struct nlmsghdr *hdr)
644 {
645         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
646
647         if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
648                                         msg->rtm_scope == RT_SCOPE_UNIVERSE) {
649                 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
650                                         msg->rtm_table, msg->rtm_scope,
651                                         msg->rtm_type, msg->rtm_flags);
652                 process_newgateway(msg, RTM_PAYLOAD(hdr));
653         }
654
655         rtnl_route(hdr);
656 }
657
658 static void rtnl_delroute(struct nlmsghdr *hdr)
659 {
660         struct rtmsg *msg = (struct rtmsg *) NLMSG_DATA(hdr);
661
662         if (msg->rtm_type == RTN_UNICAST && msg->rtm_table == RT_TABLE_MAIN &&
663                                         msg->rtm_scope == RT_SCOPE_UNIVERSE) {
664                 DBG("rtm_table %d rtm_scope %d rtm_type %d rtm_flags 0x%04x",
665                                         msg->rtm_table, msg->rtm_scope,
666                                         msg->rtm_type, msg->rtm_flags);
667                 process_delgateway(msg, RTM_PAYLOAD(hdr));
668         }
669
670         rtnl_route(hdr);
671 }
672
673 static const char *type2string(uint16_t type)
674 {
675         switch (type) {
676         case NLMSG_NOOP:
677                 return "NOOP";
678         case NLMSG_ERROR:
679                 return "ERROR";
680         case NLMSG_DONE:
681                 return "DONE";
682         case NLMSG_OVERRUN:
683                 return "OVERRUN";
684         case RTM_GETLINK:
685                 return "GETLINK";
686         case RTM_NEWLINK:
687                 return "NEWLINK";
688         case RTM_DELLINK:
689                 return "DELLINK";
690         case RTM_NEWADDR:
691                 return "NEWADDR";
692         case RTM_DELADDR:
693                 return "DELADDR";
694         case RTM_GETROUTE:
695                 return "GETROUTE";
696         case RTM_NEWROUTE:
697                 return "NEWROUTE";
698         case RTM_DELROUTE:
699                 return "DELROUTE";
700         default:
701                 return "UNKNOWN";
702         }
703 }
704
705 static GIOChannel *channel = NULL;
706
707 struct rtnl_request {
708         struct nlmsghdr hdr;
709         struct rtgenmsg msg;
710 };
711 #define RTNL_REQUEST_SIZE  (sizeof(struct nlmsghdr) + sizeof(struct rtgenmsg))
712
713 static GSList *request_list = NULL;
714 static guint32 request_seq = 0;
715
716 static struct rtnl_request *find_request(guint32 seq)
717 {
718         GSList *list;
719
720         for (list = request_list; list; list = list->next) {
721                 struct rtnl_request *req = list->data;
722
723                 if (req->hdr.nlmsg_seq == seq)
724                         return req;
725         }
726
727         return NULL;
728 }
729
730 static int send_request(struct rtnl_request *req)
731 {
732         struct sockaddr_nl addr;
733         int sk;
734
735         DBG("%s len %d type %d flags 0x%04x seq %d",
736                                 type2string(req->hdr.nlmsg_type),
737                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
738                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
739
740         sk = g_io_channel_unix_get_fd(channel);
741
742         memset(&addr, 0, sizeof(addr));
743         addr.nl_family = AF_NETLINK;
744
745         return sendto(sk, req, req->hdr.nlmsg_len, 0,
746                                 (struct sockaddr *) &addr, sizeof(addr));
747 }
748
749 static int queue_request(struct rtnl_request *req)
750 {
751         request_list = g_slist_append(request_list, req);
752
753         if (g_slist_length(request_list) > 1)
754                 return 0;
755
756         return send_request(req);
757 }
758
759 static int process_response(guint32 seq)
760 {
761         struct rtnl_request *req;
762
763         DBG("seq %d", seq);
764
765         req = find_request(seq);
766         if (req != NULL) {
767                 request_list = g_slist_remove(request_list, req);
768                 g_free(req);
769         }
770
771         req = g_slist_nth_data(request_list, 0);
772         if (req == NULL)
773                 return 0;
774
775         return send_request(req);
776 }
777
778 static void rtnl_message(void *buf, size_t len)
779 {
780         DBG("buf %p len %zd", buf, len);
781
782         while (len > 0) {
783                 struct nlmsghdr *hdr = buf;
784                 struct nlmsgerr *err;
785
786                 if (!NLMSG_OK(hdr, len))
787                         break;
788
789                 DBG("%s len %d type %d flags 0x%04x seq %d",
790                                         type2string(hdr->nlmsg_type),
791                                         hdr->nlmsg_len, hdr->nlmsg_type,
792                                         hdr->nlmsg_flags, hdr->nlmsg_seq);
793
794                 switch (hdr->nlmsg_type) {
795                 case NLMSG_NOOP:
796                 case NLMSG_OVERRUN:
797                         return;
798                 case NLMSG_DONE:
799                         process_response(hdr->nlmsg_seq);
800                         return;
801                 case NLMSG_ERROR:
802                         err = NLMSG_DATA(hdr);
803                         DBG("error %d (%s)", -err->error,
804                                                 strerror(-err->error));
805                         return;
806                 case RTM_NEWLINK:
807                         rtnl_newlink(hdr);
808                         break;
809                 case RTM_DELLINK:
810                         rtnl_dellink(hdr);
811                         break;
812                 case RTM_NEWADDR:
813                         rtnl_newaddr(hdr);
814                         break;
815                 case RTM_DELADDR:
816                         rtnl_deladdr(hdr);
817                         break;
818                 case RTM_NEWROUTE:
819                         rtnl_newroute(hdr);
820                         break;
821                 case RTM_DELROUTE:
822                         rtnl_delroute(hdr);
823                         break;
824                 }
825
826                 len -= hdr->nlmsg_len;
827                 buf += hdr->nlmsg_len;
828         }
829 }
830
831 static gboolean netlink_event(GIOChannel *chan,
832                                 GIOCondition cond, gpointer data)
833 {
834         unsigned char buf[4096];
835         gsize len;
836         GIOError err;
837
838         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
839                 return FALSE;
840
841         memset(buf, 0, sizeof(buf));
842
843         err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
844         if (err) {
845                 if (err == G_IO_ERROR_AGAIN)
846                         return TRUE;
847                 return FALSE;
848         }
849
850         rtnl_message(buf, len);
851
852         return TRUE;
853 }
854
855 int connman_rtnl_send_getlink(void)
856 {
857         struct rtnl_request *req;
858
859         DBG("");
860
861         req = g_try_malloc0(RTNL_REQUEST_SIZE);
862         if (req == NULL)
863                 return -ENOMEM;
864
865         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
866         req->hdr.nlmsg_type = RTM_GETLINK;
867         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
868         req->hdr.nlmsg_pid = 0;
869         req->hdr.nlmsg_seq = request_seq++;
870         req->msg.rtgen_family = AF_INET;
871
872         return queue_request(req);
873 }
874
875 static int send_getaddr(void)
876 {
877         struct rtnl_request *req;
878
879         DBG("");
880
881         req = g_try_malloc0(RTNL_REQUEST_SIZE);
882         if (req == NULL)
883                 return -ENOMEM;
884
885         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
886         req->hdr.nlmsg_type = RTM_GETADDR;
887         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
888         req->hdr.nlmsg_pid = 0;
889         req->hdr.nlmsg_seq = request_seq++;
890         req->msg.rtgen_family = AF_INET;
891
892         return queue_request(req);
893 }
894
895 int connman_rtnl_send_getroute(void)
896 {
897         struct rtnl_request *req;
898
899         DBG("");
900
901         req = g_try_malloc0(RTNL_REQUEST_SIZE);
902         if (req == NULL)
903                 return -ENOMEM;
904
905         req->hdr.nlmsg_len = RTNL_REQUEST_SIZE;
906         req->hdr.nlmsg_type = RTM_GETROUTE;
907         req->hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
908         req->hdr.nlmsg_pid = 0;
909         req->hdr.nlmsg_seq = request_seq++;
910         req->msg.rtgen_family = AF_INET;
911
912         return queue_request(req);
913 }
914
915 int __connman_rtnl_init(void)
916 {
917         struct sockaddr_nl addr;
918         int sk;
919
920         DBG("");
921
922         ipconfig_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
923                                                         NULL, free_ipconfig);
924
925         sk = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
926         if (sk < 0)
927                 return -1;
928
929         memset(&addr, 0, sizeof(addr));
930         addr.nl_family = AF_NETLINK;
931         addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE;
932
933         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
934                 close(sk);
935                 return -1;
936         }
937
938         channel = g_io_channel_unix_new(sk);
939         g_io_channel_set_close_on_unref(channel, TRUE);
940
941         g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
942                                                         netlink_event, NULL);
943
944         return 0;
945 }
946
947 void __connman_rtnl_start(void)
948 {
949         DBG("");
950
951         connman_rtnl_send_getlink();
952         send_getaddr();
953         connman_rtnl_send_getroute();
954 }
955
956 void __connman_rtnl_cleanup(void)
957 {
958         GSList *list;
959
960         DBG("");
961
962         g_slist_free(ipconfig_list);
963         ipconfig_list = NULL;
964
965         g_hash_table_destroy(ipconfig_hash);
966         ipconfig_hash = NULL;
967
968         for (list = watch_list; list; list = list->next) {
969                 struct watch_data *watch = list->data;
970
971                 DBG("removing watch %d", watch->id);
972
973                 g_free(watch);
974                 list->data = NULL;
975         }
976
977         g_slist_free(watch_list);
978         watch_list = NULL;
979
980         for (list = request_list; list; list = list->next) {
981                 struct rtnl_request *req = list->data;
982
983                 DBG("%s len %d type %d flags 0x%04x seq %d",
984                                 type2string(req->hdr.nlmsg_type),
985                                 req->hdr.nlmsg_len, req->hdr.nlmsg_type,
986                                 req->hdr.nlmsg_flags, req->hdr.nlmsg_seq);
987
988                 g_free(req);
989                 list->data = NULL;
990         }
991
992         g_slist_free(request_list);
993         request_list = NULL;
994
995         g_io_channel_shutdown(channel, TRUE, NULL);
996         g_io_channel_unref(channel);
997
998         channel = NULL;
999 }