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