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"
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
48 #define MIN_ARP_SEND_TIME 2000
49 #define MAX_ARP_SEND_TIME 32000
50 #define GRATUITOUS_ARP_MAC_ADDR "00:00:00:00:00:00"
51 #define UCHAR_TO_ADDRESS(hwaddr, buf) do {\
52 snprintf(buf, WLAN_MAC_ADDR_MAX,\
53 "%02X:%02X:%02X:%02X:%02X:%02X", hwaddr[0], hwaddr[1],\
54 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);\
59 unsigned char h_dest[MAC_ADDRESS_LENGTH]; /* destination ether addr */
60 unsigned char h_source[MAC_ADDRESS_LENGTH]; /* source ether addr */
61 unsigned short h_proto; /* packet type ID field */
64 unsigned short hw_type; /* hardware type(ARPHRD_ETHER) */
65 unsigned short p_type; /* protocol type(ETH_P_IP) */
66 unsigned char hw_len; /* hardware address length */
67 unsigned char p_len; /* protocol address length */
68 unsigned short operation; /* ARP opcode */
69 unsigned char s_hwaddr[MAC_ADDRESS_LENGTH]; /* sender hardware address */
70 unsigned char s_IPaddr[IP_ADDRESS_LENGTH]; /* sender IP address */
71 unsigned char t_hwaddr[MAC_ADDRESS_LENGTH]; /* target hardware address */
72 unsigned char t_IPaddr[IP_ADDRESS_LENGTH]; /* target IP address */
73 unsigned char pad[18]; /* pad for min. Ethernet payload (60 bytes) */
77 IP_CONFLICT_MODE_EXPONENTIAL = 0x00,
78 IP_CONFLICT_MODE_PERIODIC,
83 NETCONFIG_IP_CONFLICT_STATE_UNKNOWN,
84 NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED,
85 NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED
86 } ip_conflict_state_e;
91 ip_conflict_mode_e mode;
93 static struct timer_data td = {
94 MIN_ARP_SEND_TIME, MIN_ARP_SEND_TIME, IP_CONFLICT_MODE_PERIODIC
98 bool is_ip_conflict_detect_enabled = true;
99 static gboolean send_arp(gpointer data);
100 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac);
101 ip_conflict_state_e conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
103 struct sock_data *sd;
105 typedef unsigned int in_addr_t;
107 union uchar_to_uint {
109 unsigned char uchar[IP_ADDRESS_LENGTH];
112 static unsigned int __convert_uchar_to_uint(unsigned char b[IP_ADDRESS_LENGTH])
115 union uchar_to_uint u;
116 for (; idx < IP_ADDRESS_LENGTH; ++idx)
117 u.uchar[idx] = b[idx];
122 static gboolean __arp_reply_timeout_cb(gpointer data)
125 sd->arp_reply_timer = -1;
126 if (sd->timer_id != -1)
127 g_source_remove(sd->timer_id);
129 if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED && sd->iteration == 5) {
131 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
132 __netconfig_wifi_notify_ip_conflict("resolved", GRATUITOUS_ARP_MAC_ADDR);
135 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
136 return G_SOURCE_REMOVE;
139 static gboolean __netconfig_check_arp_receive(GIOChannel *source,
140 GIOCondition condition, gpointer data)
142 struct sock_data *sd = data;
143 gchar buffer[ARP_PACKET_SIZE] = {0, };
144 gsize bytes_read = 0;
145 struct arp_message arp_recv;
146 char sbuf[WLAN_MAC_ADDR_MAX];
147 char tbuf[WLAN_MAC_ADDR_MAX];
148 const char *default_ip = NULL;
150 if (g_io_channel_read_chars(source, buffer, ARP_PACKET_SIZE,
151 &bytes_read, NULL) == G_IO_STATUS_NORMAL) {
152 unsigned int target_ip = 0;
154 memset(&arp_recv, 0, sizeof(arp_recv));
155 memcpy(&arp_recv, buffer, sizeof(buffer));
157 default_ip = netconfig_get_default_ipaddress();
158 if (default_ip == NULL) {
159 INFO("ip address is not set yet");
162 target_ip = inet_addr(default_ip);
165 /* Only handle ARP replies */
166 if (arp_recv.operation != htons(ARPOP_REPLY))
169 UCHAR_TO_ADDRESS(arp_recv.t_hwaddr, tbuf);
170 UCHAR_TO_ADDRESS(arp_recv.s_hwaddr, sbuf);
172 int zero_mac = strcmp(tbuf , GRATUITOUS_ARP_MAC_ADDR);
174 DBG("Broadcast packet.\n");
177 DBG("our mac= %s source mac= %s target mac= %s", netconfig_get_default_mac_address(), sbuf, tbuf);
178 int mac_cmp = strcmp(tbuf , netconfig_get_default_mac_address());
180 INFO("Packet not intended to us.\n");
184 mac_cmp = strcmp(sbuf, netconfig_get_default_mac_address());
185 DBG("target ip = %d source ip = %d", target_ip, __convert_uchar_to_uint(arp_recv.s_IPaddr));
186 if ((mac_cmp != 0) && (__convert_uchar_to_uint(arp_recv.s_IPaddr) == target_ip)) {
188 if (conflict_state != NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED) {
189 INFO("ip conflict is detected !\n");
190 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_DETECTED;
191 __netconfig_wifi_notify_ip_conflict("conflict", sbuf);
194 if (sd->arp_reply_timer != -1) {
195 g_source_remove(sd->arp_reply_timer);
196 sd->arp_reply_timer = -1;
200 g_source_remove(sd->timer_id);
201 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
210 static gboolean send_arp(gpointer data)
212 struct sock_data *sd = data;
213 struct ether_addr *source_mac = NULL;
214 struct arp_message arp;
215 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
216 unsigned int source_ip = 0;
217 unsigned int target_ip = 0;
218 const unsigned char broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
219 struct sockaddr_ll addr = {0};
220 struct ifreq net_ifr;
223 const char *default_ip = NULL;
225 const char *mac = netconfig_get_default_mac_address();
228 source_mac = ether_aton(mac);
229 if (source_mac == NULL) {
230 INFO("Mac address is NULL");
234 memset(&arp, 0, sizeof(arp));
236 unsigned char broadcast_mac_addr[MAC_ADDRESS_LENGTH];
237 memset(broadcast_mac_addr, 0xff, sizeof(broadcast_mac_addr));
238 memcpy(arp.h_dest, broadcast_mac_addr, MAC_ADDRESS_LENGTH); /* MAC dest */
239 memcpy(arp.h_source, source_mac, MAC_ADDRESS_LENGTH); /* MAC source */
241 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
242 arp.hw_type = htons(ARPHRD_ETHER); /* hardware type */
243 arp.p_type = htons(ETH_P_IP); /* protocol type (ARP message) */
244 arp.hw_len = MAC_ADDRESS_LENGTH; /* hardware address length */
245 arp.p_len = IP_ADDRESS_LENGTH; /* protocol address length */
246 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
247 default_ip = netconfig_get_default_ipaddress();
248 if (default_ip == NULL) {
249 INFO("ip address is not set yet");
253 source_ip = inet_addr(default_ip);
254 target_ip = inet_addr(default_ip);
255 memcpy(arp.s_IPaddr, &source_ip, IP_ADDRESS_LENGTH); /* source IP address */
256 memcpy(arp.s_hwaddr, source_mac, MAC_ADDRESS_LENGTH); /* source hardware address */
257 memcpy(arp.t_IPaddr, &target_ip, IP_ADDRESS_LENGTH); /* target IP addressshek" */
259 memset(&net_ifr, 0, sizeof(net_ifr));
260 /* ifreq structure creation */
261 size_t if_name_len = strlen(netconfig_get_default_ifname());
262 if (if_name_len < sizeof(net_ifr.ifr_name)) {
263 memcpy(net_ifr.ifr_name, netconfig_get_default_ifname(), if_name_len);
264 net_ifr.ifr_name[if_name_len] = 0;
266 INFO("Error : Interface name is too long");
270 if (ioctl(sd->chk_conflict_sd, SIOCGIFINDEX, &net_ifr) == -1) {
271 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
272 INFO("ioctl Failed. Error..... = %s\n", error_buf);
276 ifindex = net_ifr.ifr_ifindex;
277 /* Construct the destination address */
278 addr.sll_family = AF_PACKET;
279 addr.sll_ifindex = ifindex;
280 addr.sll_halen = ETHER_ADDR_LEN;
281 addr.sll_protocol = htons(ETH_P_ARP);
282 memcpy(addr.sll_addr, broadcast_addr, ETHER_ADDR_LEN);
284 if (sendto(sd->chk_conflict_sd, &arp, sizeof(arp), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
285 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
286 INFO("Sending ARP Packet Failed. Error. = %s\n", error_buf);
288 if (-1 < sd->chk_conflict_sd) {
289 close(sd->chk_conflict_sd);
290 sd->chk_conflict_sd = -1;
294 if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
295 INFO("socket %d", sd->chk_conflict_sd);
296 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
297 INFO("socket Failed. Error = %s\n", error_buf);
301 DBG("Sent ARP Packet \n");
304 g_source_remove(sd->timer_id);
305 if (td.mode == IP_CONFLICT_MODE_PERIODIC)
306 sd->timeout = td.initial_time;
307 else if (td.mode == IP_CONFLICT_MODE_EXPONENTIAL && 2*sd->timeout < MAX_ARP_SEND_TIME)
310 /* Adding timeout callback for arp request */
311 sd->arp_reply_timer = g_timeout_add(1000, __arp_reply_timeout_cb,
312 (gpointer) &sd->arp_reply_timer);
316 g_source_remove(sd->timer_id);
317 sd->timer_id = g_timeout_add(sd->timeout, send_arp, sd);
321 struct sock_data * start_ip_conflict_mon(void)
323 if (is_ip_conflict_detect_enabled == false) {
324 INFO("detection mode is set to false");
328 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
330 sd = g_try_malloc0(sizeof(struct sock_data));
332 INFO("Failed to malloc sock_data");
335 sd->chk_conflict_data_id = -1;
336 sd->chk_conflict_sd = -1;
340 if ((sd->chk_conflict_sd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP))) == -1) {
341 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
342 INFO("socket Failed. Error = %s\n", error_buf);
346 sd->chk_conflict_sock_io = g_io_channel_unix_new(sd->chk_conflict_sd);
347 if (sd->chk_conflict_sock_io == NULL) {
348 INFO("Failed to create channel");
354 g_io_channel_set_close_on_unref(sd->chk_conflict_sock_io, TRUE);
356 if (G_IO_STATUS_NORMAL != g_io_channel_set_encoding(sd->chk_conflict_sock_io,
358 INFO("Failed to set encoding NULL on io channel");
359 if (G_IO_STATUS_NORMAL != g_io_channel_set_flags(sd->chk_conflict_sock_io,
360 G_IO_FLAG_NONBLOCK, NULL))
361 INFO("Failed to set flags on io channel");
362 sd->chk_conflict_data_id = g_io_add_watch(sd->chk_conflict_sock_io, G_IO_IN,
363 __netconfig_check_arp_receive, sd);
364 DBG("socket %d", sd->chk_conflict_sd);
366 sd->timeout = td.initial_time;
372 void stop_ip_conflict_mon()
375 GError* error = NULL;
380 if (-1 < sd->chk_conflict_sd) {
381 if (G_IO_STATUS_NORMAL !=
382 g_io_channel_shutdown(sd->chk_conflict_sock_io, FALSE,
384 INFO("Failure received while shutdown io channel[%d]:[%s]", error->code, error->message);
387 g_io_channel_unref(sd->chk_conflict_sock_io);
388 g_source_remove(sd->chk_conflict_data_id);
389 sd->chk_conflict_data_id = -1;
390 close(sd->chk_conflict_sd);
391 sd->chk_conflict_sd = -1;
393 if (sd->timer_id > 0) {
394 g_source_remove(sd->timer_id);
399 INFO("Monitoring stopped");
402 static void __netconfig_wifi_notify_ip_conflict(char *state, char *mac)
404 GVariantBuilder *builder = NULL;
406 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
407 g_variant_builder_add(builder, "{sv}", "state", g_variant_new_string(state));
408 g_variant_builder_add(builder, "{sv}", "mac", g_variant_new_string(mac));
410 wifi_emit_ip_conflict_event((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
411 g_variant_builder_unref(builder);
416 gboolean handle_ip_conflict_set_enable(Wifi *wifi, GDBusMethodInvocation *context,
419 g_return_val_if_fail(wifi != NULL, FALSE);
422 if (detect == false) {
423 is_ip_conflict_detect_enabled = false;
424 conflict_state = NETCONFIG_IP_CONFLICT_STATE_UNKNOWN;
426 stop_ip_conflict_mon();
428 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
429 wifi_complete_ip_conflict_set_enable(wifi, context);
433 is_ip_conflict_detect_enabled = true;
434 conflict_state = NETCONFIG_IP_CONFLICT_STATE_CONFLICT_NOT_DETECTED;
436 if (start_ip_conflict_mon() == NULL) {
437 INFO("Failed to start IP conflict monitoring");
438 netconfig_error_dbus_method_return(context,
439 NETCONFIG_ERROR_INTERNAL, "Failed");
440 wifi_complete_ip_conflict_set_enable(wifi, context);
444 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AlreadyExists");
445 wifi_complete_ip_conflict_set_enable(wifi, context);
450 wifi_complete_ip_conflict_set_enable(wifi, context);
454 gboolean handle_is_ip_conflict_detect_enabled(Wifi *wifi, GDBusMethodInvocation *context)
456 g_return_val_if_fail(wifi != NULL, FALSE);
457 GVariant *param = NULL;
458 param = g_variant_new("(b)", is_ip_conflict_detect_enabled);
459 g_dbus_method_invocation_return_value(context, param);
463 gboolean handle_set_ip_conflict_mode(Wifi *wifi, GDBusMethodInvocation *context,
464 guint mode, guint initial_time)
466 g_return_val_if_fail(wifi != NULL, FALSE);
467 if (initial_time < MAX_ARP_SEND_TIME && initial_time > MIN_ARP_SEND_TIME)
470 td.initial_time = 1000 * initial_time;
473 stop_ip_conflict_mon();
474 start_ip_conflict_mon();
475 wifi_complete_set_ip_conflict_mode(wifi, context);
479 gboolean handle_get_ip_conflict_state(Wifi *wifi, GDBusMethodInvocation *context)
481 g_return_val_if_fail(wifi != NULL, FALSE);
482 GVariant *param = NULL;
483 param = g_variant_new("(u)", conflict_state);
484 g_dbus_method_invocation_return_value(context, param);