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