Merge tag 'upstream/1.40' into tizen.
[platform/upstream/connman.git] / gdhcp / client.c
1 /*
2  *
3  *  DHCP client library with GLib integration
4  *
5  *  Copyright (C) 2009-2014  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 <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <arpa/inet.h>
33 #include <sys/time.h>
34 #include <resolv.h>
35
36 #include <netpacket/packet.h>
37 #include <netinet/if_ether.h>
38 #include <net/ethernet.h>
39
40 #include <linux/if.h>
41 #include <linux/filter.h>
42
43 #include <glib.h>
44
45 #include "../src/connman.h"
46 #include "../src/shared/arp.h"
47 #include "gdhcp.h"
48 #include "common.h"
49
50 #define DISCOVER_TIMEOUT 5
51 #define DISCOVER_RETRIES 6
52
53 #if defined TIZEN_EXT
54 #define REQUEST_TIMEOUT 1
55 #else
56 #define REQUEST_TIMEOUT 5
57 #endif
58 #define REQUEST_RETRIES 3
59
60 #if defined TIZEN_EXT
61 #define DISCOVER_TIMEOUT_WIFI 1
62 #define DISCOVER_RETRIES_WIFI 10
63 static int dhcp_discover_timeout = DISCOVER_TIMEOUT_WIFI;
64 static int dhcp_discover_max_retry = DISCOVER_RETRIES_WIFI;
65
66 void set_dhcp_discover_timeout(int timeout_value)
67 {
68         dhcp_discover_timeout = timeout_value;
69 }
70
71 void set_dhcp_discover_retry_count(int retry_count)
72 {
73         dhcp_discover_max_retry = retry_count;
74 }
75 #endif
76
77 typedef enum _listen_mode {
78         L_NONE,
79         L2,
80         L3,
81         L_ARP,
82 } ListenMode;
83
84 typedef enum _dhcp_client_state {
85         INIT_SELECTING,
86         REBOOTING,
87         REQUESTING,
88         BOUND,
89         DECLINED,
90         RENEWING,
91         REBINDING,
92         RELEASED,
93         IPV4LL_PROBE,
94         IPV4LL_ANNOUNCE,
95         IPV4LL_MONITOR,
96         IPV4LL_DEFEND,
97         INFORMATION_REQ,
98         SOLICITATION,
99         REQUEST,
100         CONFIRM,
101         RENEW,
102         REBIND,
103         RELEASE,
104         DECLINE,
105 } ClientState;
106
107 struct _GDHCPClient {
108         int ref_count;
109         GDHCPType type;
110         ClientState state;
111         int ifindex;
112         char *interface;
113         uint8_t mac_address[6];
114         uint32_t xid;
115         uint32_t server_ip;
116         uint32_t requested_ip;
117         char *assigned_ip;
118         time_t start;
119         uint32_t lease_seconds;
120         ListenMode listen_mode;
121         int listener_sockfd;
122         uint8_t retry_times;
123         uint8_t ack_retry_times;
124         uint8_t conflicts;
125         guint timeout;
126         guint t1_timeout;
127         guint t2_timeout;
128         guint lease_timeout;
129         guint listener_watch;
130         GList *require_list;
131         GList *request_list;
132         GHashTable *code_value_hash;
133         GHashTable *send_value_hash;
134         GHashTable *secs_bcast_hash;
135         GDHCPClientEventFunc lease_available_cb;
136         gpointer lease_available_data;
137         GDHCPClientEventFunc ipv4ll_available_cb;
138         gpointer ipv4ll_available_data;
139         GDHCPClientEventFunc no_lease_cb;
140         gpointer no_lease_data;
141         GDHCPClientEventFunc lease_lost_cb;
142         gpointer lease_lost_data;
143         GDHCPClientEventFunc ipv4ll_lost_cb;
144         gpointer ipv4ll_lost_data;
145         GDHCPClientEventFunc address_conflict_cb;
146         gpointer address_conflict_data;
147         GDHCPDebugFunc debug_func;
148         gpointer debug_data;
149         GDHCPClientEventFunc information_req_cb;
150         gpointer information_req_data;
151         GDHCPClientEventFunc solicitation_cb;
152         gpointer solicitation_data;
153         GDHCPClientEventFunc advertise_cb;
154         gpointer advertise_data;
155         GDHCPClientEventFunc request_cb;
156         gpointer request_data;
157         GDHCPClientEventFunc renew_cb;
158         gpointer renew_data;
159         GDHCPClientEventFunc rebind_cb;
160         gpointer rebind_data;
161         GDHCPClientEventFunc release_cb;
162         gpointer release_data;
163         GDHCPClientEventFunc confirm_cb;
164         gpointer confirm_data;
165         GDHCPClientEventFunc decline_cb;
166         gpointer decline_data;
167         char *last_address;
168         unsigned char *duid;
169         int duid_len;
170         unsigned char *server_duid;
171         int server_duid_len;
172         uint16_t status_code;
173         uint32_t iaid;
174         uint32_t T1, T2;
175         struct in6_addr ia_na;
176         struct in6_addr ia_ta;
177         time_t last_request;
178         uint32_t expire;
179         bool retransmit;
180         struct timeval start_time;
181         bool request_bcast;
182 #if defined TIZEN_EXT
183         uint32_t dhcp_lease_seconds;
184         gboolean init_reboot;
185 #endif
186 };
187
188 static inline void debug(GDHCPClient *client, const char *format, ...)
189 {
190         char str[256];
191         va_list ap;
192
193         if (!client->debug_func)
194                 return;
195
196         va_start(ap, format);
197
198         if (vsnprintf(str, sizeof(str), format, ap) > 0)
199                 client->debug_func(str, client->debug_data);
200
201         va_end(ap);
202 }
203
204 /* Initialize the packet with the proper defaults */
205 static void init_packet(GDHCPClient *dhcp_client, gpointer pkt, char type)
206 {
207         if (dhcp_client->type == G_DHCP_IPV6)
208                 dhcpv6_init_header(pkt, type);
209         else {
210                 struct dhcp_packet *packet = pkt;
211
212                 dhcp_init_header(packet, type);
213                 memcpy(packet->chaddr, dhcp_client->mac_address, 6);
214         }
215 }
216
217 static void add_request_options(GDHCPClient *dhcp_client,
218                                 struct dhcp_packet *packet)
219 {
220         int len = 0;
221         GList *list;
222         uint8_t code;
223         int end = dhcp_end_option(packet->options);
224
225         for (list = dhcp_client->request_list; list; list = list->next) {
226                 code = (uint8_t) GPOINTER_TO_INT(list->data);
227
228                 packet->options[end + OPT_DATA + len] = code;
229                 len++;
230         }
231
232         if (len) {
233                 packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
234                 packet->options[end + OPT_LEN] = len;
235                 packet->options[end + OPT_DATA + len] = DHCP_END;
236         }
237 }
238
239 struct hash_params {
240         unsigned char *buf;
241         int max_buf;
242         unsigned char **ptr_buf;
243 };
244
245 static void add_dhcpv6_binary_option(gpointer key, gpointer value,
246                                         gpointer user_data)
247 {
248         uint8_t *option = value;
249         uint16_t len;
250         struct hash_params *params = user_data;
251
252         /* option[0][1] contains option code */
253         len = option[2] << 8 | option[3];
254
255         if ((*params->ptr_buf + len + 2 + 2) > (params->buf + params->max_buf))
256                 return;
257
258         memcpy(*params->ptr_buf, option, len + 2 + 2);
259         (*params->ptr_buf) += len + 2 + 2;
260 }
261
262 static void add_dhcpv6_send_options(GDHCPClient *dhcp_client,
263                                 unsigned char *buf, int max_buf,
264                                 unsigned char **ptr_buf)
265 {
266         struct hash_params params = {
267                 .buf = buf,
268                 .max_buf = max_buf,
269                 .ptr_buf = ptr_buf
270         };
271
272         if (dhcp_client->type != G_DHCP_IPV6)
273                 return;
274
275         g_hash_table_foreach(dhcp_client->send_value_hash,
276                                 add_dhcpv6_binary_option, &params);
277
278         *ptr_buf = *params.ptr_buf;
279 }
280
281 static void copy_option(uint8_t *buf, uint16_t code, uint16_t len,
282                         uint8_t *msg)
283 {
284         buf[0] = code >> 8;
285         buf[1] = code & 0xff;
286         buf[2] = len >> 8;
287         buf[3] = len & 0xff;
288         if (len > 0 && msg)
289                 memcpy(&buf[4], msg, len);
290 }
291
292 static int32_t get_time_diff(struct timeval *tv)
293 {
294         struct timeval now;
295         int32_t hsec;
296
297         gettimeofday(&now, NULL);
298
299         hsec = (now.tv_sec - tv->tv_sec) * 100;
300         hsec += (now.tv_usec - tv->tv_usec) / 10000;
301
302         return hsec;
303 }
304
305 static void remove_timeouts(GDHCPClient *dhcp_client)
306 {
307
308         if (dhcp_client->timeout > 0)
309                 g_source_remove(dhcp_client->timeout);
310         if (dhcp_client->t1_timeout > 0)
311                 g_source_remove(dhcp_client->t1_timeout);
312         if (dhcp_client->t2_timeout > 0)
313                 g_source_remove(dhcp_client->t2_timeout);
314         if (dhcp_client->lease_timeout > 0)
315                 g_source_remove(dhcp_client->lease_timeout);
316
317         dhcp_client->timeout = 0;
318         dhcp_client->t1_timeout = 0;
319         dhcp_client->t2_timeout = 0;
320         dhcp_client->lease_timeout = 0;
321
322 }
323
324 static void add_dhcpv6_request_options(GDHCPClient *dhcp_client,
325                                 struct dhcpv6_packet *packet,
326                                 unsigned char *buf, int max_buf,
327                                 unsigned char **ptr_buf)
328 {
329         GList *list;
330         uint16_t code, value;
331         bool added;
332         int32_t diff;
333         int len;
334
335         if (dhcp_client->type != G_DHCP_IPV6)
336                 return;
337
338         for (list = dhcp_client->request_list; list; list = list->next) {
339                 code = (uint16_t) GPOINTER_TO_INT(list->data);
340                 added = false;
341
342                 switch (code) {
343                 case G_DHCPV6_CLIENTID:
344                         if (!dhcp_client->duid)
345                                 return;
346
347                         len = 2 + 2 + dhcp_client->duid_len;
348                         if ((*ptr_buf + len) > (buf + max_buf)) {
349                                 debug(dhcp_client, "Too long dhcpv6 message "
350                                         "when writing client id option");
351                                 return;
352                         }
353
354                         copy_option(*ptr_buf, G_DHCPV6_CLIENTID,
355                                 dhcp_client->duid_len, dhcp_client->duid);
356                         (*ptr_buf) += len;
357                         added = true;
358                         break;
359
360                 case G_DHCPV6_SERVERID:
361                         if (!dhcp_client->server_duid)
362                                 break;
363
364                         len = 2 + 2 + dhcp_client->server_duid_len;
365                         if ((*ptr_buf + len) > (buf + max_buf)) {
366                                 debug(dhcp_client, "Too long dhcpv6 message "
367                                         "when writing server id option");
368                                 return;
369                         }
370
371                         copy_option(*ptr_buf, G_DHCPV6_SERVERID,
372                                 dhcp_client->server_duid_len,
373                                 dhcp_client->server_duid);
374                         (*ptr_buf) += len;
375                         added = true;
376                         break;
377
378                 case G_DHCPV6_RAPID_COMMIT:
379                         len = 2 + 2;
380                         if ((*ptr_buf + len) > (buf + max_buf)) {
381                                 debug(dhcp_client, "Too long dhcpv6 message "
382                                         "when writing rapid commit option");
383                                 return;
384                         }
385
386                         copy_option(*ptr_buf, G_DHCPV6_RAPID_COMMIT, 0, 0);
387                         (*ptr_buf) += len;
388                         added = true;
389                         break;
390
391                 case G_DHCPV6_ORO:
392                         break;
393
394                 case G_DHCPV6_ELAPSED_TIME:
395                         if (!dhcp_client->retransmit) {
396                                 /*
397                                  * Initial message, elapsed time is 0.
398                                  */
399                                 diff = 0;
400                         } else {
401                                 diff = get_time_diff(&dhcp_client->start_time);
402                                 if (diff < 0 || diff > 0xffff)
403                                         diff = 0xffff;
404                         }
405
406                         len = 2 + 2 + 2;
407                         if ((*ptr_buf + len) > (buf + max_buf)) {
408                                 debug(dhcp_client, "Too long dhcpv6 message "
409                                         "when writing elapsed time option");
410                                 return;
411                         }
412
413                         value = htons((uint16_t)diff);
414                         copy_option(*ptr_buf, G_DHCPV6_ELAPSED_TIME,
415                                 2, (uint8_t *)&value);
416                         (*ptr_buf) += len;
417                         added = true;
418                         break;
419
420                 case G_DHCPV6_DNS_SERVERS:
421                         break;
422
423                 case G_DHCPV6_DOMAIN_LIST:
424                         break;
425
426                 case G_DHCPV6_SNTP_SERVERS:
427                         break;
428
429                 default:
430                         break;
431                 }
432
433                 if (added)
434                         debug(dhcp_client, "option %d len %d added", code, len);
435         }
436 }
437
438 static void add_binary_option(gpointer key, gpointer value, gpointer user_data)
439 {
440         uint8_t *option = value;
441         struct dhcp_packet *packet = user_data;
442
443         dhcp_add_binary_option(packet, option);
444 }
445
446 static void add_send_options(GDHCPClient *dhcp_client,
447                                 struct dhcp_packet *packet)
448 {
449         g_hash_table_foreach(dhcp_client->send_value_hash,
450                                 add_binary_option, packet);
451 }
452
453 /*
454  * Return an RFC 951- and 2131-complaint BOOTP 'secs' value that
455  * represents the number of seconds elapsed from the start of
456  * attempting DHCP to satisfy some DHCP servers that allow for an
457  * "authoritative" reply before responding.
458  */
459 static uint16_t dhcp_attempt_secs(GDHCPClient *dhcp_client)
460 {
461         return htons(MIN(time(NULL) - dhcp_client->start, UINT16_MAX));
462 }
463
464 static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
465 {
466         struct dhcp_packet packet;
467
468         debug(dhcp_client, "sending DHCP discover request");
469
470         init_packet(dhcp_client, &packet, DHCPDISCOVER);
471
472         packet.xid = dhcp_client->xid;
473         packet.secs = dhcp_attempt_secs(dhcp_client);
474
475         if (requested)
476                 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
477
478         /* Explicitly saying that we want RFC-compliant packets helps
479          * some buggy DHCP servers to NOT send bigger packets */
480         dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
481
482         add_request_options(dhcp_client, &packet);
483
484         add_send_options(dhcp_client, &packet);
485
486         /*
487          * If we do not get a reply to DISCOVER packet, then we try with
488          * broadcast flag set. So first packet is sent without broadcast flag,
489          * first retry is with broadcast flag, second retry is without it etc.
490          * Reason is various buggy routers/AP that either eat the other or vice
491          * versa. In the receiving side we then find out what kind of packet
492          * the server can send.
493          */
494         dhcp_client->request_bcast = dhcp_client->retry_times % 2;
495
496         if (dhcp_client->request_bcast)
497                 g_hash_table_add(dhcp_client->secs_bcast_hash,
498                                 GINT_TO_POINTER(packet.secs));
499
500         return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
501                                 INADDR_BROADCAST, SERVER_PORT,
502                                 MAC_BCAST_ADDR, dhcp_client->ifindex,
503                                 dhcp_client->request_bcast);
504 }
505
506 int g_dhcp_client_decline(GDHCPClient *dhcp_client, uint32_t requested)
507 {
508         struct dhcp_packet packet;
509
510         dhcp_client->state = DECLINED;
511         dhcp_client->retry_times = 0;
512
513         debug(dhcp_client, "sending DHCP decline");
514
515         init_packet(dhcp_client, &packet, DHCPDECLINE);
516
517         packet.xid = dhcp_client->xid;
518         packet.secs = dhcp_attempt_secs(dhcp_client);
519
520         if (requested)
521                 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP, requested);
522
523         add_send_options(dhcp_client, &packet);
524
525         return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
526                                 INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
527                                 dhcp_client->ifindex, true);
528 }
529
530 static int send_request(GDHCPClient *dhcp_client)
531 {
532         struct dhcp_packet packet;
533
534         debug(dhcp_client, "sending DHCP request (state %d)",
535                 dhcp_client->state);
536
537         init_packet(dhcp_client, &packet, DHCPREQUEST);
538
539         packet.xid = dhcp_client->xid;
540 #if defined TIZEN_EXT
541         if (dhcp_client->init_reboot != TRUE)
542 #endif
543         packet.secs = dhcp_attempt_secs(dhcp_client);
544
545         if (dhcp_client->state == REQUESTING || dhcp_client->state == REBOOTING)
546                 dhcp_add_option_uint32(&packet, DHCP_REQUESTED_IP,
547                                 dhcp_client->requested_ip);
548
549         if (dhcp_client->state == REQUESTING)
550                 dhcp_add_option_uint32(&packet, DHCP_SERVER_ID,
551                                 dhcp_client->server_ip);
552
553         dhcp_add_option_uint16(&packet, DHCP_MAX_SIZE, 576);
554
555         add_request_options(dhcp_client, &packet);
556
557         add_send_options(dhcp_client, &packet);
558
559         if (dhcp_client->state == RENEWING || dhcp_client->state == REBINDING)
560                 packet.ciaddr = htonl(dhcp_client->requested_ip);
561
562         if (dhcp_client->state == RENEWING)
563                 return dhcp_send_kernel_packet(&packet,
564                                 dhcp_client->requested_ip, CLIENT_PORT,
565                                 dhcp_client->server_ip, SERVER_PORT,
566                                 dhcp_client->interface);
567
568         return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
569                                 INADDR_BROADCAST, SERVER_PORT,
570                                 MAC_BCAST_ADDR, dhcp_client->ifindex,
571                                 dhcp_client->request_bcast);
572 }
573
574 static int send_release(GDHCPClient *dhcp_client,
575                         uint32_t server, uint32_t ciaddr)
576 {
577         struct dhcp_packet packet;
578         uint64_t rand;
579
580         debug(dhcp_client, "sending DHCP release request");
581
582         init_packet(dhcp_client, &packet, DHCPRELEASE);
583         __connman_util_get_random(&rand);
584         packet.xid = rand;
585         packet.ciaddr = htonl(ciaddr);
586
587         dhcp_add_option_uint32(&packet, DHCP_SERVER_ID, server);
588
589         return dhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT,
590                                                 server, SERVER_PORT,
591                                                 dhcp_client->interface);
592 }
593
594 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data);
595 static int switch_listening_mode(GDHCPClient *dhcp_client,
596                                         ListenMode listen_mode);
597
598 static gboolean send_probe_packet(gpointer dhcp_data)
599 {
600         GDHCPClient *dhcp_client;
601         guint timeout;
602
603         dhcp_client = dhcp_data;
604         /* if requested_ip is not valid, pick a new address*/
605         if (dhcp_client->requested_ip == 0) {
606                 debug(dhcp_client, "pick a new random address");
607                 dhcp_client->requested_ip = arp_random_ip();
608         }
609
610         debug(dhcp_client, "sending IPV4LL probe request");
611
612         if (dhcp_client->retry_times == 1) {
613                 dhcp_client->state = IPV4LL_PROBE;
614                 switch_listening_mode(dhcp_client, L_ARP);
615         }
616         arp_send_packet(dhcp_client->mac_address, 0,
617                         dhcp_client->requested_ip, dhcp_client->ifindex);
618
619         if (dhcp_client->retry_times < PROBE_NUM) {
620                 /*add a random timeout in range of PROBE_MIN to PROBE_MAX*/
621                 timeout = __connman_util_random_delay_ms(PROBE_MAX-PROBE_MIN);
622                 timeout += PROBE_MIN*1000;
623         } else
624                 timeout = (ANNOUNCE_WAIT * 1000);
625
626         dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
627                                                  timeout,
628                                                  ipv4ll_probe_timeout,
629                                                  dhcp_client,
630                                                  NULL);
631         return FALSE;
632 }
633
634 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data);
635 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data);
636
637 static gboolean send_announce_packet(gpointer dhcp_data)
638 {
639         GDHCPClient *dhcp_client;
640
641         dhcp_client = dhcp_data;
642
643         debug(dhcp_client, "sending IPV4LL announce request");
644
645         arp_send_packet(dhcp_client->mac_address,
646                                 dhcp_client->requested_ip,
647                                 dhcp_client->requested_ip,
648                                 dhcp_client->ifindex);
649
650         remove_timeouts(dhcp_client);
651
652         if (dhcp_client->state == IPV4LL_DEFEND) {
653                 dhcp_client->timeout =
654                         g_timeout_add_seconds_full(G_PRIORITY_HIGH,
655                                                 DEFEND_INTERVAL,
656                                                 ipv4ll_defend_timeout,
657                                                 dhcp_client,
658                                                 NULL);
659                 return TRUE;
660         } else
661                 dhcp_client->timeout =
662                         g_timeout_add_seconds_full(G_PRIORITY_HIGH,
663                                                 ANNOUNCE_INTERVAL,
664                                                 ipv4ll_announce_timeout,
665                                                 dhcp_client,
666                                                 NULL);
667         return TRUE;
668 }
669
670 void g_dhcpv6_client_set_retransmit(GDHCPClient *dhcp_client)
671 {
672         if (!dhcp_client)
673                 return;
674
675         dhcp_client->retransmit = true;
676 }
677
678 void g_dhcpv6_client_clear_retransmit(GDHCPClient *dhcp_client)
679 {
680         if (!dhcp_client)
681                 return;
682
683         dhcp_client->retransmit = false;
684 }
685
686 int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
687                         unsigned char **duid, int *duid_len)
688 {
689         time_t duid_time;
690
691         switch (duid_type) {
692         case G_DHCPV6_DUID_LLT:
693                 *duid_len = 2 + 2 + 4 + ETH_ALEN;
694                 *duid = g_try_malloc(*duid_len);
695                 if (!*duid)
696                         return -ENOMEM;
697
698                 (*duid)[0] = 0;
699                 (*duid)[1] = 1;
700                 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
701                 (*duid)[2] = 0;
702                 (*duid)[3] = type;
703                 duid_time = time(NULL) - DUID_TIME_EPOCH;
704                 (*duid)[4] = duid_time >> 24;
705                 (*duid)[5] = duid_time >> 16;
706                 (*duid)[6] = duid_time >> 8;
707                 (*duid)[7] = duid_time & 0xff;
708                 break;
709         case G_DHCPV6_DUID_EN:
710                 return -EINVAL;
711         case G_DHCPV6_DUID_LL:
712                 *duid_len = 2 + 2 + ETH_ALEN;
713                 *duid = g_try_malloc(*duid_len);
714                 if (!*duid)
715                         return -ENOMEM;
716
717                 (*duid)[0] = 0;
718                 (*duid)[1] = 3;
719                 __connman_inet_get_interface_mac_address(index, &(*duid)[2 + 2]);
720                 (*duid)[2] = 0;
721                 (*duid)[3] = type;
722                 break;
723         }
724
725         return 0;
726 }
727
728 static gchar *convert_to_hex(unsigned char *buf, int len)
729 {
730         gchar *ret = g_try_malloc(len * 2 + 1);
731         int i;
732
733         for (i = 0; ret && i < len; i++)
734                 g_snprintf(ret + i * 2, 3, "%02x", buf[i]);
735
736         return ret;
737 }
738
739 int g_dhcpv6_client_set_duid(GDHCPClient *dhcp_client, unsigned char *duid,
740                         int duid_len)
741 {
742         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
743                 return -EINVAL;
744
745         g_free(dhcp_client->duid);
746
747         dhcp_client->duid = duid;
748         dhcp_client->duid_len = duid_len;
749
750         if (dhcp_client->debug_func) {
751                 gchar *hex = convert_to_hex(duid, duid_len);
752                 debug(dhcp_client, "DUID(%d) %s", duid_len, hex);
753                 g_free(hex);
754         }
755
756         return 0;
757 }
758
759 int g_dhcpv6_client_set_pd(GDHCPClient *dhcp_client, uint32_t *T1,
760                         uint32_t *T2, GSList *prefixes)
761 {
762         uint8_t options[1452];
763         unsigned int max_buf = sizeof(options);
764         int len, count = g_slist_length(prefixes);
765
766         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
767                 return -EINVAL;
768
769         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_PD);
770
771         memset(options, 0, sizeof(options));
772
773         options[0] = dhcp_client->iaid >> 24;
774         options[1] = dhcp_client->iaid >> 16;
775         options[2] = dhcp_client->iaid >> 8;
776         options[3] = dhcp_client->iaid;
777
778         if (T1) {
779                 uint32_t t = htonl(*T1);
780                 memcpy(&options[4], &t, 4);
781         }
782
783         if (T2) {
784                 uint32_t t = htonl(*T2);
785                 memcpy(&options[8], &t, 4);
786         }
787
788         len = 12;
789
790         if (count > 0) {
791                 GSList *list;
792
793                 for (list = prefixes; list; list = list->next) {
794                         GDHCPIAPrefix *prefix = list->data;
795                         uint8_t sub_option[4+4+1+16];
796
797                         if ((len + 2 + 2 + sizeof(sub_option)) >= max_buf) {
798                                 debug(dhcp_client,
799                                         "Too long dhcpv6 message "
800                                         "when writing IA prefix option");
801                                 return -EINVAL;
802                         }
803
804                         memset(&sub_option, 0, sizeof(sub_option));
805
806                         /* preferred and validity time are left zero */
807
808                         sub_option[8] = prefix->prefixlen;
809                         memcpy(&sub_option[9], &prefix->prefix, 16);
810
811                         copy_option(&options[len], G_DHCPV6_IA_PREFIX,
812                                 sizeof(sub_option), sub_option);
813                         len += 2 + 2 + sizeof(sub_option);
814                 }
815         }
816
817         g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_PD,
818                                 options, len);
819
820         return 0;
821 }
822
823 uint32_t g_dhcpv6_client_get_iaid(GDHCPClient *dhcp_client)
824 {
825         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
826                 return 0;
827
828         return dhcp_client->iaid;
829 }
830
831 void g_dhcpv6_client_set_iaid(GDHCPClient *dhcp_client, uint32_t iaid)
832 {
833         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
834                 return;
835
836         dhcp_client->iaid = iaid;
837 }
838
839 void g_dhcpv6_client_create_iaid(GDHCPClient *dhcp_client, int index,
840                                 unsigned char *iaid)
841 {
842         uint8_t buf[6];
843
844         __connman_inet_get_interface_mac_address(index, buf);
845
846         memcpy(iaid, &buf[2], 4);
847         dhcp_client->iaid = iaid[0] << 24 |
848                         iaid[1] << 16 | iaid[2] << 8 | iaid[3];
849 }
850
851 int g_dhcpv6_client_get_timeouts(GDHCPClient *dhcp_client,
852                                 uint32_t *T1, uint32_t *T2,
853                                 time_t *started,
854                                 time_t *expire)
855 {
856         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
857                 return -EINVAL;
858
859         if (T1)
860                 *T1 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
861                         dhcp_client->T1;
862
863         if (T2)
864                 *T2 = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
865                         dhcp_client->T2;
866
867         if (started)
868                 *started = dhcp_client->last_request;
869
870         if (expire)
871                 *expire = (dhcp_client->expire == 0xffffffff) ? 0xffffffff:
872                         dhcp_client->last_request + dhcp_client->expire;
873
874         return 0;
875 }
876
877 static uint8_t *create_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
878                                 uint16_t len)
879 {
880         buf[0] = 0;
881         buf[1] = G_DHCPV6_IAADDR;
882         buf[2] = 0;
883         buf[3] = len;
884         memcpy(&buf[4], &dhcp_client->ia_na, 16);
885         memset(&buf[20], 0, 4); /* preferred */
886         memset(&buf[24], 0, 4); /* valid */
887         return buf;
888 }
889
890 static uint8_t *append_iaaddr(GDHCPClient *dhcp_client, uint8_t *buf,
891                         const char *address)
892 {
893         struct in6_addr addr;
894
895         if (inet_pton(AF_INET6, address, &addr) != 1)
896                 return NULL;
897
898         buf[0] = 0;
899         buf[1] = G_DHCPV6_IAADDR;
900         buf[2] = 0;
901         buf[3] = 24;
902         memcpy(&buf[4], &addr, 16);
903         memset(&buf[20], 0, 4); /* preferred */
904         memset(&buf[24], 0, 4); /* valid */
905         return &buf[28];
906 }
907
908 static void put_iaid(GDHCPClient *dhcp_client, int index, uint8_t *buf)
909 {
910         uint32_t iaid;
911
912         iaid = g_dhcpv6_client_get_iaid(dhcp_client);
913         if (iaid == 0) {
914                 g_dhcpv6_client_create_iaid(dhcp_client, index, buf);
915                 return;
916         }
917
918         buf[0] = iaid >> 24;
919         buf[1] = iaid >> 16;
920         buf[2] = iaid >> 8;
921         buf[3] = iaid;
922 }
923
924 int g_dhcpv6_client_set_ia(GDHCPClient *dhcp_client, int index,
925                         int code, uint32_t *T1, uint32_t *T2,
926                         bool add_iaaddr, const char *ia_na)
927 {
928         if (code == G_DHCPV6_IA_TA) {
929                 uint8_t ia_options[4];
930
931                 put_iaid(dhcp_client, index, ia_options);
932
933                 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_TA);
934                 g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_TA,
935                                         ia_options, sizeof(ia_options));
936
937         } else if (code == G_DHCPV6_IA_NA) {
938                 struct in6_addr addr;
939
940                 g_dhcp_client_set_request(dhcp_client, G_DHCPV6_IA_NA);
941
942                 /*
943                  * If caller has specified the IPv6 address it wishes to
944                  * to use (ia_na != NULL and address is valid), then send
945                  * the address to server.
946                  * If caller did not specify the address (ia_na == NULL) and
947                  * if the current address is not set, then we should not send
948                  * the address sub-option.
949                  */
950                 if (add_iaaddr && ((!ia_na &&
951                         !IN6_IS_ADDR_UNSPECIFIED(&dhcp_client->ia_na))
952                         || (ia_na &&
953                                 inet_pton(AF_INET6, ia_na, &addr) == 1))) {
954 #define IAADDR_LEN (16+4+4)
955                         uint8_t ia_options[4+4+4+2+2+IAADDR_LEN];
956
957                         if (ia_na)
958                                 memcpy(&dhcp_client->ia_na, &addr,
959                                                 sizeof(struct in6_addr));
960
961                         put_iaid(dhcp_client, index, ia_options);
962
963                         if (T1) {
964                                 ia_options[4] = *T1 >> 24;
965                                 ia_options[5] = *T1 >> 16;
966                                 ia_options[6] = *T1 >> 8;
967                                 ia_options[7] = *T1;
968                         } else
969                                 memset(&ia_options[4], 0x00, 4);
970
971                         if (T2) {
972                                 ia_options[8] = *T2 >> 24;
973                                 ia_options[9] = *T2 >> 16;
974                                 ia_options[10] = *T2 >> 8;
975                                 ia_options[11] = *T2;
976                         } else
977                                 memset(&ia_options[8], 0x00, 4);
978
979                         create_iaaddr(dhcp_client, &ia_options[12],
980                                         IAADDR_LEN);
981
982                         g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
983                                         ia_options, sizeof(ia_options));
984                 } else {
985                         uint8_t ia_options[4+4+4];
986
987                         put_iaid(dhcp_client, index, ia_options);
988
989                         memset(&ia_options[4], 0x00, 4); /* T1 (4 bytes) */
990                         memset(&ia_options[8], 0x00, 4); /* T2 (4 bytes) */
991
992                         g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_IA_NA,
993                                         ia_options, sizeof(ia_options));
994                 }
995
996         } else
997                 return -EINVAL;
998
999         return 0;
1000 }
1001
1002 int g_dhcpv6_client_set_ias(GDHCPClient *dhcp_client, int index,
1003                         int code, uint32_t *T1, uint32_t *T2,
1004                         GSList *addresses)
1005 {
1006         GSList *list;
1007         uint8_t *ia_options, *pos;
1008         int len, count, total_len;
1009
1010         count = g_slist_length(addresses);
1011         if (count == 0)
1012                 return -EINVAL;
1013
1014         g_dhcp_client_set_request(dhcp_client, code);
1015
1016         if (code == G_DHCPV6_IA_TA)
1017                 len = 4;         /* IAID */
1018         else if (code == G_DHCPV6_IA_NA)
1019                 len = 4 + 4 + 4; /* IAID + T1 + T2 */
1020         else
1021                 return -EINVAL;
1022
1023         total_len = len + count * (2 + 2 + 16 + 4 + 4);
1024         ia_options = g_try_malloc0(total_len);
1025         if (!ia_options)
1026                 return -ENOMEM;
1027
1028         put_iaid(dhcp_client, index, ia_options);
1029
1030         pos = &ia_options[len]; /* skip the IA_NA or IA_TA */
1031
1032         for (list = addresses; list; list = list->next) {
1033                 pos = append_iaaddr(dhcp_client, pos, list->data);
1034                 if (!pos)
1035                         break;
1036         }
1037
1038         if (code == G_DHCPV6_IA_NA) {
1039                 if (T1) {
1040                         ia_options[4] = *T1 >> 24;
1041                         ia_options[5] = *T1 >> 16;
1042                         ia_options[6] = *T1 >> 8;
1043                         ia_options[7] = *T1;
1044                 } else
1045                         memset(&ia_options[4], 0x00, 4);
1046
1047                 if (T2) {
1048                         ia_options[8] = *T2 >> 24;
1049                         ia_options[9] = *T2 >> 16;
1050                         ia_options[10] = *T2 >> 8;
1051                         ia_options[11] = *T2;
1052                 } else
1053                         memset(&ia_options[8], 0x00, 4);
1054         }
1055
1056         g_dhcpv6_client_set_send(dhcp_client, code, ia_options, total_len);
1057
1058         g_free(ia_options);
1059
1060         return 0;
1061 }
1062
1063 int g_dhcpv6_client_set_oro(GDHCPClient *dhcp_client, int args, ...)
1064 {
1065         va_list va;
1066         int i, j, len = sizeof(uint16_t) * args;
1067         uint8_t *values;
1068
1069         values = g_try_malloc(len);
1070         if (!values)
1071                 return -ENOMEM;
1072
1073         va_start(va, args);
1074         for (i = 0, j = 0; i < args; i++) {
1075                 uint16_t value = va_arg(va, int);
1076                 values[j++] = value >> 8;
1077                 values[j++] = value & 0xff;
1078         }
1079         va_end(va);
1080
1081         g_dhcpv6_client_set_send(dhcp_client, G_DHCPV6_ORO, values, len);
1082
1083         g_free(values);
1084
1085         return 0;
1086 }
1087
1088 static int send_dhcpv6_msg(GDHCPClient *dhcp_client, int type, char *msg)
1089 {
1090         struct dhcpv6_packet *packet;
1091         uint8_t buf[MAX_DHCPV6_PKT_SIZE];
1092         unsigned char *ptr;
1093         int ret, max_buf;
1094
1095         memset(buf, 0, sizeof(buf));
1096         packet = (struct dhcpv6_packet *)&buf[0];
1097         ptr = buf + sizeof(struct dhcpv6_packet);
1098
1099         init_packet(dhcp_client, packet, type);
1100
1101         if (!dhcp_client->retransmit) {
1102                 dhcp_client->xid = packet->transaction_id[0] << 16 |
1103                                 packet->transaction_id[1] << 8 |
1104                                 packet->transaction_id[2];
1105                 gettimeofday(&dhcp_client->start_time, NULL);
1106         } else {
1107                 packet->transaction_id[0] = dhcp_client->xid >> 16;
1108                 packet->transaction_id[1] = dhcp_client->xid >> 8 ;
1109                 packet->transaction_id[2] = dhcp_client->xid;
1110         }
1111
1112         g_dhcp_client_set_request(dhcp_client, G_DHCPV6_ELAPSED_TIME);
1113
1114         debug(dhcp_client, "sending DHCPv6 %s message xid 0x%04x", msg,
1115                                                         dhcp_client->xid);
1116
1117         max_buf = MAX_DHCPV6_PKT_SIZE - sizeof(struct dhcpv6_packet);
1118
1119         add_dhcpv6_request_options(dhcp_client, packet, buf, max_buf, &ptr);
1120
1121         add_dhcpv6_send_options(dhcp_client, buf, max_buf, &ptr);
1122
1123         ret = dhcpv6_send_packet(dhcp_client->ifindex, packet, ptr - buf);
1124
1125         debug(dhcp_client, "sent %d pkt %p len %d", ret, packet, ptr - buf);
1126         return ret;
1127 }
1128
1129 static int send_solicitation(GDHCPClient *dhcp_client)
1130 {
1131         return send_dhcpv6_msg(dhcp_client, DHCPV6_SOLICIT, "solicit");
1132 }
1133
1134 static int send_dhcpv6_request(GDHCPClient *dhcp_client)
1135 {
1136         return send_dhcpv6_msg(dhcp_client, DHCPV6_REQUEST, "request");
1137 }
1138
1139 static int send_dhcpv6_confirm(GDHCPClient *dhcp_client)
1140 {
1141         return send_dhcpv6_msg(dhcp_client, DHCPV6_CONFIRM, "confirm");
1142 }
1143
1144 static int send_dhcpv6_renew(GDHCPClient *dhcp_client)
1145 {
1146         return send_dhcpv6_msg(dhcp_client, DHCPV6_RENEW, "renew");
1147 }
1148
1149 static int send_dhcpv6_rebind(GDHCPClient *dhcp_client)
1150 {
1151         return send_dhcpv6_msg(dhcp_client, DHCPV6_REBIND, "rebind");
1152 }
1153
1154 static int send_dhcpv6_decline(GDHCPClient *dhcp_client)
1155 {
1156         return send_dhcpv6_msg(dhcp_client, DHCPV6_DECLINE, "decline");
1157 }
1158
1159 static int send_dhcpv6_release(GDHCPClient *dhcp_client)
1160 {
1161         return send_dhcpv6_msg(dhcp_client, DHCPV6_RELEASE, "release");
1162 }
1163
1164 static int send_information_req(GDHCPClient *dhcp_client)
1165 {
1166         return send_dhcpv6_msg(dhcp_client, DHCPV6_INFORMATION_REQ,
1167                                 "information-req");
1168 }
1169
1170 static void remove_value(gpointer data, gpointer user_data)
1171 {
1172         char *value = data;
1173         g_free(value);
1174 }
1175
1176 static void remove_option_value(gpointer data)
1177 {
1178         GList *option_value = data;
1179
1180         g_list_foreach(option_value, remove_value, NULL);
1181         g_list_free(option_value);
1182 }
1183
1184 GDHCPClient *g_dhcp_client_new(GDHCPType type,
1185                         int ifindex, GDHCPClientError *error)
1186 {
1187         GDHCPClient *dhcp_client;
1188
1189         if (ifindex < 0) {
1190                 *error = G_DHCP_CLIENT_ERROR_INVALID_INDEX;
1191                 return NULL;
1192         }
1193
1194         dhcp_client = g_try_new0(GDHCPClient, 1);
1195         if (!dhcp_client) {
1196                 *error = G_DHCP_CLIENT_ERROR_NOMEM;
1197                 return NULL;
1198         }
1199
1200         dhcp_client->interface = get_interface_name(ifindex);
1201         if (!dhcp_client->interface) {
1202                 *error = G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE;
1203                 goto error;
1204         }
1205
1206         if (!interface_is_up(ifindex)) {
1207                 *error = G_DHCP_CLIENT_ERROR_INTERFACE_DOWN;
1208                 goto error;
1209         }
1210
1211         __connman_inet_get_interface_mac_address(ifindex, dhcp_client->mac_address);
1212
1213         dhcp_client->listener_sockfd = -1;
1214         dhcp_client->listen_mode = L_NONE;
1215         dhcp_client->ref_count = 1;
1216         dhcp_client->type = type;
1217         dhcp_client->ifindex = ifindex;
1218         dhcp_client->lease_available_cb = NULL;
1219         dhcp_client->ipv4ll_available_cb = NULL;
1220         dhcp_client->no_lease_cb = NULL;
1221         dhcp_client->lease_lost_cb = NULL;
1222         dhcp_client->ipv4ll_lost_cb = NULL;
1223         dhcp_client->address_conflict_cb = NULL;
1224         dhcp_client->listener_watch = 0;
1225         dhcp_client->retry_times = 0;
1226         dhcp_client->ack_retry_times = 0;
1227         dhcp_client->code_value_hash = g_hash_table_new_full(g_direct_hash,
1228                                 g_direct_equal, NULL, remove_option_value);
1229         dhcp_client->send_value_hash = g_hash_table_new_full(g_direct_hash,
1230                                 g_direct_equal, NULL, g_free);
1231         dhcp_client->secs_bcast_hash = g_hash_table_new(g_direct_hash,
1232                                 g_direct_equal);
1233         dhcp_client->request_list = NULL;
1234         dhcp_client->require_list = NULL;
1235         dhcp_client->duid = NULL;
1236         dhcp_client->duid_len = 0;
1237         dhcp_client->last_request = time(NULL);
1238         dhcp_client->expire = 0;
1239         dhcp_client->request_bcast = false;
1240
1241         *error = G_DHCP_CLIENT_ERROR_NONE;
1242
1243         return dhcp_client;
1244
1245 error:
1246         g_free(dhcp_client->interface);
1247         g_free(dhcp_client);
1248         return NULL;
1249 }
1250
1251 #define SERVER_AND_CLIENT_PORTS  ((67 << 16) + 68)
1252
1253 static int dhcp_l2_socket(int ifindex)
1254 {
1255         int fd;
1256         struct sockaddr_ll sock;
1257
1258         /*
1259          * Comment:
1260          *
1261          *      I've selected not to see LL header, so BPF doesn't see it, too.
1262          *      The filter may also pass non-IP and non-ARP packets, but we do
1263          *      a more complete check when receiving the message in userspace.
1264          *
1265          * and filter shamelessly stolen from:
1266          *
1267          *      http://www.flamewarmaster.de/software/dhcpclient/
1268          *
1269          * There are a few other interesting ideas on that page (look under
1270          * "Motivation").  Use of netlink events is most interesting.  Think
1271          * of various network servers listening for events and reconfiguring.
1272          * That would obsolete sending HUP signals and/or make use of restarts.
1273          *
1274          * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
1275          * License: GPL v2.
1276          *
1277          * TODO: make conditional?
1278          */
1279         static const struct sock_filter filter_instr[] = {
1280                 /* check for udp */
1281                 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
1282                 /* L5, L1, is UDP? */
1283                 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0),
1284                 /* ugly check for arp on ethernet-like and IPv4 */
1285                 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
1286                 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4),/* L3, L4 */
1287                 /* skip IP header */
1288                 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
1289                 /* check udp source and destination ports */
1290                 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
1291                 /* L3, L4 */
1292                 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1),
1293                 /* returns */
1294                 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* L3: pass */
1295                 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
1296         };
1297
1298         static const struct sock_fprog filter_prog = {
1299                 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
1300                 /* casting const away: */
1301                 .filter = (struct sock_filter *) filter_instr,
1302         };
1303
1304         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1305         if (fd < 0)
1306                 return -errno;
1307
1308         if (SERVER_PORT == 67 && CLIENT_PORT == 68)
1309                 /* Use only if standard ports are in use */
1310                 setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
1311                                                         sizeof(filter_prog));
1312
1313         memset(&sock, 0, sizeof(sock));
1314         sock.sll_family = AF_PACKET;
1315         sock.sll_protocol = htons(ETH_P_IP);
1316         sock.sll_ifindex = ifindex;
1317
1318         if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) != 0) {
1319                 int err = -errno;
1320                 close(fd);
1321                 return err;
1322         }
1323
1324         return fd;
1325 }
1326
1327 static bool sanity_check(struct ip_udp_dhcp_packet *packet, int bytes)
1328 {
1329         if (packet->ip.protocol != IPPROTO_UDP)
1330                 return false;
1331
1332         if (packet->ip.version != IPVERSION)
1333                 return false;
1334
1335         if (packet->ip.ihl != sizeof(packet->ip) >> 2)
1336                 return false;
1337
1338         if (packet->udp.dest != htons(CLIENT_PORT))
1339                 return false;
1340
1341         if (ntohs(packet->udp.len) != (uint16_t)(bytes - sizeof(packet->ip)))
1342                 return false;
1343
1344         return true;
1345 }
1346
1347 static int dhcp_recv_l2_packet(struct dhcp_packet *dhcp_pkt, int fd,
1348                                 struct sockaddr_in *dst_addr)
1349 {
1350         int bytes;
1351         struct ip_udp_dhcp_packet packet;
1352         uint16_t check;
1353
1354         memset(&packet, 0, sizeof(packet));
1355
1356         bytes = read(fd, &packet, sizeof(packet));
1357         if (bytes < 0)
1358                 return -1;
1359
1360         if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp)))
1361                 return -1;
1362
1363         if (bytes < ntohs(packet.ip.tot_len))
1364                 /* packet is bigger than sizeof(packet), we did partial read */
1365                 return -1;
1366
1367         /* ignore any extra garbage bytes */
1368         bytes = ntohs(packet.ip.tot_len);
1369
1370         if (!sanity_check(&packet, bytes))
1371                 return -1;
1372
1373         check = packet.ip.check;
1374         packet.ip.check = 0;
1375         if (check != dhcp_checksum(&packet.ip, sizeof(packet.ip)))
1376                 return -1;
1377
1378         /* verify UDP checksum. IP header has to be modified for this */
1379         memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
1380         /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
1381         packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
1382         check = packet.udp.check;
1383         packet.udp.check = 0;
1384         if (check && check != dhcp_checksum(&packet, bytes))
1385                 return -1;
1386
1387         memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) +
1388                                                         sizeof(packet.udp)));
1389
1390         if (dhcp_pkt->cookie != htonl(DHCP_MAGIC))
1391                 return -1;
1392
1393         dst_addr->sin_addr.s_addr = packet.ip.daddr;
1394
1395         return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
1396 }
1397
1398 static void ipv4ll_start(GDHCPClient *dhcp_client)
1399 {
1400         guint timeout;
1401
1402         remove_timeouts(dhcp_client);
1403
1404         switch_listening_mode(dhcp_client, L_NONE);
1405         dhcp_client->retry_times = 0;
1406         dhcp_client->requested_ip = 0;
1407
1408         dhcp_client->requested_ip = arp_random_ip();
1409
1410         /*first wait a random delay to avoid storm of arp request on boot*/
1411         timeout = __connman_util_random_delay_ms(PROBE_WAIT);
1412
1413         dhcp_client->retry_times++;
1414         dhcp_client->timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1415                                                 timeout,
1416                                                 send_probe_packet,
1417                                                 dhcp_client,
1418                                                 NULL);
1419 }
1420
1421 static void ipv4ll_stop(GDHCPClient *dhcp_client)
1422 {
1423
1424         switch_listening_mode(dhcp_client, L_NONE);
1425
1426         remove_timeouts(dhcp_client);
1427
1428         if (dhcp_client->listener_watch > 0) {
1429                 g_source_remove(dhcp_client->listener_watch);
1430                 dhcp_client->listener_watch = 0;
1431         }
1432
1433         dhcp_client->state = IPV4LL_PROBE;
1434         dhcp_client->retry_times = 0;
1435         dhcp_client->requested_ip = 0;
1436
1437         g_free(dhcp_client->assigned_ip);
1438         dhcp_client->assigned_ip = NULL;
1439 }
1440
1441 static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
1442 {
1443         int bytes;
1444         struct ether_arp arp;
1445         uint32_t ip_requested;
1446         int source_conflict;
1447         int target_conflict;
1448         guint timeout_ms;
1449
1450         memset(&arp, 0, sizeof(arp));
1451         bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
1452         if (bytes < 0)
1453                 return bytes;
1454
1455         if (arp.arp_op != htons(ARPOP_REPLY) &&
1456                         arp.arp_op != htons(ARPOP_REQUEST))
1457                 return -EINVAL;
1458
1459         if (memcmp(arp.arp_sha, dhcp_client->mac_address, ETH_ALEN) == 0)
1460                 return 0;
1461
1462         ip_requested = htonl(dhcp_client->requested_ip);
1463         source_conflict = !memcmp(arp.arp_spa, &ip_requested,
1464                                                 sizeof(ip_requested));
1465
1466         target_conflict = !memcmp(arp.arp_tpa, &ip_requested,
1467                                 sizeof(ip_requested));
1468
1469         if (!source_conflict && !target_conflict)
1470                 return 0;
1471
1472         dhcp_client->conflicts++;
1473
1474         debug(dhcp_client, "IPV4LL conflict detected");
1475
1476         if (dhcp_client->state == IPV4LL_MONITOR) {
1477                 if (!source_conflict)
1478                         return 0;
1479                 dhcp_client->state = IPV4LL_DEFEND;
1480                 debug(dhcp_client, "DEFEND mode conflicts : %d",
1481                         dhcp_client->conflicts);
1482                 /*Try to defend with a single announce*/
1483                 send_announce_packet(dhcp_client);
1484                 return 0;
1485         }
1486
1487         if (dhcp_client->state == IPV4LL_DEFEND) {
1488                 if (!source_conflict)
1489                         return 0;
1490                 else if (dhcp_client->ipv4ll_lost_cb)
1491                         dhcp_client->ipv4ll_lost_cb(dhcp_client,
1492                                                 dhcp_client->ipv4ll_lost_data);
1493         }
1494
1495         ipv4ll_stop(dhcp_client);
1496
1497         /* If we got a lot of conflicts, RFC3927 states that we have
1498          * to wait RATE_LIMIT_INTERVAL before retrying,
1499          */
1500         if (dhcp_client->conflicts < MAX_CONFLICTS)
1501                 timeout_ms = __connman_util_random_delay_ms(PROBE_WAIT);
1502         else
1503                 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
1504         dhcp_client->retry_times++;
1505         dhcp_client->timeout =
1506                 g_timeout_add_full(G_PRIORITY_HIGH,
1507                                 timeout_ms,
1508                                 send_probe_packet,
1509                                 dhcp_client,
1510                                 NULL);
1511
1512         return 0;
1513 }
1514
1515 static bool check_package_owner(GDHCPClient *dhcp_client, gpointer pkt)
1516 {
1517         if (dhcp_client->type == G_DHCP_IPV6) {
1518                 struct dhcpv6_packet *packet6 = pkt;
1519                 uint32_t xid;
1520
1521                 if (!packet6)
1522                         return false;
1523
1524                 xid = packet6->transaction_id[0] << 16 |
1525                         packet6->transaction_id[1] << 8 |
1526                         packet6->transaction_id[2];
1527
1528                 if (xid != dhcp_client->xid)
1529                         return false;
1530         } else {
1531                 struct dhcp_packet *packet = pkt;
1532
1533                 if (packet->xid != dhcp_client->xid)
1534                         return false;
1535
1536                 if (packet->hlen != 6)
1537                         return false;
1538
1539                 if (memcmp(packet->chaddr, dhcp_client->mac_address, 6))
1540                         return false;
1541         }
1542
1543         return true;
1544 }
1545
1546 static void start_request(GDHCPClient *dhcp_client);
1547
1548 static gboolean request_timeout(gpointer user_data)
1549 {
1550         GDHCPClient *dhcp_client = user_data;
1551
1552 #if defined TIZEN_EXT
1553         if (dhcp_client->init_reboot) {
1554                 debug(dhcp_client, "DHCPREQUEST of INIT-REBOOT has failed");
1555
1556                 /* Start DHCPDISCOVERY when DHCPREQUEST of INIT-REBOOT has failed */
1557                 g_dhcp_client_set_address_known(dhcp_client, FALSE);
1558
1559                 dhcp_client->retry_times = 0;
1560                 dhcp_client->requested_ip = 0;
1561
1562                 g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1563
1564                 return FALSE;
1565         }
1566 #endif
1567         debug(dhcp_client, "request timeout (retries %d)",
1568                                         dhcp_client->retry_times);
1569
1570         dhcp_client->retry_times++;
1571
1572         start_request(dhcp_client);
1573
1574         return FALSE;
1575 }
1576
1577 static void listener_watch_destroy(gpointer user_data)
1578 {
1579         GDHCPClient *dhcp_client = user_data;
1580         g_dhcp_client_unref(dhcp_client);
1581 }
1582
1583 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
1584                                                         gpointer user_data);
1585
1586 static int switch_listening_mode(GDHCPClient *dhcp_client,
1587                                         ListenMode listen_mode)
1588 {
1589         GIOChannel *listener_channel;
1590         int listener_sockfd;
1591
1592         if (dhcp_client->listen_mode == listen_mode)
1593                 return 0;
1594
1595         debug(dhcp_client, "switch listening mode (%d ==> %d)",
1596                                 dhcp_client->listen_mode, listen_mode);
1597
1598         if (dhcp_client->listen_mode != L_NONE) {
1599                 if (dhcp_client->listener_watch > 0)
1600                         g_source_remove(dhcp_client->listener_watch);
1601                 dhcp_client->listen_mode = L_NONE;
1602                 dhcp_client->listener_sockfd = -1;
1603                 dhcp_client->listener_watch = 0;
1604         }
1605
1606         if (listen_mode == L_NONE)
1607                 return 0;
1608
1609         if (listen_mode == L2)
1610                 listener_sockfd = dhcp_l2_socket(dhcp_client->ifindex);
1611         else if (listen_mode == L3) {
1612                 if (dhcp_client->type == G_DHCP_IPV6)
1613                         listener_sockfd = dhcp_l3_socket(DHCPV6_CLIENT_PORT,
1614                                                         dhcp_client->interface,
1615                                                         AF_INET6);
1616                 else
1617                         listener_sockfd = dhcp_l3_socket(CLIENT_PORT,
1618                                                         dhcp_client->interface,
1619                                                         AF_INET);
1620         } else if (listen_mode == L_ARP)
1621                 listener_sockfd = arp_socket(dhcp_client->ifindex);
1622         else
1623                 return -EIO;
1624
1625         if (listener_sockfd < 0)
1626                 return -EIO;
1627
1628         listener_channel = g_io_channel_unix_new(listener_sockfd);
1629         if (!listener_channel) {
1630                 /* Failed to create listener channel */
1631                 close(listener_sockfd);
1632                 return -EIO;
1633         }
1634
1635         dhcp_client->listen_mode = listen_mode;
1636         dhcp_client->listener_sockfd = listener_sockfd;
1637
1638         g_io_channel_set_close_on_unref(listener_channel, TRUE);
1639         dhcp_client->listener_watch =
1640                         g_io_add_watch_full(listener_channel, G_PRIORITY_HIGH,
1641                                 G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
1642                                                 listener_event, g_dhcp_client_ref(dhcp_client),
1643                                                                 listener_watch_destroy);
1644         g_io_channel_unref(listener_channel);
1645
1646         return 0;
1647 }
1648
1649 static void start_request(GDHCPClient *dhcp_client)
1650 {
1651         debug(dhcp_client, "start request (retries %d)",
1652                                         dhcp_client->retry_times);
1653
1654         if (dhcp_client->retry_times == REQUEST_RETRIES) {
1655                 if (dhcp_client->no_lease_cb)
1656                         dhcp_client->no_lease_cb(dhcp_client,
1657                                                 dhcp_client->no_lease_data);
1658                 return;
1659         }
1660
1661         if (dhcp_client->retry_times == 0) {
1662                 dhcp_client->state = REQUESTING;
1663                 switch_listening_mode(dhcp_client, L2);
1664         }
1665
1666         send_request(dhcp_client);
1667
1668         dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1669                                                         REQUEST_TIMEOUT,
1670                                                         request_timeout,
1671                                                         dhcp_client,
1672                                                         NULL);
1673 }
1674
1675 static uint32_t get_lease(struct dhcp_packet *packet, uint16_t packet_len)
1676 {
1677         uint8_t *option;
1678         uint32_t lease_seconds;
1679
1680         option = dhcp_get_option(packet, packet_len, DHCP_LEASE_TIME);
1681         if (!option)
1682                 return 3600;
1683
1684         lease_seconds = get_be32(option);
1685
1686         if (lease_seconds < 10)
1687                 lease_seconds = 10;
1688
1689         return lease_seconds;
1690 }
1691
1692 static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
1693 {
1694         debug(dhcp_client, "restart DHCP (retries %d)", retry_times);
1695
1696         remove_timeouts(dhcp_client);
1697
1698         dhcp_client->retry_times = retry_times;
1699         dhcp_client->requested_ip = 0;
1700         dhcp_client->state = INIT_SELECTING;
1701         switch_listening_mode(dhcp_client, L2);
1702
1703         g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
1704 }
1705
1706 static gboolean start_expire(gpointer user_data)
1707 {
1708         GDHCPClient *dhcp_client = user_data;
1709
1710         debug(dhcp_client, "lease expired");
1711
1712         /*remove all timeouts if they are set*/
1713         remove_timeouts(dhcp_client);
1714
1715         restart_dhcp(dhcp_client, 0);
1716
1717         /* ip need to be cleared */
1718         if (dhcp_client->lease_lost_cb)
1719                 dhcp_client->lease_lost_cb(dhcp_client,
1720                                 dhcp_client->lease_lost_data);
1721
1722         return false;
1723 }
1724
1725 static gboolean continue_rebound(gpointer user_data)
1726 {
1727         GDHCPClient *dhcp_client = user_data;
1728         uint64_t rand;
1729
1730         switch_listening_mode(dhcp_client, L2);
1731         send_request(dhcp_client);
1732
1733         if (dhcp_client->t2_timeout> 0) {
1734                 g_source_remove(dhcp_client->t2_timeout);
1735                 dhcp_client->t2_timeout = 0;
1736         }
1737
1738         /*recalculate remaining rebind time*/
1739         dhcp_client->T2 >>= 1;
1740         if (dhcp_client->T2 > 60) {
1741                 __connman_util_get_random(&rand);
1742                 dhcp_client->t2_timeout =
1743                         g_timeout_add_full(G_PRIORITY_HIGH,
1744                                         dhcp_client->T2 * 1000 + (rand % 2000) - 1000,
1745                                         continue_rebound,
1746                                         dhcp_client,
1747                                         NULL);
1748         }
1749
1750         return FALSE;
1751 }
1752
1753 static gboolean start_rebound(gpointer user_data)
1754 {
1755         GDHCPClient *dhcp_client = user_data;
1756
1757         /*remove renew timer*/
1758         if (dhcp_client->t1_timeout > 0)
1759                 g_source_remove(dhcp_client->t1_timeout);
1760
1761         debug(dhcp_client, "start rebound");
1762         dhcp_client->state = REBINDING;
1763
1764         /*calculate total rebind time*/
1765         dhcp_client->T2 = dhcp_client->expire - dhcp_client->T2;
1766
1767         /*send the first rebound and reschedule*/
1768         continue_rebound(user_data);
1769
1770         return FALSE;
1771 }
1772
1773 static gboolean continue_renew (gpointer user_data)
1774 {
1775         GDHCPClient *dhcp_client = user_data;
1776         uint64_t rand;
1777
1778         switch_listening_mode(dhcp_client, L3);
1779         send_request(dhcp_client);
1780
1781         if (dhcp_client->t1_timeout > 0)
1782                 g_source_remove(dhcp_client->t1_timeout);
1783
1784         dhcp_client->t1_timeout = 0;
1785
1786         dhcp_client->T1 >>= 1;
1787
1788         if (dhcp_client->T1 > 60) {
1789                 __connman_util_get_random(&rand);
1790                 dhcp_client->t1_timeout = g_timeout_add_full(G_PRIORITY_HIGH,
1791                                 dhcp_client->T1 * 1000 + (rand % 2000) - 1000,
1792                                 continue_renew,
1793                                 dhcp_client,
1794                                 NULL);
1795         }
1796
1797         return FALSE;
1798 }
1799 static gboolean start_renew(gpointer user_data)
1800 {
1801         GDHCPClient *dhcp_client = user_data;
1802
1803         debug(dhcp_client, "start renew");
1804         dhcp_client->state = RENEWING;
1805
1806         /*calculate total renew period*/
1807         dhcp_client->T1 = dhcp_client->T2 - dhcp_client->T1;
1808
1809         /*send first renew and reschedule for half the remaining time.*/
1810         continue_renew(user_data);
1811
1812         return FALSE;
1813 }
1814
1815 static void start_bound(GDHCPClient *dhcp_client)
1816 {
1817         debug(dhcp_client, "start bound");
1818
1819         dhcp_client->state = BOUND;
1820
1821         remove_timeouts(dhcp_client);
1822
1823         /*
1824          *TODO: T1 and T2 should be set through options instead of
1825          * defaults as they are here.
1826          */
1827
1828         dhcp_client->T1 = dhcp_client->lease_seconds >> 1;
1829         dhcp_client->T2 = dhcp_client->lease_seconds * 0.875;
1830         dhcp_client->expire = dhcp_client->lease_seconds;
1831
1832         dhcp_client->t1_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1833                                         dhcp_client->T1,
1834                                         start_renew, dhcp_client,
1835                                                         NULL);
1836
1837         dhcp_client->t2_timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1838                                         dhcp_client->T2,
1839                                         start_rebound, dhcp_client,
1840                                                         NULL);
1841
1842         dhcp_client->lease_timeout= g_timeout_add_seconds_full(G_PRIORITY_HIGH,
1843                                         dhcp_client->expire,
1844                                         start_expire, dhcp_client,
1845                                                         NULL);
1846 }
1847
1848 static gboolean restart_dhcp_timeout(gpointer user_data)
1849 {
1850         GDHCPClient *dhcp_client = user_data;
1851
1852         debug(dhcp_client, "restart DHCP timeout");
1853
1854         if (dhcp_client->state == REBOOTING) {
1855                 g_free(dhcp_client->last_address);
1856                 dhcp_client->last_address = NULL;
1857                 restart_dhcp(dhcp_client, 0);
1858         } else {
1859                 dhcp_client->ack_retry_times++;
1860                 restart_dhcp(dhcp_client, dhcp_client->ack_retry_times);
1861         }
1862         return FALSE;
1863 }
1864
1865 static char *get_ip(uint32_t ip)
1866 {
1867         struct in_addr addr;
1868
1869         addr.s_addr = ip;
1870
1871         return g_strdup(inet_ntoa(addr));
1872 }
1873
1874 /* get a rough idea of how long an option will be */
1875 static const uint8_t len_of_option_as_string[] = {
1876         [OPTION_IP] = sizeof("255.255.255.255 "),
1877         [OPTION_STRING] = 1,
1878         [OPTION_U8] = sizeof("255 "),
1879         [OPTION_U16] = sizeof("65535 "),
1880         [OPTION_U32] = sizeof("4294967295 "),
1881 };
1882
1883 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
1884 {
1885         return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
1886 }
1887
1888 /* Create "opt_value1 option_value2 ..." string */
1889 static char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
1890 {
1891         unsigned upper_length;
1892         int len, optlen;
1893         char *dest, *ret;
1894
1895         len = option[OPT_LEN - OPT_DATA];
1896         type &= OPTION_TYPE_MASK;
1897         optlen = dhcp_option_lengths[type];
1898         if (optlen == 0)
1899                 return NULL;
1900         upper_length = len_of_option_as_string[type] *
1901                         ((unsigned)len / (unsigned)optlen);
1902         dest = ret = g_malloc(upper_length + 1);
1903         if (!ret)
1904                 return NULL;
1905
1906         while (len >= optlen) {
1907                 switch (type) {
1908                 case OPTION_IP:
1909                         dest += sprint_nip(dest, "", option);
1910                         break;
1911                 case OPTION_U16: {
1912                         uint16_t val_u16 = get_be16(option);
1913                         dest += sprintf(dest, "%u", val_u16);
1914                         break;
1915                 }
1916                 case OPTION_U32: {
1917                         uint32_t val_u32 = get_be32(option);
1918                         dest += sprintf(dest, "%u", val_u32);
1919                         break;
1920                 }
1921                 case OPTION_STRING:
1922                         memcpy(dest, option, len);
1923                         dest[len] = '\0';
1924                         return ret;
1925                 default:
1926                         break;
1927                 }
1928                 option += optlen;
1929                 len -= optlen;
1930                 if (len <= 0)
1931                         break;
1932                 *dest++ = ' ';
1933                 *dest = '\0';
1934         }
1935
1936         return ret;
1937 }
1938
1939 static GList *get_option_value_list(char *value, GDHCPOptionType type)
1940 {
1941         char *pos = value;
1942         GList *list = NULL;
1943
1944         if (!pos)
1945                 return NULL;
1946
1947         if (type == OPTION_STRING)
1948                 return g_list_append(list, g_strdup(value));
1949
1950         while ((pos = strchr(pos, ' '))) {
1951                 *pos = '\0';
1952
1953                 list = g_list_append(list, g_strdup(value));
1954
1955                 value = ++pos;
1956         }
1957
1958         list = g_list_append(list, g_strdup(value));
1959
1960         return list;
1961 }
1962
1963 static inline uint32_t get_uint32(unsigned char *value)
1964 {
1965         return value[0] << 24 | value[1] << 16 |
1966                 value[2] << 8 | value[3];
1967 }
1968
1969 static inline uint16_t get_uint16(unsigned char *value)
1970 {
1971         return value[0] << 8 | value[1];
1972 }
1973
1974 static GList *add_prefix(GDHCPClient *dhcp_client, GList *list,
1975                         struct in6_addr *addr,
1976                         unsigned char prefixlen, uint32_t preferred,
1977                         uint32_t valid)
1978 {
1979         GDHCPIAPrefix *ia_prefix;
1980
1981         ia_prefix = g_try_new(GDHCPIAPrefix, 1);
1982         if (!ia_prefix)
1983                 return list;
1984
1985         if (dhcp_client->debug_func) {
1986                 char addr_str[INET6_ADDRSTRLEN + 1];
1987                 inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
1988                 debug(dhcp_client, "prefix %s/%d preferred %u valid %u",
1989                         addr_str, prefixlen, preferred, valid);
1990         }
1991
1992         memcpy(&ia_prefix->prefix, addr, sizeof(struct in6_addr));
1993         ia_prefix->prefixlen = prefixlen;
1994         ia_prefix->preferred = preferred;
1995         ia_prefix->valid = valid;
1996         ia_prefix->expire = time(NULL) + valid;
1997
1998         return g_list_prepend(list, ia_prefix);
1999 }
2000
2001 static GList *get_addresses(GDHCPClient *dhcp_client,
2002                                 int code, int len,
2003                                 unsigned char *value,
2004                                 uint16_t *status)
2005 {
2006         GList *list = NULL;
2007         struct in6_addr addr;
2008         uint32_t iaid, T1 = 0, T2 = 0, preferred = 0, valid = 0;
2009         uint16_t option_len, option_code, st = 0, max_len;
2010         int addr_count = 0, prefix_count = 0, i, pos;
2011         unsigned char prefixlen;
2012         unsigned int shortest_valid = 0;
2013         uint8_t *option;
2014         char *str;
2015
2016         if (!value || len < 4)
2017                 return NULL;
2018
2019         iaid = get_uint32(&value[0]);
2020         if (dhcp_client->iaid != iaid)
2021                 return NULL;
2022
2023         if (code == G_DHCPV6_IA_NA || code == G_DHCPV6_IA_PD) {
2024                 T1 = get_uint32(&value[4]);
2025                 T2 = get_uint32(&value[8]);
2026
2027                 if (T1 > T2)
2028                         /* IA_NA: RFC 3315, 22.4 */
2029                         /* IA_PD: RFC 3633, ch 9 */
2030                         return NULL;
2031
2032                 pos = 12;
2033         } else
2034                 pos = 4;
2035
2036         if (len <= pos)
2037                 return NULL;
2038
2039         max_len = len - pos;
2040
2041         debug(dhcp_client, "header %d sub-option max len %d", pos, max_len);
2042
2043         /* We have more sub-options in this packet. */
2044         do {
2045                 option = dhcpv6_get_sub_option(&value[pos], max_len,
2046                                         &option_code, &option_len);
2047
2048                 debug(dhcp_client, "pos %d option %p code %d len %d",
2049                         pos, option, option_code, option_len);
2050
2051                 if (!option)
2052                         break;
2053
2054                 if (pos >= len)
2055                         break;
2056
2057                 switch (option_code) {
2058                 case G_DHCPV6_IAADDR:
2059                         i = 0;
2060                         memcpy(&addr, &option[0], sizeof(addr));
2061                         i += sizeof(addr);
2062                         preferred = get_uint32(&option[i]);
2063                         i += 4;
2064                         valid = get_uint32(&option[i]);
2065
2066                         addr_count++;
2067                         break;
2068
2069                 case G_DHCPV6_STATUS_CODE:
2070                         st = get_uint16(&option[0]);
2071                         debug(dhcp_client, "error code %d", st);
2072                         if (option_len > 2) {
2073                                 str = g_strndup((gchar *)&option[2],
2074                                                 option_len - 2);
2075                                 debug(dhcp_client, "error text: %s", str);
2076                                 g_free(str);
2077                         }
2078
2079                         *status = st;
2080                         break;
2081
2082                 case G_DHCPV6_IA_PREFIX:
2083                         i = 0;
2084                         preferred = get_uint32(&option[i]);
2085                         i += 4;
2086                         valid = get_uint32(&option[i]);
2087                         i += 4;
2088                         prefixlen = option[i];
2089                         i += 1;
2090                         memcpy(&addr, &option[i], sizeof(addr));
2091                         i += sizeof(addr);
2092                         if (preferred < valid) {
2093                                 /* RFC 3633, ch 10 */
2094                                 list = add_prefix(dhcp_client, list, &addr,
2095                                                 prefixlen, preferred, valid);
2096                                 if (shortest_valid > valid)
2097                                         shortest_valid = valid;
2098                                 prefix_count++;
2099                         }
2100                         break;
2101                 }
2102
2103                 pos += 2 + 2 + option_len;
2104
2105         } while (pos < len);
2106
2107         if (addr_count > 0 && st == 0) {
2108                 /* We only support one address atm */
2109                 char addr_str[INET6_ADDRSTRLEN + 1];
2110
2111                 if (preferred > valid)
2112                         /* RFC 3315, 22.6 */
2113                         return NULL;
2114
2115                 dhcp_client->T1 = T1;
2116                 dhcp_client->T2 = T2;
2117
2118                 inet_ntop(AF_INET6, &addr, addr_str, INET6_ADDRSTRLEN);
2119                 debug(dhcp_client, "address count %d addr %s T1 %u T2 %u",
2120                         addr_count, addr_str, T1, T2);
2121
2122                 list = g_list_append(list, g_strdup(addr_str));
2123
2124                 if (code == G_DHCPV6_IA_NA)
2125                         memcpy(&dhcp_client->ia_na, &addr,
2126                                                 sizeof(struct in6_addr));
2127                 else
2128                         memcpy(&dhcp_client->ia_ta, &addr,
2129                                                 sizeof(struct in6_addr));
2130
2131                 if (valid != dhcp_client->expire)
2132                         dhcp_client->expire = valid;
2133         }
2134
2135         if (prefix_count > 0 && list) {
2136                 /*
2137                  * This means we have a list of prefixes to delegate.
2138                  */
2139                 list = g_list_reverse(list);
2140
2141                 debug(dhcp_client, "prefix count %d T1 %u T2 %u",
2142                         prefix_count, T1, T2);
2143
2144                 dhcp_client->T1 = T1;
2145                 dhcp_client->T2 = T2;
2146
2147                 dhcp_client->expire = shortest_valid;
2148         }
2149
2150         if (status && *status != 0)
2151                 debug(dhcp_client, "status %d", *status);
2152
2153         return list;
2154 }
2155
2156 static GList *get_domains(int maxlen, unsigned char *value)
2157
2158 {
2159         GList *list = NULL;
2160         int pos = 0;
2161         unsigned char *c;
2162         char dns_name[NS_MAXDNAME + 1];
2163
2164         if (!value || maxlen < 3)
2165                 return NULL;
2166
2167         while (pos < maxlen) {
2168                 strncpy(dns_name, (char *)&value[pos], NS_MAXDNAME);
2169
2170                 c = (unsigned char *)dns_name;
2171                 while (c && *c) {
2172                         int jump;
2173                         jump = *c;
2174                         *c = '.';
2175                         c += jump + 1;
2176                 }
2177                 list = g_list_prepend(list, g_strdup(&dns_name[1]));
2178                 pos += (char *)c - dns_name + 1;
2179         }
2180
2181         return g_list_reverse(list);
2182 }
2183
2184 static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
2185                                         int code, int len,
2186                                         unsigned char *value,
2187                                         uint16_t *status)
2188 {
2189         GList *list = NULL;
2190         char *str;
2191         int i;
2192
2193         if (!value)
2194                 return NULL;
2195
2196         switch (code) {
2197         case G_DHCPV6_DNS_SERVERS:      /* RFC 3646, chapter 3 */
2198         case G_DHCPV6_SNTP_SERVERS:     /* RFC 4075, chapter 4 */
2199                 if (len % 16) {
2200                         debug(dhcp_client,
2201                                 "%s server list length (%d) is invalid",
2202                                 code == G_DHCPV6_DNS_SERVERS ? "DNS" : "SNTP",
2203                                 len);
2204                         return NULL;
2205                 }
2206                 for (i = 0; i < len; i += 16) {
2207
2208                         str = g_try_malloc0(INET6_ADDRSTRLEN+1);
2209                         if (!str)
2210                                 return list;
2211
2212                         if (!inet_ntop(AF_INET6, &value[i], str,
2213                                         INET6_ADDRSTRLEN))
2214                                 g_free(str);
2215                         else
2216                                 list = g_list_append(list, str);
2217                 }
2218                 break;
2219
2220         case G_DHCPV6_IA_NA:            /* RFC 3315, chapter 22.4 */
2221         case G_DHCPV6_IA_TA:            /* RFC 3315, chapter 22.5 */
2222         case G_DHCPV6_IA_PD:            /* RFC 3633, chapter 9 */
2223                 list = get_addresses(dhcp_client, code, len, value, status);
2224                 break;
2225
2226         case G_DHCPV6_DOMAIN_LIST:
2227                 list = get_domains(len, value);
2228                 break;
2229
2230         default:
2231                 break;
2232         }
2233
2234         return list;
2235 }
2236
2237 static void get_dhcpv6_request(GDHCPClient *dhcp_client,
2238                                 struct dhcpv6_packet *packet,
2239                                 uint16_t pkt_len, uint16_t *status)
2240 {
2241         GList *list, *value_list;
2242         uint8_t *option;
2243         uint16_t code;
2244         uint16_t option_len;
2245
2246         for (list = dhcp_client->request_list; list; list = list->next) {
2247                 code = (uint16_t) GPOINTER_TO_INT(list->data);
2248
2249                 option = dhcpv6_get_option(packet, pkt_len, code, &option_len,
2250                                                 NULL);
2251                 if (!option) {
2252                         g_hash_table_remove(dhcp_client->code_value_hash,
2253                                                 GINT_TO_POINTER((int) code));
2254                         continue;
2255                 }
2256
2257                 value_list = get_dhcpv6_option_value_list(dhcp_client, code,
2258                                                 option_len, option, status);
2259
2260                 debug(dhcp_client, "code %d %p len %d list %p", code, option,
2261                         option_len, value_list);
2262
2263                 if (!value_list)
2264                         g_hash_table_remove(dhcp_client->code_value_hash,
2265                                                 GINT_TO_POINTER((int) code));
2266                 else
2267                         g_hash_table_insert(dhcp_client->code_value_hash,
2268                                 GINT_TO_POINTER((int) code), value_list);
2269         }
2270 }
2271
2272 static void get_request(GDHCPClient *dhcp_client, struct dhcp_packet *packet,
2273                 uint16_t packet_len)
2274 {
2275         GDHCPOptionType type;
2276         GList *list, *value_list;
2277         char *option_value;
2278         uint8_t *option;
2279         uint8_t code;
2280
2281         for (list = dhcp_client->request_list; list; list = list->next) {
2282                 code = (uint8_t) GPOINTER_TO_INT(list->data);
2283
2284                 option = dhcp_get_option(packet, packet_len, code);
2285                 if (!option) {
2286                         g_hash_table_remove(dhcp_client->code_value_hash,
2287                                                 GINT_TO_POINTER((int) code));
2288                         continue;
2289                 }
2290
2291                 type =  dhcp_get_code_type(code);
2292
2293                 option_value = malloc_option_value_string(option, type);
2294                 if (!option_value)
2295                         g_hash_table_remove(dhcp_client->code_value_hash,
2296                                                 GINT_TO_POINTER((int) code));
2297
2298                 value_list = get_option_value_list(option_value, type);
2299
2300                 g_free(option_value);
2301
2302                 if (!value_list)
2303                         g_hash_table_remove(dhcp_client->code_value_hash,
2304                                                 GINT_TO_POINTER((int) code));
2305                 else
2306                         g_hash_table_insert(dhcp_client->code_value_hash,
2307                                 GINT_TO_POINTER((int) code), value_list);
2308         }
2309 }
2310
2311 static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
2312                                                         gpointer user_data)
2313 {
2314         GDHCPClient *dhcp_client = user_data;
2315         struct sockaddr_in dst_addr = { 0 };
2316         struct dhcp_packet packet = { 0 };
2317         struct dhcpv6_packet *packet6 = NULL;
2318         uint8_t *message_type = NULL, *client_id = NULL, *option,
2319                 *server_id = NULL;
2320         uint16_t option_len = 0, status = 0;
2321         uint32_t xid = 0;
2322         gpointer pkt;
2323         unsigned char buf[MAX_DHCPV6_PKT_SIZE];
2324         uint16_t pkt_len = 0;
2325         int count;
2326         int re;
2327
2328         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2329                 dhcp_client->listener_watch = 0;
2330 #if defined TIZEN_EXT
2331                 /* re-register event listener when socket failed */
2332                 int retry_count = 0;
2333                 int ret = -1;
2334                 while (retry_count++ < GIO_SOCKET_RETRY_COUNT && ret < 0)
2335                         ret = switch_listening_mode(dhcp_client,
2336                                         dhcp_client->type);
2337 #endif /* defined TIZEN_EXT */
2338                 return FALSE;
2339         }
2340
2341         if (dhcp_client->listen_mode == L_NONE)
2342                 return FALSE;
2343
2344         pkt = &packet;
2345
2346         dhcp_client->status_code = 0;
2347
2348         if (dhcp_client->listen_mode == L2) {
2349                 re = dhcp_recv_l2_packet(&packet,
2350                                         dhcp_client->listener_sockfd,
2351                                         &dst_addr);
2352                 pkt_len = (uint16_t)(unsigned int)re;
2353                 xid = packet.xid;
2354         } else if (dhcp_client->listen_mode == L3) {
2355                 if (dhcp_client->type == G_DHCP_IPV6) {
2356                         re = dhcpv6_recv_l3_packet(&packet6, buf, sizeof(buf),
2357                                                 dhcp_client->listener_sockfd);
2358                         if (re < 0)
2359                             return TRUE;
2360                         pkt_len = re;
2361                         pkt = packet6;
2362                         xid = packet6->transaction_id[0] << 16 |
2363                                 packet6->transaction_id[1] << 8 |
2364                                 packet6->transaction_id[2];
2365                 } else {
2366                         re = dhcp_recv_l3_packet(&packet,
2367                                                 dhcp_client->listener_sockfd);
2368                         xid = packet.xid;
2369                 }
2370         } else if (dhcp_client->listen_mode == L_ARP) {
2371                 ipv4ll_recv_arp_packet(dhcp_client);
2372                 return TRUE;
2373         } else
2374                 re = -EIO;
2375
2376         if (re < 0)
2377                 return TRUE;
2378
2379         if (!check_package_owner(dhcp_client, pkt))
2380                 return TRUE;
2381
2382         if (dhcp_client->type == G_DHCP_IPV6) {
2383                 if (!packet6)
2384                         return TRUE;
2385
2386                 count = 0;
2387                 client_id = dhcpv6_get_option(packet6, pkt_len,
2388                                 G_DHCPV6_CLIENTID, &option_len, &count);
2389
2390                 if (!client_id || count == 0 || option_len == 0 ||
2391                                 memcmp(dhcp_client->duid, client_id,
2392                                         dhcp_client->duid_len) != 0) {
2393                         debug(dhcp_client,
2394                                 "client duid error, discarding msg %p/%d/%d",
2395                                 client_id, option_len, count);
2396                         return TRUE;
2397                 }
2398
2399                 option = dhcpv6_get_option(packet6, pkt_len,
2400                                 G_DHCPV6_STATUS_CODE, &option_len, NULL);
2401                 if (option != 0 && option_len > 0) {
2402                         status = option[0]<<8 | option[1];
2403                         if (status != 0) {
2404                                 debug(dhcp_client, "error code %d", status);
2405                                 if (option_len > 2) {
2406                                         gchar *txt = g_strndup(
2407                                                 (gchar *)&option[2],
2408                                                 option_len - 2);
2409                                         debug(dhcp_client, "error text: %s",
2410                                                 txt);
2411                                         g_free(txt);
2412                                 }
2413                         }
2414                         dhcp_client->status_code = status;
2415                 }
2416         } else {
2417                 message_type = dhcp_get_option(&packet, pkt_len, DHCP_MESSAGE_TYPE);
2418                 if (!message_type)
2419                         return TRUE;
2420         }
2421
2422         debug(dhcp_client, "received DHCP packet xid 0x%04x "
2423                 "(current state %d)", ntohl(xid), dhcp_client->state);
2424
2425         switch (dhcp_client->state) {
2426         case INIT_SELECTING:
2427                 if (*message_type != DHCPOFFER)
2428                         return TRUE;
2429
2430                 remove_timeouts(dhcp_client);
2431                 dhcp_client->timeout = 0;
2432                 dhcp_client->retry_times = 0;
2433
2434                 option = dhcp_get_option(&packet, pkt_len, DHCP_SERVER_ID);
2435                 dhcp_client->server_ip = get_be32(option);
2436                 dhcp_client->requested_ip = ntohl(packet.yiaddr);
2437
2438                 dhcp_client->state = REQUESTING;
2439
2440                 /*
2441                  * RFC2131:
2442                  *
2443                  *   If unicasting is not possible, the message MAY be
2444                  *   sent as an IP broadcast using an IP broadcast address
2445                  *   (preferably 0xffffffff) as the IP destination address
2446                  *   and the link-layer broadcast address as the link-layer
2447                  *   destination address.
2448                  *
2449                  * For interoperability reasons, if the response is an IP
2450                  * broadcast, let's reuse broadcast flag from DHCPDISCOVER
2451                  * to which the server has responded. Some servers are picky
2452                  * about this flag.
2453                  */
2454                 dhcp_client->request_bcast =
2455                         dst_addr.sin_addr.s_addr == INADDR_BROADCAST &&
2456                         g_hash_table_contains(dhcp_client->secs_bcast_hash,
2457                                 GINT_TO_POINTER(packet.secs));
2458
2459                 debug(dhcp_client, "init ip %s secs %hu -> broadcast flag %s",
2460                         inet_ntoa(dst_addr.sin_addr), packet.secs,
2461                         dhcp_client->request_bcast ? "on" : "off");
2462
2463                 start_request(dhcp_client);
2464
2465                 return TRUE;
2466         case REBOOTING:
2467                 if (dst_addr.sin_addr.s_addr == INADDR_BROADCAST)
2468                         dhcp_client->request_bcast = true;
2469                 else
2470                         dhcp_client->request_bcast = false;
2471
2472                 debug(dhcp_client, "ip %s -> %sadding broadcast flag",
2473                         inet_ntoa(dst_addr.sin_addr),
2474                         dhcp_client->request_bcast ? "" : "not ");
2475                 /* fall through */
2476         case REQUESTING:
2477         case RENEWING:
2478         case REBINDING:
2479                 if (*message_type == DHCPACK) {
2480                         dhcp_client->retry_times = 0;
2481
2482                         remove_timeouts(dhcp_client);
2483
2484                         dhcp_client->lease_seconds = get_lease(&packet, pkt_len);
2485
2486 #if defined TIZEN_EXT
2487                         dhcp_client->dhcp_lease_seconds = dhcp_client->lease_seconds;
2488 #endif
2489
2490                         get_request(dhcp_client, &packet, pkt_len);
2491
2492                         switch_listening_mode(dhcp_client, L_NONE);
2493
2494                         g_free(dhcp_client->assigned_ip);
2495                         dhcp_client->assigned_ip = get_ip(packet.yiaddr);
2496
2497                         if (dhcp_client->state == REBOOTING) {
2498                                 option = dhcp_get_option(&packet, pkt_len,
2499                                                         DHCP_SERVER_ID);
2500                                 dhcp_client->server_ip = get_be32(option);
2501                         }
2502
2503                         /* Address should be set up here */
2504                         if (dhcp_client->lease_available_cb)
2505                                 dhcp_client->lease_available_cb(dhcp_client,
2506                                         dhcp_client->lease_available_data);
2507
2508                         start_bound(dhcp_client);
2509                 } else if (*message_type == DHCPNAK) {
2510                         dhcp_client->retry_times = 0;
2511
2512                         remove_timeouts(dhcp_client);
2513
2514 #if defined TIZEN_EXT
2515                         if (dhcp_client->init_reboot) {
2516                                 g_dhcp_client_set_address_known(dhcp_client, FALSE);
2517                                 dhcp_client->timeout = g_idle_add_full(
2518                                                                 G_PRIORITY_HIGH,
2519                                                                 restart_dhcp_timeout,
2520                                                                 dhcp_client,
2521                                                                 NULL);
2522
2523                                 break;
2524                         }
2525 #endif
2526                         dhcp_client->timeout = g_timeout_add_seconds_full(
2527                                                         G_PRIORITY_HIGH, 3,
2528                                                         restart_dhcp_timeout,
2529                                                         dhcp_client,
2530                                                         NULL);
2531                 }
2532
2533                 break;
2534         case SOLICITATION:
2535                 if (dhcp_client->type != G_DHCP_IPV6)
2536                         return TRUE;
2537
2538                 if (packet6->message != DHCPV6_REPLY &&
2539                                 packet6->message != DHCPV6_ADVERTISE)
2540                         return TRUE;
2541
2542                 count = 0;
2543                 server_id = dhcpv6_get_option(packet6, pkt_len,
2544                                 G_DHCPV6_SERVERID, &option_len, &count);
2545                 if (!server_id || count != 1 || option_len == 0) {
2546                         /* RFC 3315, 15.10 */
2547                         debug(dhcp_client,
2548                                 "server duid error, discarding msg %p/%d/%d",
2549                                 server_id, option_len, count);
2550                         return TRUE;
2551                 }
2552                 dhcp_client->server_duid = g_try_malloc(option_len);
2553                 if (!dhcp_client->server_duid)
2554                         return TRUE;
2555                 memcpy(dhcp_client->server_duid, server_id, option_len);
2556                 dhcp_client->server_duid_len = option_len;
2557
2558                 if (packet6->message == DHCPV6_REPLY) {
2559                         uint8_t *rapid_commit;
2560                         count = 0;
2561                         option_len = 0;
2562                         rapid_commit = dhcpv6_get_option(packet6, pkt_len,
2563                                                         G_DHCPV6_RAPID_COMMIT,
2564                                                         &option_len, &count);
2565                         if (!rapid_commit || option_len != 0 ||
2566                                                                 count != 1)
2567                                 /* RFC 3315, 17.1.4 */
2568                                 return TRUE;
2569                 }
2570
2571                 switch_listening_mode(dhcp_client, L_NONE);
2572
2573                 if (dhcp_client->status_code == 0)
2574                         get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2575                                         &dhcp_client->status_code);
2576
2577                 if (packet6->message == DHCPV6_ADVERTISE) {
2578                         if (dhcp_client->advertise_cb)
2579                                 dhcp_client->advertise_cb(dhcp_client,
2580                                                 dhcp_client->advertise_data);
2581                         return TRUE;
2582                 }
2583
2584                 if (dhcp_client->solicitation_cb) {
2585                         /*
2586                          * The dhcp_client might not be valid after the
2587                          * callback call so just return immediately.
2588                          */
2589                         dhcp_client->solicitation_cb(dhcp_client,
2590                                         dhcp_client->solicitation_data);
2591                         return TRUE;
2592                 }
2593                 break;
2594         case REBIND:
2595                 if (dhcp_client->type != G_DHCP_IPV6)
2596                         return TRUE;
2597
2598                 server_id = dhcpv6_get_option(packet6, pkt_len,
2599                                 G_DHCPV6_SERVERID, &option_len, &count);
2600                 if (!dhcp_client->server_duid && server_id &&
2601                                                                 count == 1) {
2602                         /*
2603                          * If we do not have server duid yet, then get it now.
2604                          * Prefix delegation renew support needs it.
2605                          */
2606                         dhcp_client->server_duid = g_try_malloc(option_len);
2607                         if (!dhcp_client->server_duid)
2608                                 return TRUE;
2609                         memcpy(dhcp_client->server_duid, server_id, option_len);
2610                         dhcp_client->server_duid_len = option_len;
2611                 }
2612                 /* fall through */
2613         case INFORMATION_REQ:
2614         case REQUEST:
2615         case RENEW:
2616         case RELEASE:
2617         case CONFIRM:
2618         case DECLINE:
2619                 if (dhcp_client->type != G_DHCP_IPV6)
2620                         return TRUE;
2621
2622                 if (packet6->message != DHCPV6_REPLY)
2623                         return TRUE;
2624
2625                 count = 0;
2626                 option_len = 0;
2627                 server_id = dhcpv6_get_option(packet6, pkt_len,
2628                                 G_DHCPV6_SERVERID, &option_len, &count);
2629                 if (!server_id || count != 1 || option_len == 0 ||
2630                                 (dhcp_client->server_duid_len > 0 &&
2631                                 memcmp(dhcp_client->server_duid, server_id,
2632                                         dhcp_client->server_duid_len) != 0)) {
2633                         /* RFC 3315, 15.10 */
2634                         debug(dhcp_client,
2635                                 "server duid error, discarding msg %p/%d/%d",
2636                                 server_id, option_len, count);
2637                         return TRUE;
2638                 }
2639
2640                 switch_listening_mode(dhcp_client, L_NONE);
2641
2642                 get_dhcpv6_request(dhcp_client, packet6, pkt_len,
2643                                                 &dhcp_client->status_code);
2644
2645                 if (dhcp_client->information_req_cb) {
2646                         /*
2647                          * The dhcp_client might not be valid after the
2648                          * callback call so just return immediately.
2649                          */
2650                         dhcp_client->information_req_cb(dhcp_client,
2651                                         dhcp_client->information_req_data);
2652                         return TRUE;
2653                 }
2654                 if (dhcp_client->request_cb) {
2655                         dhcp_client->request_cb(dhcp_client,
2656                                         dhcp_client->request_data);
2657                         return TRUE;
2658                 }
2659                 if (dhcp_client->renew_cb) {
2660                         dhcp_client->renew_cb(dhcp_client,
2661                                         dhcp_client->renew_data);
2662                         return TRUE;
2663                 }
2664                 if (dhcp_client->rebind_cb) {
2665                         dhcp_client->rebind_cb(dhcp_client,
2666                                         dhcp_client->rebind_data);
2667                         return TRUE;
2668                 }
2669                 if (dhcp_client->release_cb) {
2670                         dhcp_client->release_cb(dhcp_client,
2671                                         dhcp_client->release_data);
2672                         return TRUE;
2673                 }
2674                 if (dhcp_client->decline_cb) {
2675                         dhcp_client->decline_cb(dhcp_client,
2676                                         dhcp_client->decline_data);
2677                         return TRUE;
2678                 }
2679                 if (dhcp_client->confirm_cb) {
2680                         count = 0;
2681                         server_id = dhcpv6_get_option(packet6, pkt_len,
2682                                                 G_DHCPV6_SERVERID, &option_len,
2683                                                 &count);
2684                         if (!server_id || count != 1 ||
2685                                                         option_len == 0) {
2686                                 /* RFC 3315, 15.10 */
2687                                 debug(dhcp_client,
2688                                         "confirm server duid error, "
2689                                         "discarding msg %p/%d/%d",
2690                                         server_id, option_len, count);
2691                                 return TRUE;
2692                         }
2693                         dhcp_client->server_duid = g_try_malloc(option_len);
2694                         if (!dhcp_client->server_duid)
2695                                 return TRUE;
2696                         memcpy(dhcp_client->server_duid, server_id, option_len);
2697                         dhcp_client->server_duid_len = option_len;
2698
2699                         dhcp_client->confirm_cb(dhcp_client,
2700                                                 dhcp_client->confirm_data);
2701                         return TRUE;
2702                 }
2703                 break;
2704         default:
2705                 break;
2706         }
2707
2708         debug(dhcp_client, "processed DHCP packet (new state %d)",
2709                                                         dhcp_client->state);
2710
2711         return TRUE;
2712 }
2713
2714 static gboolean discover_timeout(gpointer user_data)
2715 {
2716         GDHCPClient *dhcp_client = user_data;
2717
2718         dhcp_client->retry_times++;
2719
2720         /*
2721          * We do not send the REQUESTED IP option if we are retrying because
2722          * if the server is non-authoritative it will ignore the request if the
2723          * option is present.
2724          */
2725         g_dhcp_client_start(dhcp_client, NULL);
2726
2727         return FALSE;
2728 }
2729
2730 static gboolean reboot_timeout(gpointer user_data)
2731 {
2732         GDHCPClient *dhcp_client = user_data;
2733         dhcp_client->retry_times = 0;
2734         dhcp_client->requested_ip = 0;
2735         dhcp_client->state = INIT_SELECTING;
2736         /*
2737          * We do not send the REQUESTED IP option because the server didn't
2738          * respond when we send DHCPREQUEST with the REQUESTED IP option in
2739          * init-reboot state
2740          */
2741         g_dhcp_client_start(dhcp_client, NULL);
2742
2743         return FALSE;
2744 }
2745
2746 static gboolean ipv4ll_defend_timeout(gpointer dhcp_data)
2747 {
2748         GDHCPClient *dhcp_client = dhcp_data;
2749
2750         debug(dhcp_client, "back to MONITOR mode");
2751
2752         dhcp_client->conflicts = 0;
2753         dhcp_client->state = IPV4LL_MONITOR;
2754
2755         return FALSE;
2756 }
2757
2758 static gboolean ipv4ll_announce_timeout(gpointer dhcp_data)
2759 {
2760         GDHCPClient *dhcp_client = dhcp_data;
2761         uint32_t ip;
2762
2763 #if defined TIZEN_EXT
2764         if (!dhcp_client)
2765                 return FALSE;
2766 #endif
2767
2768         debug(dhcp_client, "request timeout (retries %d)",
2769                dhcp_client->retry_times);
2770
2771         if (dhcp_client->retry_times != ANNOUNCE_NUM) {
2772                 dhcp_client->retry_times++;
2773                 send_announce_packet(dhcp_client);
2774                 return FALSE;
2775         }
2776
2777         ip = htonl(dhcp_client->requested_ip);
2778         debug(dhcp_client, "switching to monitor mode");
2779         dhcp_client->state = IPV4LL_MONITOR;
2780         dhcp_client->assigned_ip = get_ip(ip);
2781
2782         if (dhcp_client->ipv4ll_available_cb)
2783                 dhcp_client->ipv4ll_available_cb(dhcp_client,
2784                                         dhcp_client->ipv4ll_available_data);
2785         dhcp_client->conflicts = 0;
2786         dhcp_client->timeout = 0;
2787
2788         return FALSE;
2789 }
2790
2791 static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
2792 {
2793
2794         GDHCPClient *dhcp_client = dhcp_data;
2795
2796         debug(dhcp_client, "IPV4LL probe timeout (retries %d)",
2797                dhcp_client->retry_times);
2798
2799         if (dhcp_client->retry_times == PROBE_NUM) {
2800                 dhcp_client->state = IPV4LL_ANNOUNCE;
2801                 dhcp_client->retry_times = 0;
2802
2803                 dhcp_client->retry_times++;
2804                 send_announce_packet(dhcp_client);
2805                 return FALSE;
2806         }
2807         dhcp_client->retry_times++;
2808         send_probe_packet(dhcp_client);
2809
2810         return FALSE;
2811 }
2812
2813 int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
2814 {
2815         int re;
2816         uint32_t addr;
2817         uint64_t rand;
2818         ClientState oldstate = dhcp_client->state;
2819
2820 #if defined TIZEN_EXT
2821         int discover_retry = 0;
2822         int timeout = 0;
2823 #endif
2824
2825         remove_timeouts(dhcp_client);
2826
2827         if (dhcp_client->type == G_DHCP_IPV6) {
2828                 if (dhcp_client->information_req_cb) {
2829                         dhcp_client->state = INFORMATION_REQ;
2830                         re = switch_listening_mode(dhcp_client, L3);
2831                         if (re != 0) {
2832                                 switch_listening_mode(dhcp_client, L_NONE);
2833                                 dhcp_client->state = 0;
2834                                 return re;
2835                         }
2836                         send_information_req(dhcp_client);
2837
2838                 } else if (dhcp_client->solicitation_cb) {
2839                         dhcp_client->state = SOLICITATION;
2840                         re = switch_listening_mode(dhcp_client, L3);
2841                         if (re != 0) {
2842                                 switch_listening_mode(dhcp_client, L_NONE);
2843                                 dhcp_client->state = 0;
2844                                 return re;
2845                         }
2846                         send_solicitation(dhcp_client);
2847
2848                 } else if (dhcp_client->request_cb) {
2849                         dhcp_client->state = REQUEST;
2850                         re = switch_listening_mode(dhcp_client, L3);
2851                         if (re != 0) {
2852                                 switch_listening_mode(dhcp_client, L_NONE);
2853                                 dhcp_client->state = 0;
2854                                 return re;
2855                         }
2856                         send_dhcpv6_request(dhcp_client);
2857
2858                 } else if (dhcp_client->confirm_cb) {
2859                         dhcp_client->state = CONFIRM;
2860                         re = switch_listening_mode(dhcp_client, L3);
2861                         if (re != 0) {
2862                                 switch_listening_mode(dhcp_client, L_NONE);
2863                                 dhcp_client->state = 0;
2864                                 return re;
2865                         }
2866                         send_dhcpv6_confirm(dhcp_client);
2867
2868                 } else if (dhcp_client->renew_cb) {
2869                         dhcp_client->state = RENEW;
2870                         re = switch_listening_mode(dhcp_client, L3);
2871                         if (re != 0) {
2872                                 switch_listening_mode(dhcp_client, L_NONE);
2873                                 dhcp_client->state = 0;
2874                                 return re;
2875                         }
2876                         send_dhcpv6_renew(dhcp_client);
2877
2878                 } else if (dhcp_client->rebind_cb) {
2879                         dhcp_client->state = REBIND;
2880                         re = switch_listening_mode(dhcp_client, L3);
2881                         if (re != 0) {
2882                                 switch_listening_mode(dhcp_client, L_NONE);
2883                                 dhcp_client->state = 0;
2884                                 return re;
2885                         }
2886                         send_dhcpv6_rebind(dhcp_client);
2887
2888                 } else if (dhcp_client->release_cb) {
2889                         dhcp_client->state = RENEW;
2890                         re = switch_listening_mode(dhcp_client, L3);
2891                         if (re != 0) {
2892                                 switch_listening_mode(dhcp_client, L_NONE);
2893                                 dhcp_client->state = 0;
2894                                 return re;
2895                         }
2896                         send_dhcpv6_release(dhcp_client);
2897                 } else if (dhcp_client->decline_cb) {
2898                         dhcp_client->state = DECLINE;
2899                         re = switch_listening_mode(dhcp_client, L3);
2900                         if (re != 0) {
2901                                 switch_listening_mode(dhcp_client, L_NONE);
2902                                 dhcp_client->state = 0;
2903                                 return re;
2904                         }
2905                         send_dhcpv6_decline(dhcp_client);
2906                 }
2907
2908                 return 0;
2909         }
2910
2911 #if defined TIZEN_EXT
2912         if (g_ascii_strncasecmp(dhcp_client->interface, "wlan", 4)
2913                         == 0) {
2914                 discover_retry = DISCOVER_RETRIES_WIFI;
2915                 timeout = DISCOVER_TIMEOUT_WIFI;
2916         } else {
2917                 discover_retry = DISCOVER_RETRIES;
2918                 timeout = DISCOVER_TIMEOUT;
2919         }
2920
2921         debug(dhcp_client, "[DHCPC] Discover retry/total : [%d]/[%d] timeout [%d]",
2922                         dhcp_client->retry_times, discover_retry, timeout);
2923 #endif
2924
2925
2926         if (dhcp_client->type == G_DHCP_IPV4LL) {
2927                 dhcp_client->state = INIT_SELECTING;
2928                 ipv4ll_start(dhcp_client);
2929                 return 0;
2930         }
2931
2932 #if defined TIZEN_EXT
2933         if (dhcp_client->retry_times == discover_retry) {
2934 #else
2935         if (dhcp_client->retry_times == DISCOVER_RETRIES) {
2936 #endif
2937                 if (dhcp_client->no_lease_cb)
2938                         dhcp_client->no_lease_cb(dhcp_client,
2939                                                 dhcp_client->no_lease_data);
2940                 dhcp_client->retry_times = 0;
2941                 return 0;
2942         }
2943
2944         if (dhcp_client->retry_times == 0) {
2945                 g_free(dhcp_client->assigned_ip);
2946                 dhcp_client->assigned_ip = NULL;
2947
2948                 dhcp_client->state = INIT_SELECTING;
2949                 re = switch_listening_mode(dhcp_client, L2);
2950                 if (re != 0)
2951                         return re;
2952
2953                 __connman_util_get_random(&rand);
2954                 dhcp_client->xid = rand;
2955                 dhcp_client->start = time(NULL);
2956                 g_hash_table_remove_all(dhcp_client->secs_bcast_hash);
2957         }
2958
2959         if (!last_address || oldstate == DECLINED) {
2960                 addr = 0;
2961         } else {
2962                 addr = ntohl(inet_addr(last_address));
2963                 if (addr == 0xFFFFFFFF || ((addr & LINKLOCAL_ADDR) ==
2964                                         LINKLOCAL_ADDR)) {
2965                         addr = 0;
2966                 } else if (dhcp_client->last_address != last_address) {
2967                         g_free(dhcp_client->last_address);
2968                         dhcp_client->last_address = g_strdup(last_address);
2969                 }
2970         }
2971
2972         if ((addr != 0) && (dhcp_client->type != G_DHCP_IPV4LL)) {
2973                 debug(dhcp_client, "DHCP client start with state init_reboot");
2974                 dhcp_client->requested_ip = addr;
2975                 dhcp_client->state = REBOOTING;
2976                 send_request(dhcp_client);
2977
2978                 dhcp_client->timeout = g_timeout_add_seconds_full(
2979                                                                 G_PRIORITY_HIGH,
2980 #if defined TIZEN_EXT
2981                                                                 timeout,
2982 #else
2983                                                                 REQUEST_TIMEOUT,
2984 #endif
2985                                                                 reboot_timeout,
2986                                                                 dhcp_client,
2987                                                                 NULL);
2988                 return 0;
2989         }
2990         send_discover(dhcp_client, addr);
2991
2992         dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
2993 #if defined TIZEN_EXT
2994                                                         timeout,
2995 #else
2996                                                         DISCOVER_TIMEOUT,
2997 #endif
2998                                                         discover_timeout,
2999                                                         dhcp_client,
3000                                                         NULL);
3001         return 0;
3002 }
3003
3004 void g_dhcp_client_stop(GDHCPClient *dhcp_client)
3005 {
3006         switch_listening_mode(dhcp_client, L_NONE);
3007
3008         if (dhcp_client->state == BOUND ||
3009                         dhcp_client->state == RENEWING ||
3010                                 dhcp_client->state == REBINDING)
3011                 send_release(dhcp_client, dhcp_client->server_ip,
3012                                         dhcp_client->requested_ip);
3013
3014         remove_timeouts(dhcp_client);
3015
3016         if (dhcp_client->listener_watch > 0) {
3017                 g_source_remove(dhcp_client->listener_watch);
3018                 dhcp_client->listener_watch = 0;
3019         }
3020
3021         dhcp_client->retry_times = 0;
3022         dhcp_client->ack_retry_times = 0;
3023
3024         dhcp_client->requested_ip = 0;
3025         dhcp_client->state = RELEASED;
3026         dhcp_client->lease_seconds = 0;
3027         dhcp_client->request_bcast = false;
3028 }
3029
3030 GList *g_dhcp_client_get_option(GDHCPClient *dhcp_client,
3031                                         unsigned char option_code)
3032 {
3033         return g_hash_table_lookup(dhcp_client->code_value_hash,
3034                                         GINT_TO_POINTER((int) option_code));
3035 }
3036
3037 void g_dhcp_client_register_event(GDHCPClient *dhcp_client,
3038                                         GDHCPClientEvent event,
3039                                         GDHCPClientEventFunc func,
3040                                                         gpointer data)
3041 {
3042         switch (event) {
3043         case G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE:
3044                 dhcp_client->lease_available_cb = func;
3045                 dhcp_client->lease_available_data = data;
3046                 return;
3047         case G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE:
3048                 if (dhcp_client->type == G_DHCP_IPV6)
3049                         return;
3050                 dhcp_client->ipv4ll_available_cb = func;
3051                 dhcp_client->ipv4ll_available_data = data;
3052                 return;
3053         case G_DHCP_CLIENT_EVENT_NO_LEASE:
3054                 dhcp_client->no_lease_cb = func;
3055                 dhcp_client->no_lease_data = data;
3056                 return;
3057         case G_DHCP_CLIENT_EVENT_LEASE_LOST:
3058                 dhcp_client->lease_lost_cb = func;
3059                 dhcp_client->lease_lost_data = data;
3060                 return;
3061         case G_DHCP_CLIENT_EVENT_IPV4LL_LOST:
3062                 if (dhcp_client->type == G_DHCP_IPV6)
3063                         return;
3064                 dhcp_client->ipv4ll_lost_cb = func;
3065                 dhcp_client->ipv4ll_lost_data = data;
3066                 return;
3067         case G_DHCP_CLIENT_EVENT_ADDRESS_CONFLICT:
3068                 dhcp_client->address_conflict_cb = func;
3069                 dhcp_client->address_conflict_data = data;
3070                 return;
3071         case G_DHCP_CLIENT_EVENT_INFORMATION_REQ:
3072                 if (dhcp_client->type != G_DHCP_IPV6)
3073                         return;
3074                 dhcp_client->information_req_cb = func;
3075                 dhcp_client->information_req_data = data;
3076                 return;
3077         case G_DHCP_CLIENT_EVENT_SOLICITATION:
3078                 if (dhcp_client->type != G_DHCP_IPV6)
3079                         return;
3080                 dhcp_client->solicitation_cb = func;
3081                 dhcp_client->solicitation_data = data;
3082                 return;
3083         case G_DHCP_CLIENT_EVENT_ADVERTISE:
3084                 if (dhcp_client->type != G_DHCP_IPV6)
3085                         return;
3086                 dhcp_client->advertise_cb = func;
3087                 dhcp_client->advertise_data = data;
3088                 return;
3089         case G_DHCP_CLIENT_EVENT_REQUEST:
3090                 if (dhcp_client->type != G_DHCP_IPV6)
3091                         return;
3092                 dhcp_client->request_cb = func;
3093                 dhcp_client->request_data = data;
3094                 return;
3095         case G_DHCP_CLIENT_EVENT_RENEW:
3096                 if (dhcp_client->type != G_DHCP_IPV6)
3097                         return;
3098                 dhcp_client->renew_cb = func;
3099                 dhcp_client->renew_data = data;
3100                 return;
3101         case G_DHCP_CLIENT_EVENT_REBIND:
3102                 if (dhcp_client->type != G_DHCP_IPV6)
3103                         return;
3104                 dhcp_client->rebind_cb = func;
3105                 dhcp_client->rebind_data = data;
3106                 return;
3107         case G_DHCP_CLIENT_EVENT_RELEASE:
3108                 if (dhcp_client->type != G_DHCP_IPV6)
3109                         return;
3110                 dhcp_client->release_cb = func;
3111                 dhcp_client->release_data = data;
3112                 return;
3113         case G_DHCP_CLIENT_EVENT_CONFIRM:
3114                 if (dhcp_client->type != G_DHCP_IPV6)
3115                         return;
3116                 dhcp_client->confirm_cb = func;
3117                 dhcp_client->confirm_data = data;
3118                 return;
3119         case G_DHCP_CLIENT_EVENT_DECLINE:
3120                 if (dhcp_client->type != G_DHCP_IPV6)
3121                         return;
3122                 dhcp_client->decline_cb = func;
3123                 dhcp_client->decline_data = data;
3124                 return;
3125         }
3126 }
3127
3128 int g_dhcp_client_get_index(GDHCPClient *dhcp_client)
3129 {
3130         return dhcp_client->ifindex;
3131 }
3132
3133 char *g_dhcp_client_get_server_address(GDHCPClient *dhcp_client)
3134 {
3135         if (!dhcp_client)
3136                 return NULL;
3137
3138 #if defined TIZEN_EXT
3139         return get_ip(htonl(dhcp_client->server_ip));
3140 #else
3141         return get_ip(dhcp_client->server_ip);
3142 #endif
3143 }
3144
3145 #if defined TIZEN_EXT
3146 int g_dhcp_client_get_dhcp_lease_duration(GDHCPClient *dhcp_client)
3147 {
3148         return dhcp_client->dhcp_lease_seconds;
3149 }
3150 #endif
3151
3152 char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
3153 {
3154         return g_strdup(dhcp_client->assigned_ip);
3155 }
3156
3157 char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
3158 {
3159         GList *option = NULL;
3160
3161         if (dhcp_client->type == G_DHCP_IPV6)
3162                 return NULL;
3163
3164         switch (dhcp_client->state) {
3165         case IPV4LL_DEFEND:
3166         case IPV4LL_MONITOR:
3167                 return g_strdup("255.255.0.0");
3168         case BOUND:
3169         case RENEWING:
3170         case REBINDING:
3171                 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
3172                 if (option)
3173                         return g_strdup(option->data);
3174         case INIT_SELECTING:
3175         case REBOOTING:
3176         case REQUESTING:
3177         case RELEASED:
3178         case DECLINED:
3179         case IPV4LL_PROBE:
3180         case IPV4LL_ANNOUNCE:
3181         case INFORMATION_REQ:
3182         case SOLICITATION:
3183         case REQUEST:
3184         case CONFIRM:
3185         case RENEW:
3186         case REBIND:
3187         case RELEASE:
3188         case DECLINE:
3189                 break;
3190         }
3191         return NULL;
3192 }
3193
3194 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
3195                                                 unsigned int option_code)
3196 {
3197         if (!g_list_find(dhcp_client->request_list,
3198                                 GINT_TO_POINTER((int)option_code)))
3199                 dhcp_client->request_list = g_list_prepend(
3200                                         dhcp_client->request_list,
3201                                         (GINT_TO_POINTER((int) option_code)));
3202
3203         return G_DHCP_CLIENT_ERROR_NONE;
3204 }
3205
3206 void g_dhcp_client_clear_requests(GDHCPClient *dhcp_client)
3207 {
3208         g_list_free(dhcp_client->request_list);
3209         dhcp_client->request_list = NULL;
3210 }
3211
3212 void g_dhcp_client_clear_values(GDHCPClient *dhcp_client)
3213 {
3214         g_hash_table_remove_all(dhcp_client->send_value_hash);
3215 }
3216
3217 static uint8_t *alloc_dhcp_option(int code, const uint8_t *data, unsigned size)
3218 {
3219         uint8_t *storage;
3220
3221         storage = g_try_malloc(size + OPT_DATA);
3222         if (!storage)
3223                 return NULL;
3224
3225         storage[OPT_CODE] = code;
3226         storage[OPT_LEN] = size;
3227         memcpy(&storage[OPT_DATA], data, size);
3228
3229         return storage;
3230 }
3231
3232 static uint8_t *alloc_dhcp_data_option(int code, const uint8_t *data,
3233                                         unsigned size)
3234 {
3235         return alloc_dhcp_option(code, data, MIN(size, 255));
3236 }
3237
3238 static uint8_t *alloc_dhcp_string_option(int code, const char *str)
3239 {
3240         return alloc_dhcp_data_option(code, (const uint8_t *)str, strlen(str));
3241 }
3242
3243 GDHCPClientError g_dhcp_client_set_id(GDHCPClient *dhcp_client)
3244 {
3245         const unsigned maclen = 6;
3246         const unsigned idlen = maclen + 1;
3247         const uint8_t option_code = G_DHCP_CLIENT_ID;
3248         uint8_t idbuf[idlen];
3249         uint8_t *data_option;
3250
3251         idbuf[0] = ARPHRD_ETHER;
3252
3253         memcpy(&idbuf[1], dhcp_client->mac_address, maclen);
3254
3255         data_option = alloc_dhcp_data_option(option_code, idbuf, idlen);
3256         if (!data_option)
3257                 return G_DHCP_CLIENT_ERROR_NOMEM;
3258
3259         g_hash_table_insert(dhcp_client->send_value_hash,
3260                 GINT_TO_POINTER((int) option_code), data_option);
3261
3262         return G_DHCP_CLIENT_ERROR_NONE;
3263 }
3264
3265 /* Now only support send hostname and vendor class ID */
3266 GDHCPClientError g_dhcp_client_set_send(GDHCPClient *dhcp_client,
3267                 unsigned char option_code, const char *option_value)
3268 {
3269         uint8_t *binary_option;
3270
3271         if ((option_code == G_DHCP_HOST_NAME ||
3272                 option_code == G_DHCP_VENDOR_CLASS_ID) && option_value) {
3273                 binary_option = alloc_dhcp_string_option(option_code,
3274                                                         option_value);
3275                 if (!binary_option)
3276                         return G_DHCP_CLIENT_ERROR_NOMEM;
3277
3278                 g_hash_table_insert(dhcp_client->send_value_hash,
3279                         GINT_TO_POINTER((int) option_code), binary_option);
3280         }
3281
3282         return G_DHCP_CLIENT_ERROR_NONE;
3283 }
3284
3285 static uint8_t *alloc_dhcpv6_option(uint16_t code, uint8_t *option,
3286                                 uint16_t len)
3287 {
3288         uint8_t *storage;
3289
3290         storage = g_malloc(2 + 2 + len);
3291         if (!storage)
3292                 return NULL;
3293
3294         storage[0] = code >> 8;
3295         storage[1] = code & 0xff;
3296         storage[2] = len >> 8;
3297         storage[3] = len & 0xff;
3298         memcpy(storage + 2 + 2, option, len);
3299
3300         return storage;
3301 }
3302
3303 gboolean g_dhcpv6_client_clear_send(GDHCPClient *dhcp_client, uint16_t code)
3304 {
3305         return g_hash_table_remove(dhcp_client->send_value_hash,
3306                                 GINT_TO_POINTER((int)code));
3307 }
3308
3309 void g_dhcpv6_client_set_send(GDHCPClient *dhcp_client,
3310                                         uint16_t option_code,
3311                                         uint8_t *option_value,
3312                                         uint16_t option_len)
3313 {
3314         if (option_value) {
3315                 uint8_t *binary_option;
3316
3317                 debug(dhcp_client, "setting option %d to %p len %d",
3318                         option_code, option_value, option_len);
3319
3320                 binary_option = alloc_dhcpv6_option(option_code, option_value,
3321                                                 option_len);
3322                 if (binary_option)
3323                         g_hash_table_insert(dhcp_client->send_value_hash,
3324                                         GINT_TO_POINTER((int) option_code),
3325                                         binary_option);
3326         }
3327 }
3328
3329 void g_dhcpv6_client_reset_request(GDHCPClient *dhcp_client)
3330 {
3331         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3332                 return;
3333
3334         dhcp_client->last_request = time(NULL);
3335 }
3336
3337 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)
3338 {
3339         if (!dhcp_client || dhcp_client->type != G_DHCP_IPV6)
3340                 return 0;
3341
3342         return dhcp_client->status_code;
3343 }
3344
3345 GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
3346 {
3347         if (!dhcp_client)
3348                 return NULL;
3349
3350         __sync_fetch_and_add(&dhcp_client->ref_count, 1);
3351
3352         return dhcp_client;
3353 }
3354
3355 void g_dhcp_client_unref(GDHCPClient *dhcp_client)
3356 {
3357         if (!dhcp_client)
3358                 return;
3359
3360         if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
3361                 return;
3362
3363         g_dhcp_client_stop(dhcp_client);
3364
3365         g_free(dhcp_client->interface);
3366         g_free(dhcp_client->assigned_ip);
3367         g_free(dhcp_client->last_address);
3368         g_free(dhcp_client->duid);
3369         g_free(dhcp_client->server_duid);
3370
3371         g_list_free(dhcp_client->request_list);
3372         g_list_free(dhcp_client->require_list);
3373
3374         g_hash_table_destroy(dhcp_client->code_value_hash);
3375         g_hash_table_destroy(dhcp_client->send_value_hash);
3376         g_hash_table_destroy(dhcp_client->secs_bcast_hash);
3377
3378         g_free(dhcp_client);
3379 #if defined TIZEN_EXT
3380         dhcp_client = NULL;
3381 #endif
3382 }
3383
3384 void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
3385                                 GDHCPDebugFunc func, gpointer user_data)
3386 {
3387         if (!dhcp_client)
3388                 return;
3389
3390         dhcp_client->debug_func = func;
3391         dhcp_client->debug_data = user_data;
3392 }
3393
3394 static GDHCPIAPrefix *copy_prefix(gpointer data)
3395 {
3396         GDHCPIAPrefix *copy, *prefix = data;
3397
3398         copy = g_try_new(GDHCPIAPrefix, 1);
3399         if (!copy)
3400                 return NULL;
3401
3402         memcpy(copy, prefix, sizeof(GDHCPIAPrefix));
3403
3404         return copy;
3405 }
3406
3407 GSList *g_dhcpv6_copy_prefixes(GSList *prefixes)
3408 {
3409         GSList *copy = NULL;
3410         GSList *list;
3411
3412         for (list = prefixes; list; list = list->next)
3413                 copy = g_slist_prepend(copy, copy_prefix(list->data));
3414
3415         return copy;
3416 }
3417
3418 #if defined TIZEN_EXT
3419 void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
3420 {
3421         /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
3422          * 4.4.3 Initialization with known network address
3423          * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
3424          */
3425         debug(dhcp_client, "known network address (%d)", known);
3426
3427         if (dhcp_client->init_reboot == known)
3428                 return;
3429
3430         dhcp_client->init_reboot = known;
3431 }
3432 #endif