2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
37 #include "ip-conflict-detect.h"
38 #include "network-state.h"
43 #define ARP_PACKET_SIZE 60
44 #define MAX_SIZE_ERROR_BUFFER 256
45 #define IP_ADDRESS_LENGTH 4
46 #define MAC_ADDRESS_LENGTH 6
47 #define WLAN_MAC_ADDR_MAX 20
48 #define ARP_SOURCE_IP "0.0.0.0"
49 #define INITIAL_BURST_ARP_COUNT 5
51 #define CONFLICT_REMOVE_ITERATION_LIMIT 4
52 #define MIN_ARP_SEND_TIME 20000
53 #define BURST_ARP_SEND_TIME 3000
54 #define MAX_ARP_SEND_TIME 32000
55 #define GRATUITOUS_ARP_MAC_ADDR "00:00:00:00:00:00"
56 #define UCHAR_TO_ADDRESS(hwaddr, buf) do {\
57 snprintf(buf, WLAN_MAC_ADDR_MAX,\
58 "%02X:%02X:%02X:%02X:%02X:%02X", hwaddr[0], hwaddr[1],\
59 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);\
64 unsigned char h_dest[MAC_ADDRESS_LENGTH]; /* destination ether addr */
65 unsigned char h_source[MAC_ADDRESS_LENGTH]; /* source ether addr */
66 unsigned short h_proto; /* packet type ID field */
69 unsigned short hw_type; /* hardware type(ARPHRD_ETHER) */
70 unsigned short p_type; /* protocol type(ETH_P_IP) */
71 unsigned char hw_len; /* hardware address length */
72 unsigned char p_len; /* protocol address length */
73 unsigned short operation; /* ARP opcode */
74 unsigned char s_hwaddr[MAC_ADDRESS_LENGTH]; /* sender hardware address */
75 unsigned char s_IPaddr[IP_ADDRESS_LENGTH]; /* sender IP address */
76 unsigned char t_hwaddr[MAC_ADDRESS_LENGTH]; /* target hardware address */
77 unsigned char t_IPaddr[IP_ADDRESS_LENGTH]; /* target IP address */
78 unsigned char pad[18]; /* pad for min. Ethernet payload (60 bytes) */
82 NETCONFIG_IP_CONFLICT_STATE_UNKNOWN,
83 NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED,
84 NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED
85 } ip_conflict_state_e;
91 static struct timer_data td = {
92 MIN_ARP_SEND_TIME, MIN_ARP_SEND_TIME
96 static bool initial_bursts = true;
97 bool is_ip_conflict_detect_enabled = false;
98 static gboolean send_arp(gpointer data);
99 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac);
100 ip_conflict_state_e conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
102 struct sock_data *sd;
104 typedef unsigned int in_addr_t;
106 union uchar_to_uint {
108 unsigned char uchar[IP_ADDRESS_LENGTH];
111 static unsigned int __convert_uchar_to_uint(unsigned char b[IP_ADDRESS_LENGTH])
114 union uchar_to_uint u;
115 for (; idx < IP_ADDRESS_LENGTH; ++idx)
116 u.uchar[idx] = b[idx];
121 static gboolean __arp_reply_timeout_cb(gpointer data)
124 INFO("Ignore timeout cb");
125 return G_SOURCE_REMOVE;
129 sd->arp_reply_timer = -1;
131 if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED &&
132 sd->iteration == CONFLICT_REMOVE_ITERATION_LIMIT) {
134 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
135 __netconfig_wifi_notify_ip_conflict("resolved", GRATUITOUS_ARP_MAC_ADDR);
136 initial_bursts = true;
139 if (sd->timer_id > 0)
140 g_source_remove(sd->timer_id);
141 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
142 return G_SOURCE_REMOVE;
145 static gboolean __netconfig_check_arp_receive(GIOChannel *source,
146 GIOCondition condition, gpointer data)
148 struct sock_data *sd = data;
149 gchar buffer[ARP_PACKET_SIZE] = {0, };
150 gsize bytes_read = 0;
151 struct arp_message arp_recv;
152 char sbuf[WLAN_MAC_ADDR_MAX];
153 char tbuf[WLAN_MAC_ADDR_MAX];
154 const char *default_ip = NULL;
156 if (g_io_channel_read_chars(source, buffer, ARP_PACKET_SIZE,
157 &bytes_read, NULL) == G_IO_STATUS_NORMAL) {
158 unsigned int target_ip = 0;
160 memset(&arp_recv, 0, sizeof(arp_recv));
161 memcpy(&arp_recv, buffer, sizeof(buffer));
163 default_ip = netconfig_get_default_ipaddress();
164 if (default_ip == NULL) {
165 INFO("ip address is not set yet");
168 target_ip = inet_addr(default_ip);
171 /* Only handle ARP replies */
172 if (arp_recv.operation != htons(ARPOP_REPLY))
175 UCHAR_TO_ADDRESS(arp_recv.t_hwaddr, tbuf);
176 UCHAR_TO_ADDRESS(arp_recv.s_hwaddr, sbuf);
178 int zero_mac = strcmp(tbuf , GRATUITOUS_ARP_MAC_ADDR);
180 DBG("Broadcast packet.\n");
183 DBG("our mac= %s source mac= %s target mac= %s", netconfig_get_default_mac_address(), sbuf, tbuf);
184 int mac_cmp = strcmp(tbuf , netconfig_get_default_mac_address());
186 INFO("Packet not intended to us.\n");
190 mac_cmp = strcmp(sbuf, netconfig_get_default_mac_address());
191 DBG("target ip = %d source ip = %d", target_ip, __convert_uchar_to_uint(arp_recv.s_IPaddr));
192 if ((mac_cmp != 0) && (__convert_uchar_to_uint(arp_recv.s_IPaddr) == target_ip)) {
194 if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED) {
195 INFO("ip conflict is detected !\n");
196 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED;
197 __netconfig_wifi_notify_ip_conflict("conflict", sbuf);
198 sd->timeout = BURST_ARP_SEND_TIME;
201 if (sd->arp_reply_timer != -1) {
202 g_source_remove(sd->arp_reply_timer);
203 sd->arp_reply_timer = -1;
207 g_source_remove(sd->timer_id);
208 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
216 static void __close_channel_and_sock(struct sock_data *sd)
218 GError *error = NULL;
223 if (G_IO_STATUS_NORMAL !=
224 g_io_channel_shutdown(sd->chk_conflict_sock_io, FALSE,
226 INFO("Failure received while shutdown io channel[%d]:[%s]", error->code, error->message);
230 g_io_channel_unref(sd->chk_conflict_sock_io);
231 g_source_remove(sd->chk_conflict_data_id);
232 sd->chk_conflict_data_id = -1;
234 close(sd->chk_conflict_sd);
235 sd->chk_conflict_sd = -1;
238 static int __open_channel_and_sock(struct sock_data *sd)
243 if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
244 INFO("socket Failed.\n");
248 sd->chk_conflict_sock_io = g_io_channel_unix_new(sd->chk_conflict_sd);
249 if (sd->chk_conflict_sock_io == NULL) {
250 INFO("Failed to create channel");
251 close(sd->chk_conflict_sd);
252 sd->chk_conflict_sd = -1;
256 g_io_channel_set_close_on_unref(sd->chk_conflict_sock_io, TRUE);
257 if (G_IO_STATUS_NORMAL != g_io_channel_set_encoding(sd->chk_conflict_sock_io, NULL, NULL))
258 INFO("Failed to set encoding NULL on io channel");
260 if (G_IO_STATUS_NORMAL != g_io_channel_set_flags(sd->chk_conflict_sock_io,
261 G_IO_FLAG_NONBLOCK, NULL))
262 INFO("Failed to set flags on io channel");
264 sd->chk_conflict_data_id = g_io_add_watch(sd->chk_conflict_sock_io, G_IO_IN,
265 __netconfig_check_arp_receive, sd);
266 DBG("socket %d", sd->chk_conflict_sd);
270 static gboolean send_arp(gpointer data)
272 struct sock_data *sd = data;
273 struct ether_addr *source_mac = NULL;
274 struct arp_message arp;
275 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
276 unsigned int source_ip = 0;
277 unsigned int target_ip = 0;
278 const unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
279 struct sockaddr_ll addr = {0};
280 struct ifreq net_ifr;
283 const char *default_ip = NULL;
284 const char *if_name = NULL;
285 static int initial_send_arp_count = 0;
287 if (initial_bursts && initial_send_arp_count >= INITIAL_BURST_ARP_COUNT) {
288 initial_bursts = false;
289 initial_send_arp_count = 0;
293 initial_send_arp_count++;
295 const char *mac = netconfig_get_default_mac_address();
298 source_mac = ether_aton(mac);
299 if (source_mac == NULL) {
300 INFO("Mac address is NULL");
304 memset(&arp, 0, sizeof(arp));
306 unsigned char broadcast_mac_addr[MAC_ADDRESS_LENGTH];
307 memset(broadcast_mac_addr, 0xff, sizeof(broadcast_mac_addr));
308 memcpy(arp.h_dest, broadcast_mac_addr, MAC_ADDRESS_LENGTH); /* MAC dest */
309 memcpy(arp.h_source, source_mac, MAC_ADDRESS_LENGTH); /* MAC source */
311 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
312 arp.hw_type = htons(ARPHRD_ETHER); /* hardware type */
313 arp.p_type = htons(ETH_P_IP); /* protocol type (ARP message) */
314 arp.hw_len = MAC_ADDRESS_LENGTH; /* hardware address length */
315 arp.p_len = IP_ADDRESS_LENGTH; /* protocol address length */
316 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
317 default_ip = netconfig_get_default_ipaddress();
318 if (default_ip == NULL) {
319 INFO("ip address is not set yet");
323 source_ip = inet_addr(ARP_SOURCE_IP);
324 target_ip = inet_addr(default_ip);
325 memcpy(arp.s_IPaddr, &source_ip, IP_ADDRESS_LENGTH); /* source IP address */
326 memcpy(arp.s_hwaddr, source_mac, MAC_ADDRESS_LENGTH); /* source hardware address */
327 memcpy(arp.t_IPaddr, &target_ip, IP_ADDRESS_LENGTH); /* target IP addressshek" */
329 memset(&net_ifr, 0, sizeof(net_ifr));
330 /* ifreq structure creation */
331 if_name = netconfig_get_default_ifname();
332 size_t if_name_len = strlen(if_name);
334 if (if_name_len == 0) {
335 INFO("Error : Unable to get interface name ");
339 if (if_name_len < sizeof(net_ifr.ifr_name)) {
340 memcpy(net_ifr.ifr_name, netconfig_get_default_ifname(), if_name_len);
341 net_ifr.ifr_name[if_name_len] = 0;
343 INFO("Error : Interface name is too long");
347 if (ioctl(sd->chk_conflict_sd, SIOCGIFINDEX, &net_ifr) == -1) {
348 INFO("ioctl Failed. Error..... = %s\n",
349 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
353 ifindex = net_ifr.ifr_ifindex;
354 /* Construct the destination address */
355 addr.sll_family = AF_PACKET;
356 addr.sll_ifindex = ifindex;
357 addr.sll_halen = ETHER_ADDR_LEN;
358 addr.sll_protocol = htons(ETH_P_ARP);
359 memcpy(addr.sll_addr, broadcast_addr, ETHER_ADDR_LEN);
361 if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0,
362 (struct sockaddr*)&addr, sizeof(addr)) < 0) {
363 INFO("Sending ARP Packet Failed. Error. = %s\n",
364 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
365 __close_channel_and_sock(sd);
366 if (__open_channel_and_sock(sd) == -1)
367 INFO("__open_channel_and_sock failed");
370 DBG("Sent ARP Packet \n");
373 g_source_remove(sd->timer_id);
376 if (conflict_state == NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED || initial_bursts)
377 sd->timeout = BURST_ARP_SEND_TIME;
379 sd->timeout = td.initial_time;
381 /* Adding timeout callback for arp request */
382 sd->arp_reply_timer = g_timeout_add(1000, __arp_reply_timeout_cb,
383 (gpointer) &sd->arp_reply_timer);
387 g_source_remove(sd->timer_id);
388 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
392 struct sock_data * start_ip_conflict_mon(void)
394 if (is_ip_conflict_detect_enabled == true) {
395 INFO("detection mode is set to true");
399 initial_bursts = true;
401 sd = g_try_malloc0(sizeof(struct sock_data));
403 INFO("Failed to malloc sock_data");
406 sd->chk_conflict_data_id = -1;
407 sd->chk_conflict_sd = -1;
411 if (__open_channel_and_sock(sd) == -1) {
412 INFO("__open_channel_and_sock failed");
417 sd->timeout = td.initial_time;
419 is_ip_conflict_detect_enabled = true;
420 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
425 void stop_ip_conflict_mon()
433 if (-1 < sd->chk_conflict_sd)
434 __close_channel_and_sock(sd);
436 if (sd->timer_id > 0) {
437 g_source_remove(sd->timer_id);
442 is_ip_conflict_detect_enabled = false;
443 conflict_state = NETCONFIG_IP_CONFLICT_STATE_UNKNOWN;
444 INFO("Monitoring stopped");
447 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
449 GVariantBuilder *builder = NULL;
451 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
452 g_variant_builder_add(builder, "{sv}", "state", g_variant_new_string(state));
453 g_variant_builder_add(builder, "{sv}", "mac", g_variant_new_string(mac));
455 wifi_emit_ip_conflict_event((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
456 g_variant_builder_unref(builder);
458 /* send notification using net-popup */
459 if (!strcmp(state, "conflict"))
460 netconfig_send_notification_to_net_popup(NETCONFIG_ADD_IP_CONFLICT_NOTI, mac);
462 netconfig_send_notification_to_net_popup(NETCONFIG_DEL_IP_CONFLICT_NOTI, mac);
467 gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *context,
470 g_return_val_if_fail(wifi != NULL, TRUE);
472 if (detect == false) {
474 stop_ip_conflict_mon();
476 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
477 wifi_complete_ip_conflict_set_enable(wifi, context);
482 if (start_ip_conflict_mon() == NULL) {
483 INFO("Failed to start IP conflict monitoring");
484 netconfig_error_dbus_method_return(context,
485 NETCONFIG_ERROR_INTERNAL, "Failed");
486 wifi_complete_ip_conflict_set_enable(wifi, context);
490 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
491 wifi_complete_ip_conflict_set_enable(wifi, context);
496 wifi_complete_ip_conflict_set_enable(wifi, context);
500 gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation *context)
502 g_return_val_if_fail(wifi != NULL, TRUE);
503 GVariant *param = NULL;
504 param = g_variant_new("(b)", is_ip_conflict_detect_enabled);
505 g_dbus_method_invocation_return_value(context, param);
509 gboolean handle_set_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context, guint initial_time)
511 g_return_val_if_fail(wifi != NULL, TRUE);
512 INFO("%d", initial_time);
513 if (initial_time > MAX_ARP_SEND_TIME || initial_time < MIN_ARP_SEND_TIME) {
514 netconfig_error_dbus_method_return(context,
515 NETCONFIG_ERROR_INTERNAL, "Failed");
519 td.initial_time = 1000 * initial_time;
521 stop_ip_conflict_mon();
522 start_ip_conflict_mon();
523 wifi_complete_set_ip_conflict_period(wifi, context);
527 gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context)
529 g_return_val_if_fail(wifi != NULL, TRUE);
530 GVariant *param = NULL;
531 param = g_variant_new("(u)", conflict_state);
532 g_dbus_method_invocation_return_value(context, param);
536 gboolean handle_get_ip_conflict_period(Wifi *wifi, GDBusMethodInvocation *context)
538 g_return_val_if_fail(wifi != NULL, TRUE);
539 GVariant *param = NULL;
540 param = g_variant_new("(u)", td.initial_time/1000);
541 g_dbus_method_invocation_return_value(context, param);