5 * Copyright (C) 2007 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
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
32 #include <net/ethernet.h>
36 int __connman_iface_create_identifier(struct connman_iface *iface)
39 struct ether_addr *eth;
42 DBG("iface %p", iface);
44 sk = socket(PF_INET, SOCK_DGRAM, 0);
48 memset(&ifr, 0, sizeof(ifr));
49 ifr.ifr_ifindex = iface->index;
51 err = ioctl(sk, SIOCGIFNAME, &ifr);
54 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
61 iface->identifier = malloc(18);
62 if (iface->identifier == NULL)
65 eth = (void *) &ifr.ifr_hwaddr.sa_data;
66 sprintf(iface->identifier, "%02X-%02X-%02X-%02X-%02X-%02X",
67 eth->ether_addr_octet[0],
68 eth->ether_addr_octet[1],
69 eth->ether_addr_octet[2],
70 eth->ether_addr_octet[3],
71 eth->ether_addr_octet[4],
72 eth->ether_addr_octet[5]);
77 int __connman_iface_init_via_inet(struct connman_iface *iface)
82 DBG("iface %p", iface);
84 sk = socket(PF_INET, SOCK_DGRAM, 0);
88 memset(&ifr, 0, sizeof(ifr));
89 ifr.ifr_ifindex = iface->index;
91 err = ioctl(sk, SIOCGIFNAME, &ifr);
94 err = ioctl(sk, SIOCGIFFLAGS, &ifr);
101 if (ifr.ifr_flags & IFF_UP)
102 iface->state = CONNMAN_IFACE_STATE_ENABLED;
104 iface->state = CONNMAN_IFACE_STATE_OFF;
106 if (ifr.ifr_flags & IFF_RUNNING) {
107 if (!(iface->flags & CONNMAN_IFACE_FLAG_NOCARRIER))
108 iface->state = CONNMAN_IFACE_STATE_CARRIER;
114 static int __connman_iface_up(struct connman_iface *iface)
119 DBG("iface %p", iface);
121 sk = socket(PF_INET, SOCK_DGRAM, 0);
125 memset(&ifr, 0, sizeof(ifr));
126 ifr.ifr_ifindex = iface->index;
128 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
133 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
138 if (ifr.ifr_flags & IFF_UP) {
143 ifr.ifr_flags |= IFF_UP;
145 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
158 static int __connman_iface_down(struct connman_iface *iface)
163 DBG("iface %p", iface);
165 sk = socket(PF_INET, SOCK_DGRAM, 0);
169 memset(&ifr, 0, sizeof(ifr));
170 ifr.ifr_ifindex = iface->index;
172 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
177 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
182 if (!(ifr.ifr_flags & IFF_UP)) {
187 ifr.ifr_flags &= ~IFF_UP;
189 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
200 int __connman_iface_start(struct connman_iface *iface)
204 DBG("iface %p", iface);
206 if (iface->flags & CONNMAN_IFACE_FLAG_STARTED)
209 err = __connman_iface_up(iface);
211 if (iface->driver->start) {
212 err = iface->driver->start(iface);
217 iface->flags |= CONNMAN_IFACE_FLAG_STARTED;
222 int __connman_iface_stop(struct connman_iface *iface)
226 DBG("iface %p", iface);
228 __connman_dhcp_release(iface);
230 connman_iface_clear_ipv4(iface);
232 if (iface->flags & CONNMAN_IFACE_FLAG_RUNNING) {
233 if (iface->driver->disconnect)
234 iface->driver->disconnect(iface);
235 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;
238 if (!(iface->flags & CONNMAN_IFACE_FLAG_STARTED))
241 if (iface->driver->stop) {
242 err = iface->driver->stop(iface);
247 iface->flags &= ~CONNMAN_IFACE_FLAG_STARTED;
249 err = __connman_iface_down(iface);
256 int __connman_iface_connect(struct connman_iface *iface,
257 struct connman_network *network)
259 DBG("iface %p name %s passphrase %s", iface,
260 network->identifier, network->passphrase);
262 if (iface->flags & CONNMAN_IFACE_FLAG_RUNNING) {
263 __connman_dhcp_release(iface);
265 connman_iface_clear_ipv4(iface);
267 if (iface->driver->disconnect)
268 iface->driver->disconnect(iface);
270 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;
273 if (iface->driver->connect)
274 iface->driver->connect(iface, network);
276 iface->flags |= CONNMAN_IFACE_FLAG_RUNNING;
281 int __connman_iface_disconnect(struct connman_iface *iface)
283 DBG("iface %p", iface);
285 __connman_dhcp_release(iface);
287 connman_iface_clear_ipv4(iface);
289 if (!(iface->flags & CONNMAN_IFACE_FLAG_RUNNING))
292 if (iface->driver->disconnect)
293 iface->driver->disconnect(iface);
295 iface->flags &= ~CONNMAN_IFACE_FLAG_RUNNING;