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
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
33 #include <net/ethernet.h>
37 int __connman_iface_create_identifier(struct connman_iface *iface)
40 struct ether_addr *eth;
43 DBG("iface %p", iface);
45 sk = socket(PF_INET, SOCK_DGRAM, 0);
49 memset(&ifr, 0, sizeof(ifr));
50 ifr.ifr_ifindex = iface->index;
52 err = ioctl(sk, SIOCGIFNAME, &ifr);
55 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
62 iface->identifier = malloc(18);
63 if (iface->identifier == NULL)
66 eth = (void *) &ifr.ifr_hwaddr.sa_data;
67 sprintf(iface->identifier, "%02X-%02X-%02X-%02X-%02X-%02X",
68 eth->ether_addr_octet[0],
69 eth->ether_addr_octet[1],
70 eth->ether_addr_octet[2],
71 eth->ether_addr_octet[3],
72 eth->ether_addr_octet[4],
73 eth->ether_addr_octet[5]);
78 int __connman_iface_init_via_inet(struct connman_iface *iface)
83 DBG("iface %p", iface);
85 sk = socket(PF_INET, SOCK_DGRAM, 0);
89 memset(&ifr, 0, sizeof(ifr));
90 ifr.ifr_ifindex = iface->index;
92 err = ioctl(sk, SIOCGIFNAME, &ifr);
95 err = ioctl(sk, SIOCGIFFLAGS, &ifr);
102 if (ifr.ifr_flags & IFF_UP)
103 iface->state = CONNMAN_IFACE_STATE_ENABLED;
105 iface->state = CONNMAN_IFACE_STATE_OFF;
107 if (ifr.ifr_flags & IFF_RUNNING) {
108 if (!(iface->flags & CONNMAN_IFACE_FLAG_NOCARRIER))
109 iface->state = CONNMAN_IFACE_STATE_CARRIER;
115 static int __connman_iface_up(struct connman_iface *iface)
120 DBG("iface %p", iface);
122 sk = socket(PF_INET, SOCK_DGRAM, 0);
126 memset(&ifr, 0, sizeof(ifr));
127 ifr.ifr_ifindex = iface->index;
129 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
134 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
139 if (ifr.ifr_flags & IFF_UP) {
144 ifr.ifr_flags |= IFF_UP;
146 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
159 static int __connman_iface_down(struct connman_iface *iface)
164 DBG("iface %p", iface);
166 sk = socket(PF_INET, SOCK_DGRAM, 0);
170 memset(&ifr, 0, sizeof(ifr));
171 ifr.ifr_ifindex = iface->index;
173 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
178 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
183 if (!(ifr.ifr_flags & IFF_UP)) {
188 ifr.ifr_flags &= ~IFF_UP;
190 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
201 int __connman_iface_start(struct connman_iface *iface)
205 DBG("iface %p", iface);
207 if (iface->flags & CONNMAN_IFACE_FLAG_STARTED)
210 err = __connman_iface_up(iface);
212 if (iface->driver->start) {
213 err = iface->driver->start(iface);
218 iface->flags |= CONNMAN_IFACE_FLAG_STARTED;
223 int __connman_iface_stop(struct connman_iface *iface)
227 DBG("iface %p", iface);
229 __connman_dhcp_release(iface);
231 connman_iface_clear_ipv4(iface);
233 if (iface->flags & CONNMAN_IFACE_FLAG_RUNNING) {
234 if (iface->driver->disconnect)
235 iface->driver->disconnect(iface);
236 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;
239 if (!(iface->flags & CONNMAN_IFACE_FLAG_STARTED))
242 if (iface->driver->stop) {
243 err = iface->driver->stop(iface);
248 iface->flags &= ~CONNMAN_IFACE_FLAG_STARTED;
250 err = __connman_iface_down(iface);
257 int __connman_iface_connect(struct connman_iface *iface,
258 struct connman_network *network)
260 DBG("iface %p name %s passphrase %s", iface,
261 network->identifier, network->passphrase);
263 if (iface->flags & CONNMAN_IFACE_FLAG_RUNNING) {
264 __connman_dhcp_release(iface);
266 connman_iface_clear_ipv4(iface);
268 if (iface->driver->disconnect)
269 iface->driver->disconnect(iface);
271 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;
274 if (iface->driver->connect)
275 iface->driver->connect(iface, network);
277 iface->flags |= CONNMAN_IFACE_FLAG_RUNNING;
282 int __connman_iface_disconnect(struct connman_iface *iface)
284 DBG("iface %p", iface);
286 __connman_dhcp_release(iface);
288 connman_iface_clear_ipv4(iface);
290 if (!(iface->flags & CONNMAN_IFACE_FLAG_RUNNING))
293 if (iface->driver->disconnect)
294 iface->driver->disconnect(iface);
296 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;