Merge "Fixed memory leak in SetField method" into tizen
[platform/core/connectivity/net-config.git] / src / ip-conflict-detect.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <net/if.h>
22 #include <linux/if_packet.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <arpa/inet.h>
26 #include <netinet/ether.h>
27 #include <net/ethernet.h>
28 #include <netinet/in.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <errno.h>
32 #include <glib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <stdbool.h>
36
37 #include "ip-conflict-detect.h"
38 #include "network-state.h"
39 #include "log.h"
40 #include "neterror.h"
41
42 #define ARP_PACKET_SIZE 60
43 #define MAX_SIZE_ERROR_BUFFER 256
44 #define IP_ADDRESS_LENGTH 4
45 #define MAC_ADDRESS_LENGTH 6
46 #define WLAN_MAC_ADDR_MAX 20
47 #define ARP_SOURCE_IP "0.0.0.0"
48
49 #define MIN_ARP_SEND_TIME 2000
50 #define MAX_ARP_SEND_TIME 32000
51 #define GRATUITOUS_ARP_MAC_ADDR "00:00:00:00:00:00"
52 #define UCHAR_TO_ADDRESS(hwaddr, buf) do {\
53                 snprintf(buf, WLAN_MAC_ADDR_MAX,\
54                 "%02X:%02X:%02X:%02X:%02X:%02X", hwaddr[0], hwaddr[1],\
55                 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);\
56                 } while (0)
57
58 struct arp_message {
59         /* Ethernet header */
60         unsigned char   h_dest[MAC_ADDRESS_LENGTH];     /* destination ether addr */
61         unsigned char   h_source[MAC_ADDRESS_LENGTH];   /* source ether addr */
62         unsigned short  h_proto;                                /* packet type ID field */
63
64         /* ARP packet */
65         unsigned short hw_type;                         /* hardware type(ARPHRD_ETHER) */
66         unsigned short p_type;                          /* protocol type(ETH_P_IP) */
67         unsigned char  hw_len;                          /* hardware address length */
68         unsigned char  p_len;                                   /* protocol address length */
69         unsigned short operation;                               /* ARP opcode */
70         unsigned char  s_hwaddr[MAC_ADDRESS_LENGTH];            /* sender hardware address */
71         unsigned char  s_IPaddr[IP_ADDRESS_LENGTH];             /* sender IP address */
72         unsigned char  t_hwaddr[MAC_ADDRESS_LENGTH];            /* target hardware address */
73         unsigned char  t_IPaddr[IP_ADDRESS_LENGTH];             /* target IP address */
74         unsigned char  pad[18];                         /* pad for min. Ethernet payload (60 bytes) */
75 };
76
77 typedef enum {
78         NETCONFIG_IP_CONFLICT_STATE_UNKNOWN,
79         NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED,
80         NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED
81 } ip_conflict_state_e;
82
83 struct timer_data {
84         guint initial_time;
85         guint timeout;
86 };
87 static struct timer_data td = {
88         MIN_ARP_SEND_TIME, MIN_ARP_SEND_TIME
89 };
90
91 int ioctl_sock;
92 bool is_ip_conflict_detect_enabled = true;
93 static gboolean send_arp(gpointer data);
94 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac);
95 ip_conflict_state_e conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
96
97 struct sock_data *sd;
98
99 typedef unsigned int in_addr_t;
100
101 union uchar_to_uint {
102         unsigned int uint;
103         unsigned char uchar[IP_ADDRESS_LENGTH];
104 };
105
106 static unsigned int __convert_uchar_to_uint(unsigned char b[IP_ADDRESS_LENGTH])
107 {
108         int idx = 0;
109         union uchar_to_uint u;
110         for (; idx < IP_ADDRESS_LENGTH; ++idx)
111                 u.uchar[idx] = b[idx];
112
113         return u.uint;
114 }
115
116 static gboolean __arp_reply_timeout_cb(gpointer data)
117 {
118         if (sd == NULL) {
119                 INFO("Ignore timeout cb");
120                 return G_SOURCE_REMOVE;
121         }
122
123         sd->iteration++;
124         sd->arp_reply_timer = -1;
125         if (sd->timer_id != -1)
126                 g_source_remove(sd->timer_id);
127
128         if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED && sd->iteration == 5) {
129                 sd->iteration = 0;
130                 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
131                 __netconfig_wifi_notify_ip_conflict("resolved", GRATUITOUS_ARP_MAC_ADDR);
132         }
133
134         sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
135         return G_SOURCE_REMOVE;
136 }
137
138 static gboolean __netconfig_check_arp_receive(GIOChannel *source,
139                                                   GIOCondition condition, gpointer data)
140 {
141         struct sock_data *sd = data;
142         gchar buffer[ARP_PACKET_SIZE] = {0, };
143         gsize bytes_read = 0;
144         struct arp_message arp_recv;
145         char sbuf[WLAN_MAC_ADDR_MAX];
146         char tbuf[WLAN_MAC_ADDR_MAX];
147         const char *default_ip = NULL;
148
149         if (g_io_channel_read_chars(source, buffer, ARP_PACKET_SIZE,
150                                 &bytes_read, NULL) == G_IO_STATUS_NORMAL) {
151                 unsigned int target_ip = 0;
152
153                 memset(&arp_recv, 0, sizeof(arp_recv));
154                 memcpy(&arp_recv, buffer, sizeof(buffer));
155
156                 default_ip = netconfig_get_default_ipaddress();
157                 if (default_ip == NULL) {
158                         INFO("ip address is not set yet");
159                         goto out;
160                 }
161                 target_ip = inet_addr(default_ip);
162
163
164                 /* Only handle ARP replies */
165                 if (arp_recv.operation != htons(ARPOP_REPLY))
166                         goto out;
167
168                 UCHAR_TO_ADDRESS(arp_recv.t_hwaddr, tbuf);
169                 UCHAR_TO_ADDRESS(arp_recv.s_hwaddr, sbuf);
170
171                 int zero_mac = strcmp(tbuf , GRATUITOUS_ARP_MAC_ADDR);
172                 if (zero_mac == 0) {
173                         DBG("Broadcast packet.\n");
174                         goto skip;
175                 }
176                 DBG("our mac= %s source mac= %s target mac= %s", netconfig_get_default_mac_address(), sbuf, tbuf);
177                 int mac_cmp = strcmp(tbuf , netconfig_get_default_mac_address());
178                 if (mac_cmp != 0) {
179                         INFO("Packet not intended to us.\n");
180                         goto out;
181                 }
182 skip:
183                 mac_cmp = strcmp(sbuf, netconfig_get_default_mac_address());
184                 DBG("target ip = %d source ip = %d", target_ip, __convert_uchar_to_uint(arp_recv.s_IPaddr));
185                 if ((mac_cmp != 0) && (__convert_uchar_to_uint(arp_recv.s_IPaddr) == target_ip)) {
186                         sd->iteration = 0;
187                         if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED) {
188                                 INFO("ip conflict is detected !\n");
189                                 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED;
190                                 __netconfig_wifi_notify_ip_conflict("conflict", sbuf);
191                         }
192
193                         if (sd->arp_reply_timer != -1) {
194                                 g_source_remove(sd->arp_reply_timer);
195                                 sd->arp_reply_timer = -1;
196                         }
197
198                         if (sd->timer_id)
199                                 g_source_remove(sd->timer_id);
200                         sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
201
202                 }
203         }
204
205 out:
206         return TRUE;
207 }
208
209 static gboolean send_arp(gpointer data)
210 {
211         struct sock_data *sd = data;
212         struct ether_addr *source_mac = NULL;
213         struct arp_message arp;
214         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
215         unsigned int source_ip = 0;
216         unsigned int target_ip = 0;
217         const unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
218         struct sockaddr_ll addr = {0};
219         struct ifreq net_ifr;
220         int ifindex = 0;
221         errno = 0;
222         const char *default_ip = NULL;
223
224         const char *mac = netconfig_get_default_mac_address();
225         if (mac == NULL)
226                 goto err;
227         source_mac = ether_aton(mac);
228         if (source_mac == NULL) {
229                 INFO("Mac address is NULL");
230                 goto err;
231         }
232
233         memset(&arp, 0, sizeof(arp));
234
235         unsigned char  broadcast_mac_addr[MAC_ADDRESS_LENGTH];
236         memset(broadcast_mac_addr, 0xff, sizeof(broadcast_mac_addr));
237         memcpy(arp.h_dest, broadcast_mac_addr, MAC_ADDRESS_LENGTH);             /* MAC dest */
238         memcpy(arp.h_source, source_mac, MAC_ADDRESS_LENGTH);                   /* MAC source */
239
240         arp.h_proto = htons(ETH_P_ARP);                                         /* protocol type (Ethernet) */
241         arp.hw_type = htons(ARPHRD_ETHER);                                      /* hardware type */
242         arp.p_type = htons(ETH_P_IP);                                           /* protocol type (ARP message) */
243         arp.hw_len = MAC_ADDRESS_LENGTH;                                        /* hardware address length */
244         arp.p_len = IP_ADDRESS_LENGTH;                                          /* protocol address length */
245         arp.operation = htons(ARPOP_REQUEST);                                   /* ARP op code */
246         default_ip = netconfig_get_default_ipaddress();
247         if (default_ip == NULL) {
248                 INFO("ip address is not set yet");
249                 goto err;
250         }
251
252         source_ip = inet_addr(ARP_SOURCE_IP);
253         target_ip = inet_addr(default_ip);
254         memcpy(arp.s_IPaddr, &source_ip, IP_ADDRESS_LENGTH);                    /* source IP address */
255         memcpy(arp.s_hwaddr, source_mac, MAC_ADDRESS_LENGTH);                   /* source hardware address */
256         memcpy(arp.t_IPaddr, &target_ip, IP_ADDRESS_LENGTH);                    /* target IP addressshek" */
257
258         memset(&net_ifr, 0, sizeof(net_ifr));
259         /* ifreq structure creation */
260         size_t if_name_len = strlen(netconfig_get_default_ifname());
261         if (if_name_len < sizeof(net_ifr.ifr_name)) {
262                 memcpy(net_ifr.ifr_name, netconfig_get_default_ifname(), if_name_len);
263                 net_ifr.ifr_name[if_name_len] = 0;
264         } else {
265                 INFO("Error : Interface name is too long");
266                 goto err;
267         }
268
269         if (ioctl(sd->chk_conflict_sd, SIOCGIFINDEX, &net_ifr) == -1) {
270                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
271                 INFO("ioctl Failed. Error..... = %s\n", error_buf);
272                 goto err;
273         }
274
275         ifindex = net_ifr.ifr_ifindex;
276         /* Construct the destination address */
277         addr.sll_family = AF_PACKET;
278         addr.sll_ifindex = ifindex;
279         addr.sll_halen = ETHER_ADDR_LEN;
280         addr.sll_protocol = htons(ETH_P_ARP);
281         memcpy(addr.sll_addr, broadcast_addr, ETHER_ADDR_LEN);
282
283         if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
284                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
285                 INFO("Sending ARP Packet Failed. Error. = %s\n", error_buf);
286                 /* close socket */
287                 if (-1 < sd->chk_conflict_sd) {
288                         close(sd->chk_conflict_sd);
289                         sd->chk_conflict_sd = -1;
290                 }
291
292                 /* reopen socket */
293                 if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
294                         INFO("socket %d", sd->chk_conflict_sd);
295                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
296                         INFO("socket Failed. Error = %s\n", error_buf);
297                 }
298                 goto err;
299         } else {
300                 DBG("Sent ARP Packet \n");
301         }
302
303         g_source_remove(sd->timer_id);
304
305         sd->timeout = td.initial_time;
306
307         /* Adding timeout callback for arp request */
308         sd->arp_reply_timer = g_timeout_add(1000, __arp_reply_timeout_cb,
309                                         (gpointer) &sd->arp_reply_timer);
310         return FALSE;
311 err:
312         if (sd->timer_id)
313                 g_source_remove(sd->timer_id);
314         sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
315         return FALSE;
316 }
317
318 struct sock_data * start_ip_conflict_mon(void)
319 {
320         if (is_ip_conflict_detect_enabled == false) {
321                 INFO("detection mode is set to false");
322                 return NULL;
323         }
324
325         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
326
327         sd = g_try_malloc0(sizeof(struct sock_data));
328         if (sd == NULL) {
329                 INFO("Failed to malloc sock_data");
330                 return NULL;
331         }
332         sd->chk_conflict_data_id = -1;
333         sd->chk_conflict_sd = -1;
334         sd->timer_id = 0;
335         sd->iteration = 0;
336
337         if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
338                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
339                 INFO("socket Failed. Error = %s\n", error_buf);
340                 g_free(sd);
341                 return NULL;
342         } else {
343                 sd->chk_conflict_sock_io = g_io_channel_unix_new(sd->chk_conflict_sd);
344                 if (sd->chk_conflict_sock_io == NULL) {
345                         INFO("Failed to create channel");
346                         INFO("Exit");
347                         g_free(sd);
348                         return NULL;
349                 }
350
351                 g_io_channel_set_close_on_unref(sd->chk_conflict_sock_io, TRUE);
352
353                 if (G_IO_STATUS_NORMAL != g_io_channel_set_encoding(sd->chk_conflict_sock_io,
354                                                                    NULL, NULL))
355                         INFO("Failed to set encoding NULL on io channel");
356                 if (G_IO_STATUS_NORMAL != g_io_channel_set_flags(sd->chk_conflict_sock_io,
357                                                                 G_IO_FLAG_NONBLOCK, NULL))
358                         INFO("Failed to set flags on io channel");
359                 sd->chk_conflict_data_id = g_io_add_watch(sd->chk_conflict_sock_io, G_IO_IN,
360                                                           __netconfig_check_arp_receive, sd);
361                 DBG("socket %d", sd->chk_conflict_sd);
362
363                 sd->timeout = td.initial_time;
364                 send_arp(sd);
365                 return sd;
366         }
367 }
368
369 void stop_ip_conflict_mon()
370 {
371         INFO("+");
372         GError* error = NULL;
373         if (sd == NULL) {
374                 INFO("sd is NULL");
375                 return;
376         }
377         if (-1 < sd->chk_conflict_sd) {
378                 if (G_IO_STATUS_NORMAL !=
379                     g_io_channel_shutdown(sd->chk_conflict_sock_io, FALSE,
380                                           &error)) {
381                         INFO("Failure received while shutdown io channel[%d]:[%s]", error->code, error->message);
382                         g_error_free(error);
383                 }
384                 g_io_channel_unref(sd->chk_conflict_sock_io);
385                 g_source_remove(sd->chk_conflict_data_id);
386                 sd->chk_conflict_data_id = -1;
387                 close(sd->chk_conflict_sd);
388                 sd->chk_conflict_sd = -1;
389         }
390         if (sd->timer_id > 0) {
391                 g_source_remove(sd->timer_id);
392                 sd->timer_id = 0;
393         }
394         g_free(sd);
395         sd = NULL;
396         INFO("Monitoring stopped");
397 }
398
399 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
400 {
401         GVariantBuilder *builder = NULL;
402
403         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
404         g_variant_builder_add(builder, "{sv}", "state", g_variant_new_string(state));
405         g_variant_builder_add(builder, "{sv}", "mac", g_variant_new_string(mac));
406
407         wifi_emit_ip_conflict_event((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
408         g_variant_builder_unref(builder);
409
410         return;
411 }
412
413 gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *context,
414                                        bool detect)
415 {
416         g_return_val_if_fail(wifi != NULL, FALSE);
417
418
419         if (detect == false) {
420                 is_ip_conflict_detect_enabled = false;
421                 conflict_state = NETCONFIG_IP_CONFLICT_STATE_UNKNOWN;
422                 if (sd != NULL)
423                         stop_ip_conflict_mon();
424                 else {
425                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
426                         wifi_complete_ip_conflict_set_enable(wifi, context);
427                         return FALSE;
428                 }
429         } else {
430                 is_ip_conflict_detect_enabled = true;
431                 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
432                 if (sd == NULL) {
433                         if (start_ip_conflict_mon() == NULL) {
434                                 INFO("Failed to start IP conflict monitoring");
435                                 netconfig_error_dbus_method_return(context,
436                                                 NETCONFIG_ERROR_INTERNAL, "Failed");
437                                 wifi_complete_ip_conflict_set_enable(wifi, context);
438                                 return FALSE;
439                         }
440                 } else {
441                         netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
442                         wifi_complete_ip_conflict_set_enable(wifi, context);
443                         return FALSE;
444                 }
445         }
446
447         wifi_complete_ip_conflict_set_enable(wifi, context);
448         return TRUE;
449 }
450
451 gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation *context)
452 {
453         g_return_val_if_fail(wifi != NULL, FALSE);
454         GVariant *param = NULL;
455         param = g_variant_new("(b)", is_ip_conflict_detect_enabled);
456         g_dbus_method_invocation_return_value(context, param);
457         return TRUE;
458 }
459
460 gboolean handle_set_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context, guint initial_time)
461 {
462         g_return_val_if_fail(wifi != NULL, FALSE);
463         if (initial_time < MAX_ARP_SEND_TIME && initial_time > MIN_ARP_SEND_TIME)
464                 return FALSE;
465
466         td.initial_time = 1000 * initial_time;
467         // remove timer
468         stop_ip_conflict_mon();
469         start_ip_conflict_mon();
470         wifi_complete_set_ip_conflict_period(wifi, context);
471         return TRUE;
472 }
473
474 gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context)
475 {
476         g_return_val_if_fail(wifi != NULL, FALSE);
477         GVariant *param = NULL;
478         param = g_variant_new("(u)", conflict_state);
479         g_dbus_method_invocation_return_value(context, param);
480         return TRUE;
481 }
482
483 gboolean handle_get_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context)
484 {
485         g_return_val_if_fail(wifi != NULL, FALSE);
486         GVariant *param = NULL;
487         param = g_variant_new("(u)", td.initial_time/1000);
488         g_dbus_method_invocation_return_value(context, param);
489         return TRUE;
490 }