5 * Copyright (C) 2007-2008 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>
33 #include <net/route.h>
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
38 #include <connman/plugin.h>
39 #include <connman/driver.h>
40 #include <connman/log.h>
42 enum connman_ipv4_method {
43 CONNMAN_IPV4_METHOD_UNKNOWN = 0,
44 CONNMAN_IPV4_METHOD_OFF = 1,
45 CONNMAN_IPV4_METHOD_STATIC = 2,
46 CONNMAN_IPV4_METHOD_DHCP = 3,
50 enum connman_ipv4_method method;
51 struct in_addr address;
52 struct in_addr netmask;
53 struct in_addr gateway;
54 struct in_addr network;
55 struct in_addr broadcast;
56 struct in_addr nameserver;
59 static int set_ipv4(struct connman_element *element,
60 struct connman_ipv4 *ipv4)
64 struct sockaddr_in *addr;
67 DBG("element %p ipv4 %p", element, ipv4);
69 sk = socket(PF_INET, SOCK_DGRAM, 0);
73 memset(&ifr, 0, sizeof(ifr));
74 ifr.ifr_ifindex = element->netdev.index;
76 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
81 DBG("ifname %s", ifr.ifr_name);
83 addr = (struct sockaddr_in *) &ifr.ifr_addr;
84 addr->sin_family = AF_INET;
85 addr->sin_addr = ipv4->address;
87 err = ioctl(sk, SIOCSIFADDR, &ifr);
90 DBG("address setting failed (%s)", strerror(errno));
92 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
93 addr->sin_family = AF_INET;
94 addr->sin_addr = ipv4->netmask;
96 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
99 DBG("netmask setting failed (%s)", strerror(errno));
101 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
102 addr->sin_family = AF_INET;
103 addr->sin_addr = ipv4->broadcast;
105 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
108 DBG("broadcast setting failed (%s)", strerror(errno));
110 memset(&rt, 0, sizeof(rt));
111 rt.rt_flags = RTF_UP | RTF_GATEWAY;
113 addr = (struct sockaddr_in *) &rt.rt_dst;
114 addr->sin_family = AF_INET;
115 addr->sin_addr.s_addr = INADDR_ANY;
117 addr = (struct sockaddr_in *) &rt.rt_gateway;
118 addr->sin_family = AF_INET;
119 addr->sin_addr = ipv4->gateway;
121 addr = (struct sockaddr_in *) &rt.rt_genmask;
122 addr->sin_family = AF_INET;
123 addr->sin_addr.s_addr = INADDR_ANY;
125 err = ioctl(sk, SIOCADDRT, &rt);
130 DBG("default route failed (%s)", strerror(errno));
137 static int clear_ipv4(struct connman_element *element)
140 struct sockaddr_in *addr;
143 DBG("element %p", element);
145 sk = socket(PF_INET, SOCK_DGRAM, 0);
149 memset(&ifr, 0, sizeof(ifr));
150 ifr.ifr_ifindex = element->netdev.index;
152 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
157 DBG("ifname %s", ifr.ifr_name);
159 addr = (struct sockaddr_in *) &ifr.ifr_addr;
160 addr->sin_family = AF_INET;
161 addr->sin_addr.s_addr = INADDR_ANY;
163 //err = ioctl(sk, SIOCDIFADDR, &ifr);
164 err = ioctl(sk, SIOCSIFADDR, &ifr);
168 if (err < 0 && errno != EADDRNOTAVAIL) {
169 DBG("address removal failed (%s)", strerror(errno));
176 static int ipv4_probe(struct connman_element *element)
178 struct connman_element *resolver;
179 struct connman_ipv4 ipv4;
180 const char *address = NULL, *netmask = NULL, *gateway = NULL;
182 DBG("element %p name %s", element, element->name);
184 connman_element_get_value(element,
185 CONNMAN_PROPERTY_TYPE_IPV4_ADDRESS, &address);
186 connman_element_get_value(element,
187 CONNMAN_PROPERTY_TYPE_IPV4_NETMASK, &netmask);
188 connman_element_get_value(element,
189 CONNMAN_PROPERTY_TYPE_IPV4_GATEWAY, &gateway);
191 DBG("address %s", address);
192 DBG("netmask %s", netmask);
193 DBG("gateway %s", gateway);
195 memset(&ipv4, 0, sizeof(ipv4));
196 ipv4.address.s_addr = inet_addr(address);
197 ipv4.netmask.s_addr = inet_addr(netmask);
198 ipv4.gateway.s_addr = inet_addr(gateway);
200 set_ipv4(element, &ipv4);
202 resolver = connman_element_create();
204 resolver->type = CONNMAN_ELEMENT_TYPE_RESOLVER;
205 resolver->netdev.name = g_strdup(element->netdev.name);
207 connman_element_set_data(element, resolver);
209 connman_element_register(resolver, element);
214 static void ipv4_remove(struct connman_element *element)
216 struct connman_element *resolver = connman_element_get_data(element);
218 DBG("element %p name %s", element, element->name);
220 connman_element_set_data(element, NULL);
222 connman_element_unregister(resolver);
224 connman_element_unref(resolver);
229 static struct connman_driver ipv4_driver = {
231 .type = CONNMAN_ELEMENT_TYPE_IPV4,
233 .remove = ipv4_remove,
236 static int ipv4_init(void)
238 return connman_driver_register(&ipv4_driver);
241 static void ipv4_exit(void)
243 connman_driver_unregister(&ipv4_driver);
246 CONNMAN_PLUGIN_DEFINE("ipv4", "IPv4 configuration plugin", VERSION,
247 ipv4_init, ipv4_exit)