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