5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
37 enum connman_ipconfig_method method;
38 struct in_addr address;
39 struct in_addr netmask;
40 struct in_addr broadcast;
43 static int set_ipv4(struct connman_element *element, struct connman_ipv4 *ipv4)
46 struct sockaddr_in addr;
49 DBG("element %p ipv4 %p", element, ipv4);
51 sk = socket(PF_INET, SOCK_DGRAM, 0);
55 memset(&ifr, 0, sizeof(ifr));
56 ifr.ifr_ifindex = element->index;
58 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
63 DBG("ifname %s", ifr.ifr_name);
65 memset(&addr, 0, sizeof(addr));
66 addr.sin_family = AF_INET;
67 addr.sin_addr = ipv4->address;
68 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
70 err = ioctl(sk, SIOCSIFADDR, &ifr);
73 DBG("address setting failed (%s)", strerror(errno));
75 memset(&addr, 0, sizeof(addr));
76 addr.sin_family = AF_INET;
77 addr.sin_addr = ipv4->netmask;
78 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
80 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
83 DBG("netmask setting failed (%s)", strerror(errno));
85 memset(&addr, 0, sizeof(addr));
86 addr.sin_family = AF_INET;
87 addr.sin_addr = ipv4->broadcast;
88 memcpy(&ifr.ifr_broadaddr, &addr, sizeof(ifr.ifr_broadaddr));
90 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
93 DBG("broadcast setting failed (%s)", strerror(errno));
100 static int clear_ipv4(struct connman_element *element)
103 struct sockaddr_in addr;
106 DBG("element %p", element);
108 sk = socket(PF_INET, SOCK_DGRAM, 0);
112 memset(&ifr, 0, sizeof(ifr));
113 ifr.ifr_ifindex = element->index;
115 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
120 DBG("ifname %s", ifr.ifr_name);
122 memset(&addr, 0, sizeof(addr));
123 addr.sin_family = AF_INET;
124 addr.sin_addr.s_addr = INADDR_ANY;
125 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
127 //err = ioctl(sk, SIOCDIFADDR, &ifr);
128 err = ioctl(sk, SIOCSIFADDR, &ifr);
132 if (err < 0 && errno != EADDRNOTAVAIL) {
133 DBG("address removal failed (%s)", strerror(errno));
140 static char *index2name(int index)
148 sk = socket(PF_INET, SOCK_DGRAM, 0);
152 memset(&ifr, 0, sizeof(ifr));
153 ifr.ifr_ifindex = index;
155 err = ioctl(sk, SIOCGIFNAME, &ifr);
162 return strdup(ifr.ifr_name);
165 static int ipv4_probe(struct connman_element *element)
167 struct connman_service *service;
168 struct connman_ipconfig *ipconfig;
169 struct connman_element *connection;
170 struct connman_ipv4 ipv4;
171 const char *address = NULL, *netmask = NULL, *broadcast = NULL;
172 const char *nameserver = NULL;
173 char *timeserver = NULL;
175 DBG("element %p name %s", element, element->name);
177 connman_element_get_value(element,
178 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
179 connman_element_get_value(element,
180 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
181 connman_element_get_value(element,
182 CONNMAN_PROPERTY_ID_IPV4_BROADCAST, &broadcast);
184 connman_element_get_value(element,
185 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
186 connman_element_get_value(element,
187 CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, ×erver);
189 DBG("address %s", address);
190 DBG("netmask %s", netmask);
191 DBG("broadcast %s", broadcast);
193 if (address == NULL || netmask == NULL)
196 memset(&ipv4, 0, sizeof(ipv4));
197 ipv4.address.s_addr = inet_addr(address);
198 ipv4.netmask.s_addr = inet_addr(netmask);
200 ipv4.broadcast.s_addr = inet_addr(broadcast);
202 ipv4.broadcast.s_addr = ipv4.address.s_addr |
203 ~ipv4.netmask.s_addr;
205 set_ipv4(element, &ipv4);
207 service = __connman_element_get_service(element);
209 if (nameserver != NULL)
210 __connman_service_append_nameserver(service, nameserver);
212 connman_timeserver_append(timeserver);
214 connection = connman_element_create(NULL);
216 connection->type = CONNMAN_ELEMENT_TYPE_CONNECTION;
217 connection->index = element->index;
218 connection->devname = index2name(element->index);
220 ipconfig = __connman_service_get_ipconfig(service);
221 if (ipconfig != NULL)
222 __connman_ipconfig_set_element_ipv6_gateway(
223 ipconfig, connection);
225 if (connman_element_register(connection, element) < 0)
226 connman_element_unref(connection);
231 static void ipv4_remove(struct connman_element *element)
233 const char *nameserver = NULL;
234 char *timeserver = NULL;
236 DBG("element %p name %s", element, element->name);
238 connman_element_get_value(element,
239 CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
240 connman_element_get_value(element,
241 CONNMAN_PROPERTY_ID_IPV4_TIMESERVER, ×erver);
243 connman_timeserver_remove(timeserver);
245 if (nameserver != NULL) {
246 struct connman_service *service;
248 service = __connman_element_get_service(element);
249 __connman_service_remove_nameserver(service, nameserver);
255 static struct connman_driver ipv4_driver = {
257 .type = CONNMAN_ELEMENT_TYPE_IPV4,
258 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
260 .remove = ipv4_remove,
263 int __connman_ipv4_init(void)
265 return connman_driver_register(&ipv4_driver);
268 void __connman_ipv4_cleanup(void)
270 connman_driver_unregister(&ipv4_driver);